colour: derive from detail::coord

This commit is contained in:
Danny Robson 2015-01-13 18:33:02 +11:00
parent 123ce6fcb4
commit ccc21b2ae8
2 changed files with 47 additions and 23 deletions

View File

@ -28,21 +28,33 @@ using namespace util;
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
const util::colour util::colour::WHITE ({ 1.f, 1.f, 1.f, 1.f }); template <typename T>
const util::colour util::colour::BLACK ({ 0.f, 0.f, 0.f, 1.f }); const util::colour<T>
util::colour<T>::WHITE (1.f, 1.f, 1.f, 1.f);
const util::colour util::colour::RED ({ 1.f, 0.f, 0.f, 1.f }); template <typename T>
const util::colour util::colour::GREEN ({ 0.f, 1.f, 0.f, 1.f }); const util::colour<T>
const util::colour util::colour::BLUE ({ 0.f, 0.f, 1.f, 1.f }); util::colour<T>::BLACK (0.f, 0.f, 0.f, 1.f);
template <typename T>
const util::colour<T>
util::colour<T>::RED (1.f, 0.f, 0.f, 1.f);
template <typename T>
const util::colour<T>
util::colour<T>::GREEN (0.f, 1.f, 0.f, 1.f);
template <typename T>
const util::colour<T>
util::colour<T>::BLUE (0.f, 0.f, 1.f, 1.f);
//-----------------------------------------------------------------------------
//! Extract a colour object from a JSON node. //! Extract a colour object from a JSON node.
//! //!
//! Data must be an array or 3 or 4 numbers. Guarantees success, or throws a //! Data must be an array or 3 or 4 numbers. Guarantees success, or throws a
//! json::type_error. //! json::type_error.
const json::node& const json::node&
operator>> (const json::node &node, colour &c) { operator>> (const json::node &node, colour4f &c) {
c.r = static_cast<float> (node[0].as_number ()); c.r = static_cast<float> (node[0].as_number ());
c.g = static_cast<float> (node[1].as_number ()); c.g = static_cast<float> (node[1].as_number ());
c.b = static_cast<float> (node[2].as_number ()); c.b = static_cast<float> (node[2].as_number ());
@ -60,24 +72,29 @@ operator>> (const json::node &node, colour &c) {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
namespace util { namespace util {
template<> template<>
colour colour4f
random (void) { random (void) {
return colour ({ range<float>::UNIT.random (), return colour4f ({ range<float>::UNIT.random (),
range<float>::UNIT.random (), range<float>::UNIT.random (),
range<float>::UNIT.random (), range<float>::UNIT.random (),
range<float>::UNIT.random () }); range<float>::UNIT.random () });
} }
template <> template <>
colour& colour4f&
randomise (colour &c) randomise (colour4f &c)
{ return c = random<colour> (); } { return c = random<colour4f> (); }
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
std::ostream& std::ostream&
util::operator<< (std::ostream &os, const util::colour &c) { util::operator<< (std::ostream &os, const util::colour4f &c) {
os << "colour(" << c.r << ", " << c.g << ", " << c.b << ", " << c.a << ")"; os << "colour(" << c.r << ", " << c.g << ", " << c.b << ", " << c.a << ")";
return os; return os;
} }
//-----------------------------------------------------------------------------
template struct util::colour<float>;
template struct util::colour<double>;

View File

@ -20,17 +20,22 @@
#ifndef __UTIL_COLOUR_HPP #ifndef __UTIL_COLOUR_HPP
#define __UTIL_COLOUR_HPP #define __UTIL_COLOUR_HPP
#include "detail/coord.hpp"
#include "json.hpp" #include "json.hpp"
#include <iostream> #include <iostream>
namespace util { namespace util {
/// An RGBA colour POD type. /// An RGBA colour POD type.
struct colour { template <typename T>
float r; struct colour : public detail::coord<4, T> {
float g; using detail::coord<4,T>::coord;
float b;
float a; T r;
T g;
T b;
T a;
static const colour WHITE; static const colour WHITE;
static const colour BLACK; static const colour BLACK;
@ -39,8 +44,10 @@ namespace util {
static const colour GREEN; static const colour GREEN;
}; };
std::ostream& operator<< (std::ostream&, const util::colour&); typedef colour<float> colour4f;
const json::node& operator>> (const json::node&, util::colour&);
const json::node& operator>> (const json::node&, util::colour4f&);
std::ostream& operator<< (std::ostream&, const util::colour4f&);
} }
#endif #endif