From 532dc415a484f10b0345488676a2ca6264114aa0 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Thu, 9 Apr 2015 18:00:40 +1000 Subject: [PATCH] colour: allow arbitrary colour dimensions --- colour.cpp | 47 +++++++++---------- colour.hpp | 11 +++-- coord/ops.hpp | 125 +++++++++++++++++++++++++------------------------- 3 files changed, 94 insertions(+), 89 deletions(-) diff --git a/colour.cpp b/colour.cpp index 0c42e298..4bcb145f 100644 --- a/colour.cpp +++ b/colour.cpp @@ -24,37 +24,36 @@ //----------------------------------------------------------------------------- -using namespace util; +using util::colour; +using util::colour4f; //----------------------------------------------------------------------------- -template -const util::colour -util::colour::WHITE (1.f, 1.f, 1.f, 1.f); +#define CONSTANT_COLOUR(NAME,R,G,B) \ +template \ +const util::colour \ +util::colour::NAME = \ + util::colour<4,float> (R,G,B,1) \ + .template redim () \ + .template cast (); -template -const util::colour -util::colour::BLACK (0.f, 0.f, 0.f, 1.f); - -template -const util::colour -util::colour::RED (1.f, 0.f, 0.f, 1.f); - -template -const util::colour -util::colour::GREEN (0.f, 1.f, 0.f, 1.f); - -template -const util::colour -util::colour::BLUE (0.f, 0.f, 1.f, 1.f); +CONSTANT_COLOUR(WHITE,1,1,1); +CONSTANT_COLOUR(BLACK,0,0,0); +CONSTANT_COLOUR(RED, 1,0,0); +CONSTANT_COLOUR(GREEN,0,1,0); +CONSTANT_COLOUR(BLUE, 0,0,1); + + + +///---------------------------------------------------------------------------- //! 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, colour4f &c) { +operator>> (const json::tree::node &node, util::colour4f &c) { c.r = static_cast (node[0].as_number ()); c.g = static_cast (node[1].as_number ()); c.b = static_cast (node[2].as_number ()); @@ -96,5 +95,7 @@ util::operator<< (std::ostream &os, const util::colour4f &c) { //----------------------------------------------------------------------------- -template struct util::colour; -template struct util::colour; +template struct util::colour<3,float>; +template struct util::colour<3,double>; +template struct util::colour<4,float>; +template struct util::colour<4,double>; diff --git a/colour.hpp b/colour.hpp index de23b08a..297fab99 100644 --- a/colour.hpp +++ b/colour.hpp @@ -28,9 +28,10 @@ namespace util { /// An RGBA colour POD type. - template - struct colour : public coord::base<4,T,colour,coord::rgba> { - using coord::base<4,T,util::colour,coord::rgba>::base; + template + struct colour : public coord::base + { + using coord::base::base; static const colour WHITE; static const colour BLACK; @@ -39,8 +40,10 @@ namespace util { static const colour GREEN; }; - typedef colour colour4f; + // Convenience types + typedef colour<4,float> colour4f; + // Serialisation const json::tree::node& operator>> (const json::tree::node&, util::colour4f&); std::ostream& operator<< (std::ostream&, const util::colour4f&); } diff --git a/coord/ops.hpp b/coord/ops.hpp index ff619d4e..e85ab998 100644 --- a/coord/ops.hpp +++ b/coord/ops.hpp @@ -30,6 +30,7 @@ namespace util { template struct point; template struct extent; template struct vector; + template struct colour; /////////////////////////////////////////////////////////////////////// // operation traits @@ -42,46 +43,46 @@ namespace util { > struct traits { }; - //------------------------------------------------------------------------- - template struct traits { typedef extent result; }; - template struct traits { typedef extent result; }; - template struct traits { typedef point result; }; - template struct traits { typedef point result; }; - template struct traits { typedef point result; }; - template struct traits { typedef vector result; }; + template struct traits { typedef colour result; }; + template struct traits { typedef extent result; }; + template struct traits { typedef extent result; }; + template struct traits { typedef point result; }; + template struct traits { typedef point result; }; + template struct traits { typedef point result; }; + template struct traits { typedef vector result; }; } /////////////////////////////////////////////////////////////////////////// // vector operators -#define ELEMENT_OP(OP) \ - template < \ - size_t S, \ - typename T, \ - template class A, \ - template class B \ - > \ - typename coord::traits::result \ - operator OP (A a, B b) \ - { \ - typename coord::traits::result out; \ - for (size_t i = 0; i < S; ++i) \ - out[i] = a[i] OP b[i]; \ - return out; \ - } \ - \ - template < \ - size_t S, \ - typename T, \ - template class A, \ - template class B \ - > \ - typename coord::traits::result& \ - operator PASTE(OP,=) (A& a, B b) \ - { \ - for (size_t i = 0; i < S; ++i) \ - a[i] PASTE(OP,=) b[i]; \ - return a; \ +#define ELEMENT_OP(OP) \ + template < \ + size_t S, \ + typename T, \ + template class A, \ + template class B \ + > \ + typename coord::traits::result \ + operator OP (A a, B b) \ + { \ + typename coord::traits::result out; \ + for (size_t i = 0; i < S; ++i) \ + out[i] = a[i] OP b[i]; \ + return out; \ + } \ + \ + template < \ + size_t S, \ + typename T, \ + template class A, \ + template class B \ + > \ + typename coord::traits::result& \ + operator PASTE(OP,=) (A& a, B b) \ + { \ + for (size_t i = 0; i < S; ++i) \ + a[i] PASTE(OP,=) b[i]; \ + return a; \ } ELEMENT_OP(+) @@ -92,33 +93,33 @@ namespace util { /////////////////////////////////////////////////////////////////////////// // scalar operators -#define SCALAR_OP(OP) \ - template < \ - size_t S, \ - typename T, \ - template class K \ - > \ - K \ - operator OP (T t, K k) \ - { \ - K out; \ - for (size_t i = 0; i < S; ++i) \ - out[i] = t OP k[i]; \ - return out; \ - } \ - \ - template < \ - size_t S, \ - typename T, \ - template class K \ - > \ - K \ - operator OP (K k, T t) \ - { \ - K out; \ - for (size_t i = 0; i < S; ++i) \ - out[i] = k[i] OP t; \ - return out; \ +#define SCALAR_OP(OP) \ + template < \ + size_t S, \ + typename T, \ + template class K \ + > \ + K \ + operator OP (T t, K k) \ + { \ + K out; \ + for (size_t i = 0; i < S; ++i) \ + out[i] = t OP k[i]; \ + return out; \ + } \ + \ + template < \ + size_t S, \ + typename T, \ + template class K \ + > \ + K \ + operator OP (K k, T t) \ + { \ + K out; \ + for (size_t i = 0; i < S; ++i) \ + out[i] = k[i] OP t; \ + return out; \ } SCALAR_OP(+)