From d3baffc2e5cca4717f153befd6c52c49deb32f15 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Sat, 29 Oct 2011 21:17:10 +1100 Subject: [PATCH] Add json extraction operator for colour --- colour.cpp | 30 ++++++++++++++++++++---------- colour.hpp | 3 ++- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/colour.cpp b/colour.cpp index 799d191b..aa466b8e 100644 --- a/colour.cpp +++ b/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::UNIT.random (), - range::UNIT.random (), - range::UNIT.random (), - range::UNIT.random ()); + return colour ({ range::UNIT.random (), + range::UNIT.random (), + range::UNIT.random (), + range::UNIT.random () }); } template <> diff --git a/colour.hpp b/colour.hpp index 6eb34ba2..2151d70b 100644 --- a/colour.hpp +++ b/colour.hpp @@ -20,13 +20,13 @@ #ifndef __UTIL_COLOUR_HPP #define __UTIL_COLOUR_HPP +#include "json.hpp" #include "random.hpp" #include 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