Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- //-------------------------------------------------
- template <typename Type>
- Type Clump (Type value, Type min, Type max);
- //-------------------------------------------------
- #pragma pack (push, 1)
- union Color
- {
- unsigned char data [4];
- struct { unsigned char b, g, r, a; };
- COLORREF colorref;
- RGBQUAD rgbquad;
- Color (int r_, int g_, int b_, int a_ = 255) :
- r (Clump (r_, 0, 255)),
- g (Clump (g_, 0, 255)),
- b (Clump (b_, 0, 255)),
- a (Clump (a_, 0, 255))
- {}
- Color (const Color & that)
- {
- r = that.r;
- g = that.g;
- b = that.b;
- a = that.a;
- }
- Color (COLORREF color)
- {
- colorref = color;
- a = 255;
- }
- Color (RGBQUAD color)
- {
- rgbquad = color;
- a = 255;
- }
- Color (unsigned char c) :
- Color (c, c, c)
- {}
- Color () :
- Color (0, 0, 0)
- {}
- Color & operator += (const Color & that)
- {
- int r0 = r, g0 = g, b0 = b;
- r0 += that.r;
- g0 += that.g;
- b0 += that.b;
- r = Clump (r0, 0, 255);
- g = Clump (g0, 0, 255);
- b = Clump (b0, 0, 255);
- return *this;
- }
- Color & operator -= (const Color & that)
- {
- int r0 = r, g0 = g, b0 = b;
- r0 -= that.r;
- g0 -= that.g;
- b0 -= that.b;
- r = Clump (r0, 0, 255);
- g = Clump (g0, 0, 255);
- b = Clump (b0, 0, 255);
- return *this;
- }
- Color & operator /= (const Color & that)
- {
- r /= that.r;
- r /= that.g;
- r /= that.b;
- return *this;
- }
- Color & operator *= (const Color & that)
- {
- r *= that.r;
- r *= that.g;
- r *= that.b;
- return *this;
- }
- unsigned char midvalue ()
- {
- return (r + g + b) / 3;
- }
- operator COLORREF ()
- {
- return RGB (r, g, b);
- }
- operator RGBQUAD ()
- {
- return rgbquad;
- }
- static Color Rand ()
- {
- return Color (rand () % 255, rand () % 255, rand () % 255);
- }
- static Color Black;
- static Color White;
- static Color Red;
- static Color Green;
- static Color Blue;
- static Color Yellow;
- static Color Pink;
- static Color Cyan;
- static Color Magneta;
- static Color DarkCyan;
- static Color DarkMagneta;
- static Color Gray;
- static Color DarkGray;
- static Color DarkestGray;
- };
- //-------------------------------------------------
- Color Color::Black (0, 0, 0 );
- Color Color::White (255, 255, 255);
- Color Color::Red (255, 0, 0 );
- Color Color::Green (0, 255, 0 );
- Color Color::Blue (0, 0, 255);
- Color Color::Yellow (255, 255, 0 );
- Color Color::Pink (255, 54, 200);
- Color Color::Cyan (0, 255, 255);
- Color Color::Magneta (255, 0, 255);
- Color Color::DarkCyan (0, 140, 255);
- Color Color::DarkMagneta (135, 0, 135);
- Color Color::Gray (100, 100, 100);
- Color Color::DarkGray (40, 40, 40 );
- Color Color::DarkestGray (24, 24, 24 );
- //-------------------------------------------------
- #pragma pack (pop)
- //-------------------------------------------------
- inline Color Blend (const Color & a, const Color & b)
- {
- float alpha0 = a.a / 255.0;
- float alpha1 = 1.0 - alpha0;
- Color result = a;
- result.r *= alpha0;
- result.g *= alpha0;
- result.b *= alpha0;
- result += Color (b.r * alpha1,
- b.g * alpha1,
- b.b * alpha1,
- b.a);
- return result;
- }
- //-------------------------------------------------
- inline Color operator <<= (const Color & a, const Color & b)
- {
- return Blend (a, b);
- }
- //-------------------------------------------------
- template <typename Type>
- inline Type Clump (Type value, Type min, Type max)
- {
- if (value > max) value = max;
- if (value < min) value = min;
- return value;
- }
- //-------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement