Color.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /******************************************************************************
  2. 4-Byte Colors helper functions.
  3. /******************************************************************************/
  4. struct Color // 4-Byte Color
  5. {
  6. union
  7. {
  8. struct{Byte r, g, b, a;}; // red, green, blue, alpha
  9. struct{Byte c[4] ;}; // component
  10. struct{UInt u ;};
  11. struct{VecB2 v2 ;};
  12. struct{VecB v3 ;};
  13. struct{VecB4 v4 ;};
  14. };
  15. Color& zero( ) {u=0; return T;}
  16. Color& set (Byte red, Byte green, Byte blue, Byte alpha=255) {T.r=red; T.g=green; T.b=blue; T.a=alpha; return T;}
  17. Color& set (Byte lum, Byte alpha=255) {T.r= T.g= T.b=lum ; T.a=alpha; return T;}
  18. Bool any()C {return u!=0 ;} // if any component is non-zero
  19. Int lum()C {return Max(r, g, b);} // get luminance
  20. Vec asVec ()C; // return as Color in Vec format (x=red, y=green, z=blue)
  21. Vec4 asVec4()C; // return as Color in Vec4 format (x=red, y=green, z=blue, w=alpha)
  22. Str asText()C; // return as text in following format "r, g, b, a" (using decimal numbers)
  23. Str asHex ()C; // return as text in following format "rrggbbaa" (using hexadecimal numbers)
  24. Bool fromHex(C Str &t); // set from "rrggbbaa" text format (using hexadecimal numbers), false on fail
  25. Bool operator==(C Color &c)C {return u==c.u;}
  26. Bool operator!=(C Color &c)C {return u!=c.u;}
  27. Color() {}
  28. Color(Byte red, Byte green, Byte blue, Byte alpha=255) {set(red, green, blue, alpha);}
  29. Color(Byte lum, Byte alpha=255) {set(lum, alpha);}
  30. Color(C Vec &color); // initialize from RGB color in 0.0 .. 1.0 scale (alpha will be set to full)
  31. Color(C Vec4 &color); // initialize from RGBA color in 0.0 .. 1.0 scale
  32. Color(C VecB &color); // initialize from RGB color taken from XYZ
  33. Color(C VecB4 &color); // initialize from RGBA color taken from XYZW
  34. };
  35. const Color
  36. BLACK ( 0, 0, 0),
  37. GREY (128, 128, 128),
  38. WHITE (255, 255, 255),
  39. RED (255, 0, 0),
  40. GREEN ( 0, 255, 0),
  41. BLUE ( 0, 0, 255),
  42. YELLOW(255, 255, 0),
  43. CYAN ( 0, 255, 255),
  44. PURPLE(255, 0, 255),
  45. ORANGE(255, 128, 0),
  46. TURQ ( 0, 128, 255),
  47. PINK (255, 128, 255),
  48. BROWN (128, 64, 0),
  49. TRANSPARENT(0, 0, 0, 0);
  50. struct Color2
  51. {
  52. Color color[2];
  53. Color2& zero( ) {color[0].zero(); color[1].zero(); return T;}
  54. Color2& set (Byte red, Byte green, Byte blue, Byte alpha=255) {color[0]=color[1].set(red, green, blue, alpha); return T;}
  55. Color2& set (Byte lum, Byte alpha=255) {color[0]=color[1].set(lum, alpha); return T;}
  56. Color& operator[](Int i) {return color[i];}
  57. C Color& operator[](Int i)C {return color[i];}
  58. Bool anyAlpha()C {return (color[0].a+color[1].a)!=0;}
  59. Color2( ) {}
  60. Color2(C Color &col ) {color[0]=color[1]=col;}
  61. Color2(C Color &a, C Color &b) {color[0]=a; color[1]=b;}
  62. };
  63. // these weights should be applied to Color in linear space
  64. const Vec ColorLumWeight (0.2126f, 0.7152f, 0.0722f), // ITU BT.709 - https://en.wikipedia.org/wiki/Rec._709
  65. ColorLumWeight2(0.2990f, 0.5870f, 0.1140f); // ITU BT.601 - https://en.wikipedia.org/wiki/Rec._601
  66. /******************************************************************************/
  67. Color ColorI(Int i); // get one of the few major colors based on 'i' index, for example: ColorI(0)->RED, ColorI(1)->GREEN, ColorI(2)->BLUE, ...
  68. Color ColorInverse (C Color &color ); // get inversed color , (1-r , 1-g , 1-b , a )
  69. Color ColorBrightness ( Flt brightness ); // get color from brightness 0..1 , ( brightness, brightness, brightness, 1 )
  70. Color ColorBrightness (C Color &color, Flt brightness ); // get color modified by brightness 0..1 , (r*brightness, g*brightness, b*brightness, a )
  71. Color ColorBrightnessB (C Color &color, Byte brightness ); // get color modified by brightness 0..255, (r*brightness, g*brightness, b*brightness, a )
  72. Color ColorAlpha ( Flt alpha ); // get color from opacity 0..1 , (1 , 1 , 1 , alpha )
  73. Color ColorAlpha (C Color &color, Flt alpha ); // get color modified by opacity 0..1 , (r , g , b , a*alpha )
  74. Color ColorBA ( Flt brightness, Flt alpha ); // get color from brightness and opacity 0..1 , ( brightness, brightness, brightness, alpha )
  75. Color ColorBA (C Color &color, Flt brightness, Flt alpha ); // get color modified by brightness and opacity 0..1 , (r*brightness, g*brightness, b*brightness, a*alpha )
  76. Color ColorMul ( Flt mul ); // get , ( mul , mul , mul , mul )
  77. Color ColorMul (C Color &color, Flt mul ); // get , (r*mul , g*mul , b*mul , a*mul )
  78. Color ColorMulZeroAlpha(C Color &color, Flt mul ); // get (r*mul , g*mul , b*mul , 0 )
  79. Color ColorMul (C Color &col0 , C Color &col1 ); // get , (r0*r1 , g0*g1 , b0*b1 , a0*a1 )
  80. Color ColorMulZeroAlpha(C Color &col0 , C Color &col1 ); // get (r0*r1 , g0*g1 , b0*b1 , 0 )
  81. Color ColorMul (C Color &col0 , C Color &col1, C Color &col2); // get , (r0*r1*r2 , g0*g1*g2 , b0*b1*b2 , a0*a1*a2)
  82. Color ColorAdd (C Color &col0 , C Color &col1 ); // get , (r0+r1 , g0+g1 , b0+b1 , a0+a1 )
  83. Color ColorAdd (C Color &col0 , C Color &col1, C Color &col2); // get , (r0+r1+r2 , g0+g1+g2 , b0+b1+b2 , a0+a1+a2)
  84. Color Avg (C Color &c0, C Color &c1 ); // get average color of c0, c1
  85. Color Avg (C Color &c0, C Color &c1, C Color &c2 ); // get average color of c0, c1, c2
  86. Color Avg (C Color &c0, C Color &c1, C Color &c2, C Color &c3 ); // get average color of c0, c1, c2, c3
  87. Color Lerp(C Color &c0, C Color &c1, Flt step ); // linear interpolation between colors, returns Lerp(c0, c1, step)
  88. Color Lerp(C Color &c0, C Color &c1, C Color &c2, C Vec &blend); // linear interpolation between colors, returns c0*blend.x + c1*blend.y + c2*blend.z
  89. Color ColorHue( Flt hue); // get color from hue (0..1) = red->yellow->green->cyan->blue->purple->red
  90. Color ColorHue(C Color &color, Flt hue); // get color modified by hue offset
  91. Vec ColorHue(C Vec &color, Flt hue); // get color modified by hue offset
  92. Color ColorHSB(Flt h, Flt s, Flt b ); // get color from hue, saturation, brightness (all in range 0..1)
  93. Vec RgbToHsb(C Vec &rgb ); // convert from RGB to HSB
  94. Vec HsbToRgb(C Vec &hsb ); // convert from HSB to RGB
  95. Vec RgbToYuv(C Vec &rgb ); // convert from RGB to YUV
  96. Vec YuvToRgb(C Vec &yuv ); // convert from YUV to RGB
  97. Int ColorDiffSum(C Color &x, C Color &y); // get difference between colors as a sum of all channel absolute differences
  98. Int ColorDiffMax(C Color &x, C Color &y); // get difference between colors as a max of all channel absolute differences
  99. Flt ColorDiffMax(C Vec &x, C Vec &y); // get difference between colors as a max of all channel absolute differences
  100. Color Blend(C Color &base, C Color &color); // return 'color' blended on top of 'base'
  101. Vec4 Blend(C Vec4 &base, C Vec4 &color); // return 'color' blended on top of 'base'
  102. Vec4 PremultipliedBlend(C Vec4 &base, C Vec4 &color); // return 'color' blended on top of 'base' where 'color' RGB are already premultiplied by Alpha
  103. Vec4 AdditiveBlend(C Vec4 &base, C Vec4 &color); // return 'color' additively blended on top of 'base'
  104. Flt SRGBToLinear( Flt s); // convert 0..1 srgb to 0..1 linear
  105. Vec SRGBToLinear(C Vec &s); // convert 0..1 srgb to 0..1 linear
  106. Flt LinearToSRGB( Flt l); // convert 0..1 linear to 0..1 srgb
  107. Vec LinearToSRGB(C Vec &l); // convert 0..1 linear to 0..1 srgb
  108. Flt LinearLumOfLinearColor(C Vec &l); // get linear photometric luminance (as perceived by human eye) of linear color
  109. Flt LinearLumOfSRGBColor (C Vec &s); // get linear photometric luminance (as perceived by human eye) of srgb color
  110. Flt SRGBLumOfLinearColor(C Vec &l); // get srgb photometric luminance (as perceived by human eye) of linear color
  111. Flt SRGBLumOfSRGBColor (C Vec &s); // get srgb photometric luminance (as perceived by human eye) of srgb color
  112. #if EE_PRIVATE
  113. Str GetColorProfilePath();
  114. #endif
  115. /******************************************************************************/