colour: allow arbitrary colour dimensions

This commit is contained in:
Danny Robson 2015-04-09 18:00:40 +10:00
parent bd88832df3
commit 532dc415a4
3 changed files with 94 additions and 89 deletions

View File

@ -24,37 +24,36 @@
//-----------------------------------------------------------------------------
using namespace util;
using util::colour;
using util::colour4f;
//-----------------------------------------------------------------------------
template <typename T>
const util::colour<T>
util::colour<T>::WHITE (1.f, 1.f, 1.f, 1.f);
#define CONSTANT_COLOUR(NAME,R,G,B) \
template <size_t S, typename T> \
const util::colour<S,T> \
util::colour<S,T>::NAME = \
util::colour<4,float> (R,G,B,1) \
.template redim<S> () \
.template cast<T> ();
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);
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<float> (node[0].as_number ());
c.g = static_cast<float> (node[1].as_number ());
c.b = static_cast<float> (node[2].as_number ());
@ -96,5 +95,7 @@ util::operator<< (std::ostream &os, const util::colour4f &c) {
//-----------------------------------------------------------------------------
template struct util::colour<float>;
template struct util::colour<double>;
template struct util::colour<3,float>;
template struct util::colour<3,double>;
template struct util::colour<4,float>;
template struct util::colour<4,double>;

View File

@ -28,9 +28,10 @@
namespace util {
/// An RGBA colour POD type.
template <typename T>
struct colour : public coord::base<4,T,colour,coord::rgba> {
using coord::base<4,T,util::colour,coord::rgba>::base;
template <size_t S, typename T>
struct colour : public coord::base<S,T,colour,coord::rgba>
{
using coord::base<S,T,util::colour,coord::rgba>::base;
static const colour WHITE;
static const colour BLACK;
@ -39,8 +40,10 @@ namespace util {
static const colour GREEN;
};
typedef colour<float> 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&);
}

View File

@ -30,6 +30,7 @@ namespace util {
template <size_t,typename> struct point;
template <size_t,typename> struct extent;
template <size_t,typename> struct vector;
template <size_t,typename> struct colour;
///////////////////////////////////////////////////////////////////////
// operation traits
@ -42,46 +43,46 @@ namespace util {
>
struct traits { };
//-------------------------------------------------------------------------
template <size_t S, typename T> struct traits<S,T,extent,extent> { typedef extent<S,T> result; };
template <size_t S, typename T> struct traits<S,T,extent,vector> { typedef extent<S,T> result; };
template <size_t S, typename T> struct traits<S,T,point,extent> { typedef point<S,T> result; };
template <size_t S, typename T> struct traits<S,T,point,vector> { typedef point<S,T> result; };
template <size_t S, typename T> struct traits<S,T,vector,point> { typedef point<S,T> result; };
template <size_t S, typename T> struct traits<S,T,vector,vector> { typedef vector<S,T> result; };
template <size_t S, typename T> struct traits<S,T,colour,colour> { typedef colour<S,T> result; };
template <size_t S, typename T> struct traits<S,T,extent,extent> { typedef extent<S,T> result; };
template <size_t S, typename T> struct traits<S,T,extent,vector> { typedef extent<S,T> result; };
template <size_t S, typename T> struct traits<S,T,point,extent> { typedef point<S,T> result; };
template <size_t S, typename T> struct traits<S,T,point,vector> { typedef point<S,T> result; };
template <size_t S, typename T> struct traits<S,T,vector,point> { typedef point<S,T> result; };
template <size_t S, typename T> struct traits<S,T,vector,vector> { typedef vector<S,T> result; };
}
///////////////////////////////////////////////////////////////////////////
// vector operators
#define ELEMENT_OP(OP) \
template < \
size_t S, \
typename T, \
template <size_t,typename> class A, \
template <size_t,typename> class B \
> \
typename coord::traits<S,T,A,B>::result \
operator OP (A<S,T> a, B<S,T> b) \
{ \
typename coord::traits<S,T,A,B>::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 <size_t,typename> class A, \
template <size_t,typename> class B \
> \
typename coord::traits<S,T,A,B>::result& \
operator PASTE(OP,=) (A<S,T>& a, B<S,T> 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 <size_t,typename> class A, \
template <size_t,typename> class B \
> \
typename coord::traits<S,T,A,B>::result \
operator OP (A<S,T> a, B<S,T> b) \
{ \
typename coord::traits<S,T,A,B>::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 <size_t,typename> class A, \
template <size_t,typename> class B \
> \
typename coord::traits<S,T,A,B>::result& \
operator PASTE(OP,=) (A<S,T>& a, B<S,T> 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 <size_t,typename> class K \
> \
K<S,T> \
operator OP (T t, K<S,T> k) \
{ \
K<S,T> out; \
for (size_t i = 0; i < S; ++i) \
out[i] = t OP k[i]; \
return out; \
} \
\
template < \
size_t S, \
typename T, \
template <size_t,typename> class K \
> \
K<S,T> \
operator OP (K<S,T> k, T t) \
{ \
K<S,T> 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 <size_t,typename> class K \
> \
K<S,T> \
operator OP (T t, K<S,T> k) \
{ \
K<S,T> out; \
for (size_t i = 0; i < S; ++i) \
out[i] = t OP k[i]; \
return out; \
} \
\
template < \
size_t S, \
typename T, \
template <size_t,typename> class K \
> \
K<S,T> \
operator OP (K<S,T> k, T t) \
{ \
K<S,T> out; \
for (size_t i = 0; i < S; ++i) \
out[i] = k[i] OP t; \
return out; \
}
SCALAR_OP(+)