Advertisement
HangMan23

Color.h

Nov 2nd, 2020
1,965
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.78 KB | None | 0 0
  1. #pragma once
  2.  
  3. //-------------------------------------------------
  4.  
  5. template <typename Type>
  6. Type Clump (Type value, Type min, Type max);
  7.  
  8. //-------------------------------------------------
  9.  
  10. #pragma pack (push, 1)
  11.  
  12. union Color
  13.  
  14. {
  15.  
  16.     unsigned char data [4];
  17.  
  18.     struct { unsigned char b, g, r, a; };
  19.  
  20.     COLORREF colorref;
  21.     RGBQUAD  rgbquad;
  22.  
  23.     Color (int r_, int g_, int b_, int a_ = 255) :
  24.  
  25.         r (Clump (r_, 0, 255)),
  26.         g (Clump (g_, 0, 255)),
  27.         b (Clump (b_, 0, 255)),
  28.         a (Clump (a_, 0, 255))
  29.  
  30.     {}
  31.  
  32.     Color (const Color & that)
  33.  
  34.     {
  35.    
  36.         r = that.r;
  37.         g = that.g;
  38.         b = that.b;
  39.         a = that.a;
  40.  
  41.     }
  42.  
  43.     Color (COLORREF color)
  44.  
  45.     {
  46.  
  47.         colorref = color;
  48.         a = 255;
  49.    
  50.     }
  51.  
  52.     Color (RGBQUAD color)
  53.  
  54.     {
  55.  
  56.         rgbquad = color;
  57.         a = 255;
  58.  
  59.     }
  60.  
  61.     Color (unsigned char c) :
  62.  
  63.         Color (c, c, c)
  64.  
  65.     {}
  66.  
  67.     Color () :
  68.  
  69.         Color (0, 0, 0)
  70.  
  71.     {}
  72.  
  73.     Color & operator += (const Color & that)
  74.  
  75.     {
  76.    
  77.         int r0 = r, g0 = g, b0 = b;
  78.  
  79.         r0 += that.r;
  80.         g0 += that.g;
  81.         b0 += that.b;
  82.  
  83.         r = Clump (r0, 0, 255);
  84.         g = Clump (g0, 0, 255);
  85.         b = Clump (b0, 0, 255);
  86.  
  87.         return *this;
  88.  
  89.     }
  90.  
  91.     Color & operator -= (const Color & that)
  92.  
  93.     {
  94.    
  95.         int r0 = r, g0 = g, b0 = b;
  96.  
  97.         r0 -= that.r;
  98.         g0 -= that.g;
  99.         b0 -= that.b;
  100.  
  101.         r = Clump (r0, 0, 255);
  102.         g = Clump (g0, 0, 255);
  103.         b = Clump (b0, 0, 255);
  104.  
  105.         return *this;
  106.  
  107.     }
  108.  
  109.     Color & operator /= (const Color & that)
  110.  
  111.     {
  112.    
  113.         r /= that.r;
  114.         r /= that.g;
  115.         r /= that.b;
  116.  
  117.         return *this;
  118.  
  119.     }
  120.  
  121.     Color & operator *= (const Color & that)
  122.  
  123.     {
  124.    
  125.         r *= that.r;
  126.         r *= that.g;
  127.         r *= that.b;
  128.  
  129.         return *this;
  130.  
  131.     }
  132.  
  133.     unsigned char midvalue ()
  134.  
  135.     {
  136.    
  137.         return (r + g + b) / 3;
  138.  
  139.     }
  140.  
  141.     operator COLORREF ()
  142.  
  143.     {
  144.  
  145.         return RGB (r, g, b);
  146.  
  147.     }
  148.  
  149.     operator RGBQUAD ()
  150.  
  151.     {
  152.    
  153.         return rgbquad;
  154.  
  155.     }
  156.  
  157.     static Color Rand ()
  158.  
  159.     {
  160.    
  161.         return Color (rand () % 255, rand () % 255, rand () % 255);
  162.  
  163.     }
  164.  
  165.      static Color Black;
  166.      static Color White;
  167.      static Color Red;
  168.      static Color Green;
  169.      static Color Blue;
  170.      static Color Yellow;
  171.      static Color Pink;
  172.      static Color Cyan;
  173.      static Color Magneta;
  174.      static Color DarkCyan;
  175.      static Color DarkMagneta;
  176.      static Color Gray;
  177.      static Color DarkGray;
  178.      static Color DarkestGray;
  179.  
  180. };
  181.  
  182. //-------------------------------------------------
  183.  
  184.  Color Color::Black       (0,   0,   0  );
  185.  Color Color::White       (255, 255, 255);
  186.  Color Color::Red         (255, 0,   0  );
  187.  Color Color::Green       (0,   255, 0  );
  188.  Color Color::Blue        (0,   0,   255);
  189.  Color Color::Yellow      (255, 255, 0  );
  190.  Color Color::Pink        (255, 54,  200);
  191.  Color Color::Cyan        (0,   255, 255);
  192.  Color Color::Magneta     (255, 0,   255);
  193.  Color Color::DarkCyan    (0,   140, 255);
  194.  Color Color::DarkMagneta (135, 0,   135);
  195.  Color Color::Gray        (100, 100, 100);
  196.  Color Color::DarkGray    (40,  40,  40 );
  197.  Color Color::DarkestGray (24,  24,  24 );
  198.  
  199. //-------------------------------------------------
  200.  
  201. #pragma pack (pop)
  202.  
  203. //-------------------------------------------------
  204.  
  205. inline Color Blend (const Color & a, const Color & b)
  206.  
  207. {
  208.  
  209.     float alpha0 = a.a / 255.0;
  210.     float alpha1 = 1.0 - alpha0;
  211.  
  212.     Color result = a;
  213.  
  214.     result.r *= alpha0;
  215.     result.g *= alpha0;
  216.     result.b *= alpha0;
  217.  
  218.     result += Color (b.r * alpha1,
  219.                      b.g * alpha1,
  220.                      b.b * alpha1,
  221.                      b.a);
  222.  
  223.     return result;
  224.  
  225. }
  226.  
  227. //-------------------------------------------------
  228.  
  229. inline Color operator <<= (const Color & a, const Color & b)
  230.  
  231. {
  232.  
  233.     return Blend (a, b);
  234.  
  235. }
  236.  
  237. //-------------------------------------------------
  238.  
  239. template <typename Type>
  240. inline Type Clump (Type value, Type min, Type max)
  241.  
  242. {
  243.  
  244.     if (value > max) value = max;
  245.     if (value < min) value = min;
  246.  
  247.     return value;
  248.  
  249. }
  250.  
  251. //-------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement