# Implement ce(contrast) using contrast ratio from the w3 web accessibility guidelines

Even after the recent update ce(contrast) still isn't selecting the most contrasting colour between white and black. I've worked around the problem by implementing a formula based on w3 guidelines (https://www.w3.org/TR/WCAG20/#contrast-ratiodef) to calculate it manually, now I adapted that into sudo java code (not a java programmer). Would be really glad to see this implemented. (there might be a mistake in the second function, I didn't test this)

``````// this assumes Color is a type exposing GetR(), GetG() and GetB()

// implemented https://www.w3.org/TR/WCAG20/#relativeluminancedef
double GetLuminance(Color c) {

double rsRGB = c.GetR() / (double)255;
double gsRGB = c.GetG() / (double)255;
double bsRGB = c.GetB() / (double)255;

double r, g, b;

if(rsRGB <= 0.03928)
r = rsRGB/12.92;
else
r = Math.pow((rsRGB + 0.055)/1.055, 2.4);

if(gsRGB <= 0.03928)
g = gsRGB/12.92;
else
g = Math.pow((gsRGB + 0.055)/1.055, 2.4);

if(bsRGB <= 0.03928)
b = bsRGB/12.92;
else
b = Math.pow((bsRGB + 0.055)/1.055, 2.4);

return 0.2126 * r + 0.7152 * g + 0.0722 * bl
}

Color GetBlackOrWhiteForBestContrast(Color c) {

if(GetLuminance(Color.Black) + 0.5 / GetLuminance(c) + 0.05 > GetLuminance(Color.White) + 0.5 / GetLuminance(c) + 0.05)
return Color.Black;
else
return Color.White;

}``````