colour: allow arbitrary colour dimensions
This commit is contained in:
parent
bd88832df3
commit
532dc415a4
47
colour.cpp
47
colour.cpp
@ -24,37 +24,36 @@
|
|||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
using namespace util;
|
using util::colour;
|
||||||
|
using util::colour4f;
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
template <typename T>
|
#define CONSTANT_COLOUR(NAME,R,G,B) \
|
||||||
const util::colour<T>
|
template <size_t S, typename T> \
|
||||||
util::colour<T>::WHITE (1.f, 1.f, 1.f, 1.f);
|
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>
|
CONSTANT_COLOUR(WHITE,1,1,1);
|
||||||
const util::colour<T>
|
CONSTANT_COLOUR(BLACK,0,0,0);
|
||||||
util::colour<T>::BLACK (0.f, 0.f, 0.f, 1.f);
|
CONSTANT_COLOUR(RED, 1,0,0);
|
||||||
|
CONSTANT_COLOUR(GREEN,0,1,0);
|
||||||
template <typename T>
|
CONSTANT_COLOUR(BLUE, 0,0,1);
|
||||||
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::tree::type_error.
|
//! json::tree::type_error.
|
||||||
const json::tree::node&
|
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.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 ());
|
||||||
@ -96,5 +95,7 @@ util::operator<< (std::ostream &os, const util::colour4f &c) {
|
|||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
template struct util::colour<float>;
|
template struct util::colour<3,float>;
|
||||||
template struct util::colour<double>;
|
template struct util::colour<3,double>;
|
||||||
|
template struct util::colour<4,float>;
|
||||||
|
template struct util::colour<4,double>;
|
||||||
|
11
colour.hpp
11
colour.hpp
@ -28,9 +28,10 @@
|
|||||||
|
|
||||||
namespace util {
|
namespace util {
|
||||||
/// An RGBA colour POD type.
|
/// An RGBA colour POD type.
|
||||||
template <typename T>
|
template <size_t S, typename T>
|
||||||
struct colour : public coord::base<4,T,colour,coord::rgba> {
|
struct colour : public coord::base<S,T,colour,coord::rgba>
|
||||||
using coord::base<4,T,util::colour,coord::rgba>::base;
|
{
|
||||||
|
using coord::base<S,T,util::colour,coord::rgba>::base;
|
||||||
|
|
||||||
static const colour WHITE;
|
static const colour WHITE;
|
||||||
static const colour BLACK;
|
static const colour BLACK;
|
||||||
@ -39,8 +40,10 @@ namespace util {
|
|||||||
static const colour GREEN;
|
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&);
|
const json::tree::node& operator>> (const json::tree::node&, util::colour4f&);
|
||||||
std::ostream& operator<< (std::ostream&, const util::colour4f&);
|
std::ostream& operator<< (std::ostream&, const util::colour4f&);
|
||||||
}
|
}
|
||||||
|
125
coord/ops.hpp
125
coord/ops.hpp
@ -30,6 +30,7 @@ namespace util {
|
|||||||
template <size_t,typename> struct point;
|
template <size_t,typename> struct point;
|
||||||
template <size_t,typename> struct extent;
|
template <size_t,typename> struct extent;
|
||||||
template <size_t,typename> struct vector;
|
template <size_t,typename> struct vector;
|
||||||
|
template <size_t,typename> struct colour;
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////
|
||||||
// operation traits
|
// operation traits
|
||||||
@ -42,46 +43,46 @@ namespace util {
|
|||||||
>
|
>
|
||||||
struct traits { };
|
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,colour,colour> { typedef colour<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,extent,extent> { 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,extent,vector> { typedef extent<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,point,extent> { 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,point,vector> { 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,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
|
// vector operators
|
||||||
#define ELEMENT_OP(OP) \
|
#define ELEMENT_OP(OP) \
|
||||||
template < \
|
template < \
|
||||||
size_t S, \
|
size_t S, \
|
||||||
typename T, \
|
typename T, \
|
||||||
template <size_t,typename> class A, \
|
template <size_t,typename> class A, \
|
||||||
template <size_t,typename> class B \
|
template <size_t,typename> class B \
|
||||||
> \
|
> \
|
||||||
typename coord::traits<S,T,A,B>::result \
|
typename coord::traits<S,T,A,B>::result \
|
||||||
operator OP (A<S,T> a, B<S,T> b) \
|
operator OP (A<S,T> a, B<S,T> b) \
|
||||||
{ \
|
{ \
|
||||||
typename coord::traits<S,T,A,B>::result out; \
|
typename coord::traits<S,T,A,B>::result out; \
|
||||||
for (size_t i = 0; i < S; ++i) \
|
for (size_t i = 0; i < S; ++i) \
|
||||||
out[i] = a[i] OP b[i]; \
|
out[i] = a[i] OP b[i]; \
|
||||||
return out; \
|
return out; \
|
||||||
} \
|
} \
|
||||||
\
|
\
|
||||||
template < \
|
template < \
|
||||||
size_t S, \
|
size_t S, \
|
||||||
typename T, \
|
typename T, \
|
||||||
template <size_t,typename> class A, \
|
template <size_t,typename> class A, \
|
||||||
template <size_t,typename> class B \
|
template <size_t,typename> class B \
|
||||||
> \
|
> \
|
||||||
typename coord::traits<S,T,A,B>::result& \
|
typename coord::traits<S,T,A,B>::result& \
|
||||||
operator PASTE(OP,=) (A<S,T>& a, B<S,T> b) \
|
operator PASTE(OP,=) (A<S,T>& a, B<S,T> b) \
|
||||||
{ \
|
{ \
|
||||||
for (size_t i = 0; i < S; ++i) \
|
for (size_t i = 0; i < S; ++i) \
|
||||||
a[i] PASTE(OP,=) b[i]; \
|
a[i] PASTE(OP,=) b[i]; \
|
||||||
return a; \
|
return a; \
|
||||||
}
|
}
|
||||||
|
|
||||||
ELEMENT_OP(+)
|
ELEMENT_OP(+)
|
||||||
@ -92,33 +93,33 @@ namespace util {
|
|||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////
|
||||||
// scalar operators
|
// scalar operators
|
||||||
#define SCALAR_OP(OP) \
|
#define SCALAR_OP(OP) \
|
||||||
template < \
|
template < \
|
||||||
size_t S, \
|
size_t S, \
|
||||||
typename T, \
|
typename T, \
|
||||||
template <size_t,typename> class K \
|
template <size_t,typename> class K \
|
||||||
> \
|
> \
|
||||||
K<S,T> \
|
K<S,T> \
|
||||||
operator OP (T t, K<S,T> k) \
|
operator OP (T t, K<S,T> k) \
|
||||||
{ \
|
{ \
|
||||||
K<S,T> out; \
|
K<S,T> out; \
|
||||||
for (size_t i = 0; i < S; ++i) \
|
for (size_t i = 0; i < S; ++i) \
|
||||||
out[i] = t OP k[i]; \
|
out[i] = t OP k[i]; \
|
||||||
return out; \
|
return out; \
|
||||||
} \
|
} \
|
||||||
\
|
\
|
||||||
template < \
|
template < \
|
||||||
size_t S, \
|
size_t S, \
|
||||||
typename T, \
|
typename T, \
|
||||||
template <size_t,typename> class K \
|
template <size_t,typename> class K \
|
||||||
> \
|
> \
|
||||||
K<S,T> \
|
K<S,T> \
|
||||||
operator OP (K<S,T> k, T t) \
|
operator OP (K<S,T> k, T t) \
|
||||||
{ \
|
{ \
|
||||||
K<S,T> out; \
|
K<S,T> out; \
|
||||||
for (size_t i = 0; i < S; ++i) \
|
for (size_t i = 0; i < S; ++i) \
|
||||||
out[i] = k[i] OP t; \
|
out[i] = k[i] OP t; \
|
||||||
return out; \
|
return out; \
|
||||||
}
|
}
|
||||||
|
|
||||||
SCALAR_OP(+)
|
SCALAR_OP(+)
|
||||||
|
Loading…
Reference in New Issue
Block a user