Creating the Color Class
You'll use a data type of float to hold your red, green, and blue color components . You'll call your data structure color3 , and you'll implement a few custom constructors, operators, and member methods . You'll also add a few internal color objects to help in your application development:
struct color3 // name { union { struct { float r, g, b; // r, g, and b color data }; float c[3]; }; color3 (){} // do nothing // custom constructor color3( float inR, float inG, float inB ) : r( inR ), g( inG ), b( inB ) { } // assign data using a method void Assign( float inR, float inG, float inB ) { r = inR; g = inG; b = inB; } // take in r,g,b (0-255) full sized components // and convert them void SetRGB( int R, int G, int B ) { r = (float) R / 255.0f; g = (float) G / 255.0f; b = (float) B / 255.0f; } // combine two colors void Composite( color3 color1, color3 color2 ) { r =: .r; g = (color1.g > color2.g) ? color1.g : color2.g; b = (color1.b > color2.b) ? color1.b : color2.b; } /* convert and scale the three floats and combine them into one double word chunk this might come in handy */ unsigned long MakeDWord() { // scale up to 255 before shifting unsigned long iR = (int)(r * 255.f )<< 16; unsigned long iG = (int)(g * 255.f )<< 8; unsigned long iB = (int)(b * 255.f ); return 0xff000000 iR iG iB; } // if any of the values are >1.0 or < 0.0. // whatever falls outside the unit range is clamped void Sat() { if( r > 1 ) r = 1.f; if( g > 1 ) g = 1.f; if( b > 1 ) b = 1.f; if( r < 0 ) r = 0.f; if( g < 0 ) g = 0.f; if( b < 0 ) b = 0.f; } // color operators color3& operator += ( const color3& in ); color3& operator -= ( const color3& in ); color3& operator *= ( const float& in ); color3& operator /= ( const float& in ); // some basic colors. static const color3 Black; static const color3 Gray; static const color3 White; static const color3 Red; static const color3 Green; static const color3 Blue; static const color3 Magenta; static const color3 Cyan; static const color3 Yellow; };