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;
colour::colour (double _red, double _green, double _blue, double _alpha):
red (_red),
green (_green),
blue (_blue),
alpha (_alpha)
{ ; }
const json::node&
operator>> (const json::node &node, colour &c) {
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 {
template<>
colour
random (void) {
return colour (range<double>::UNIT.random (),
range<double>::UNIT.random (),
range<double>::UNIT.random (),
range<double>::UNIT.random ());
return colour ({ range<double>::UNIT.random (),
range<double>::UNIT.random (),
range<double>::UNIT.random (),
range<double>::UNIT.random () });
}
template <>

View File

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