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 });
const util::colour util::colour::BLACK ({ 0.f, 0.f, 0.f, 1.f });
template <typename T>
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 });
const util::colour util::colour::GREEN ({ 0.f, 1.f, 0.f, 1.f });
const util::colour util::colour::BLUE ({ 0.f, 0.f, 1.f, 1.f });
template <typename T>
const util::colour<T>
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.
//!
//! Data must be an array or 3 or 4 numbers. Guarantees success, or throws a
//! json::type_error.
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.g = static_cast<float> (node[1].as_number ());
c.b = static_cast<float> (node[2].as_number ());
@ -60,24 +72,29 @@ operator>> (const json::node &node, colour &c) {
//-----------------------------------------------------------------------------
namespace util {
template<>
colour
colour4f
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 () });
}
template <>
colour&
randomise (colour &c)
{ return c = random<colour> (); }
colour4f&
randomise (colour4f &c)
{ return c = random<colour4f> (); }
}
//-----------------------------------------------------------------------------
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 << ")";
return os;
}
//-----------------------------------------------------------------------------
template struct util::colour<float>;
template struct util::colour<double>;

View File

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