Add json extraction operator for colour
This commit is contained in:
parent
0946382b71
commit
d3baffc2e5
30
colour.cpp
30
colour.cpp
@ -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 <>
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user