@@ -30,8 +30,8 @@ internal static double ContrastRatio(this Color color1, Color color2)
3030        // Source WCAG guidelines: https://www.w3.org/TR/WCAG20/#contrast-ratiodef 
3131
3232        // Calculate perceived luminance for both colors 
33-         double  luminance1  =  color1 . PerceivedLuminance ( ) ; 
34-         double  luminance2  =  color2 . PerceivedLuminance ( ) ; 
33+         double  luminance1  =  color1 . RelativeLuminance ( ) ; 
34+         double  luminance2  =  color2 . RelativeLuminance ( ) ; 
3535
3636        // Determine lighter and darker luminance 
3737        double  lighter  =  Math . Max ( luminance1 ,  luminance2 ) ; 
@@ -41,11 +41,11 @@ internal static double ContrastRatio(this Color color1, Color color2)
4141        return  ( lighter  +  0.05 )  /  ( darker  +  0.05 ) ; 
4242    } 
4343
44-     internal  static   double  PerceivedLuminance ( this  Color  color ) 
44+     internal  static   double  RelativeLuminance ( this  Color  color ) 
4545    { 
4646        // Color theory is a massive iceberg. Here's a peek at the tippy top: 
4747
48-         // There's two (main) standards for calculating luminance from RGB values. 
48+         // There's two (main) standards for calculating percieved  luminance from RGB values. 
4949
5050        // + ------------- + ------------------------------------ + ------------------ + ------------------------------------------------------------------------------- + 
5151        // | Standard      | Formula                              | Ref. Section       | Ref. Link                                                                       | 
@@ -61,11 +61,26 @@ internal static double PerceivedLuminance(this Color color)
6161
6262        // NOTE: If we for whatever reason we ever need to optimize this code, 
6363        // we can make approximations using integer math instead of floating point math. 
64-         // The precise values are not critical, as long as the relative luminance is accurate . 
64+         // The precise values are not critical, as long as the proportions are similar and sum to 1 . 
6565        // Like so: return (2 * color.R + 7 * color.G + color.B); 
6666
67+         // Adjust channels relative luminance out of sRGB: 
68+         // https://www.w3.org/WAI/GL/wiki/Relative_luminance#Definition_as_Stated_in_WCAG_2.x 
69+         float  sRGBtoRGB ( float  s ) 
70+         { 
71+             if  ( s  <=  0.03928f ) 
72+                 return  s  /  12.92f ; 
73+ 
74+             return  MathF . Pow ( ( ( s  +  0.055f )  /  1.055f ) ,  2.4f ) ; 
75+         } 
76+ 
77+         var  vec  =  color . ToVector3 ( ) ; 
78+         var  r  =  sRGBtoRGB ( vec . X ) ; 
79+         var  g  =  sRGBtoRGB ( vec . Y ) ; 
80+         var  b  =  sRGBtoRGB ( vec . Z ) ; 
81+ 
6782        // TLDR: We use ITU Rec. 709 standard formula for perceived luminance. 
68-         return  ( 0.2126f  *  color . R  +  0.7152f  *  color . G  +  0.0722  *  color . B )   /   255 ; 
83+         return  ( 0.2126f  *  r  +  0.7152f  *  g  +  0.0722  *  b ) ; 
6984    } 
7085
7186    internal  static   float  FindColorfulness ( this  Color  color ) 
0 commit comments