Add json extraction operator for colour

This commit is contained in:
Danny Robson 2011-10-29 21:17:10 +11:00
parent 0946382b71
commit d3baffc2e5
2 changed files with 22 additions and 11 deletions

View File

@ -23,22 +23,32 @@
using namespace util; using namespace util;
colour::colour (double _red, double _green, double _blue, double _alpha):
red (_red),
green (_green), const json::node&
blue (_blue), operator>> (const json::node &node, colour &c) {
alpha (_alpha) c.red = node[0].to_number ();
{ ; } c.green = node[1].to_number ();
c.blue = node[2].to_number ();
try {
c.alpha = node[3].to_number ();
} catch (...) {
c.alpha = 1.0;
}
return node;
}
namespace util { namespace util {
template<> template<>
colour colour
random (void) { random (void) {
return colour (range<double>::UNIT.random (), return colour ({ range<double>::UNIT.random (),
range<double>::UNIT.random (), range<double>::UNIT.random (),
range<double>::UNIT.random (), range<double>::UNIT.random (),
range<double>::UNIT.random ()); range<double>::UNIT.random () });
} }
template <> template <>

View File

@ -20,13 +20,13 @@
#ifndef __UTIL_COLOUR_HPP #ifndef __UTIL_COLOUR_HPP
#define __UTIL_COLOUR_HPP #define __UTIL_COLOUR_HPP
#include "json.hpp"
#include "random.hpp" #include "random.hpp"
#include <iostream> #include <iostream>
namespace util { namespace util {
struct colour { struct colour {
colour (double red, double green, double blue, double alpha = 1.0);
double red; double red;
double green; double green;
@ -39,5 +39,6 @@ namespace util {
} }
std::ostream& operator<< (std::ostream&, const util::colour&); std::ostream& operator<< (std::ostream&, const util::colour&);
const json::node& operator>> (const json::node&, util::colour&);
#endif #endif