colour: move json deserialisation into cpp

This commit is contained in:
Danny Robson 2015-09-08 14:32:17 +10:00
parent 673dfd87dd
commit 701e76c381
2 changed files with 14 additions and 22 deletions

View File

@ -20,6 +20,7 @@
#include "random.hpp"
#include "stream.hpp"
#include <map>
//-----------------------------------------------------------------------------
using util::colour;
@ -341,26 +342,22 @@ util::hsv_to_rgb (colour3f hsv)
}
///----------------------------------------------------------------------------
//! Extract a colour object from a JSON node.
//!
//! Data must be an array or 3 or 4 numbers. Guarantees success, or throws a
//! json::tree::type_error.
const json::tree::node&
operator>> (const json::tree::node &node, util::colour4f &c) {
c.r = static_cast<float> (node[0].as_number ());
c.g = static_cast<float> (node[1].as_number ());
c.b = static_cast<float> (node[2].as_number ());
/// Extract a colour object from a JSON node.
#include "json/tree.hpp"
try {
c.a = static_cast<float> (node[3].as_number ());
} catch (...) {
c.a = 1;
namespace json { namespace tree {
template <>
util::colour4f
io<util::colour4f>::deserialise (const node &root) {
return {
root[0].as<float> (),
root[1].as<float> (),
root[2].as<float> (),
root[3].as<float> (),
};
}
return node;
}
} }
//-----------------------------------------------------------------------------

View File

@ -19,8 +19,6 @@
#include "coord.hpp"
#include "json/tree.hpp"
#include <iostream>
namespace util {
@ -61,9 +59,6 @@ namespace util {
colour3f rgb_to_hsv (colour3f);
colour3f hsv_to_rgb (colour3f);
// Serialisation
const json::tree::node& operator>> (const json::tree::node&, util::colour4f&);
template <size_t S, typename T>
std::ostream& operator<< (std::ostream&, util::colour<S,T>);
}