20 Color() : my_channels( 0.0, 0.0, 0.0 ) {}
22 : my_channels( red, green, blue )
29 my_channels[ 0 ] = std::max( 0.0f, std::min( 1.0f, my_channels[ 0 ] ) );
30 my_channels[ 1 ] = std::max( 0.0f, std::min( 1.0f, my_channels[ 1 ] ) );
31 my_channels[ 2 ] = std::max( 0.0f, std::min( 1.0f, my_channels[ 2 ] ) );
35 operator float*() {
return my_channels; }
37 operator const float*()
const {
return my_channels; }
39 Real r()
const {
return my_channels[ 0 ]; }
40 Real g()
const {
return my_channels[ 1 ]; }
41 Real b()
const {
return my_channels[ 2 ]; }
42 Real& r() {
return my_channels[ 0 ]; }
43 Real& g() {
return my_channels[ 1 ]; }
44 Real& b() {
return my_channels[ 2 ]; }
47 Color operator*(
Real v )
const
50 tmp.my_channels[ 0 ] *= v;
51 tmp.my_channels[ 1 ] *= v;
52 tmp.my_channels[ 2 ] *= v;
57 Color operator*( Color other )
const
60 tmp.my_channels[ 0 ] *= other.my_channels[ 0 ];
61 tmp.my_channels[ 1 ] *= other.my_channels[ 1 ];
62 tmp.my_channels[ 2 ] *= other.my_channels[ 2 ];
67 Color operator+( Color other )
const
70 tmp.my_channels[ 0 ] += other.my_channels[ 0 ];
71 tmp.my_channels[ 1 ] += other.my_channels[ 1 ];
72 tmp.my_channels[ 2 ] += other.my_channels[ 2 ];
77 Color& operator+=( Color other )
79 my_channels[ 0 ] += other.my_channels[ 0 ];
80 my_channels[ 1 ] += other.my_channels[ 1 ];
81 my_channels[ 2 ] += other.my_channels[ 2 ];
85 Color sup( Color other )
const
87 other[ 0 ] = std::max( (*
this)[ 0 ], other[ 0 ] );
88 other[ 1 ] = std::max( (*
this)[ 1 ], other[ 1 ] );
89 other[ 2 ] = std::max( (*
this)[ 2 ], other[ 2 ] );
93 enum Channel { Red, Green, Blue };
94 Channel argmax()
const
96 if ( r() >= g() )
return r() >= b() ? Red : Blue;
97 else return g() >= b() ? Green : Blue;
99 Real max()
const {
return std::max( std::max( r(), g() ), b() ); }
100 Real min()
const {
return std::min( std::min( r(), g() ), b() ); }
102 void getHSV(
int & h,
Real & s,
Real & v )
const
105 if ( max() == min() ) h = 0;
107 switch ( argmax() ) {
108 case Red: h = ( (int) ( 60.0 * ( g() - b() ) / ( max() - min() ) + 360.0 ) ) % 360;
110 case Green: h = ( (int) ( 60.0 * ( b() - r() ) / ( max() - min() ) + 120.0 ) );
112 case Blue: h = ( (int) ( 60.0 * ( r() - g() ) / ( max() - min() ) + 240.0 ) );
117 s = max() == 0.0 ? 0.0 : 1.0 - min() / max();
122 void setHSV(
int h,
Real s,
Real v )
124 int t = ( h / 60 ) % 6;
127 Real bl = (int) ( v * ( 1 - s ) );
128 Real bm = (int) ( v * ( 1 - f * s ) );
129 Real bn = (int) ( v * ( 1 - ( 1 - f ) * s ) );
131 case 0: r() = bv; g() = bn; b() = bl;
break;
132 case 1: r() = bm; g() = bv; b() = bl;
break;
133 case 2: r() = bl; g() = bv; b() = bn;
break;
134 case 3: r() = bl; g() = bm; b() = bv;
break;
135 case 4: r() = bn; g() = bl; b() = bv;
break;
136 case 5: r() = bv; g() = bl; b() = bm;
break;