W3C home > Mailing lists > Public > w3c-wai-ig@w3.org > July to September 2012

Re: Color contrast between semi-transparent colors

From: Lea Verou <lea@w3.org>
Date: Thu, 6 Sep 2012 14:33:00 +0300
Cc: w3c-wai-ig@w3.org, Chris Lilley <chris@w3.org>
Message-Id: <3142B00E-3263-4492-92F0-6768CE52FCC1@w3.org>
To: Shadi Abou-Zahra <shadi@w3.org>
Actually, the algorithm I described seems correct to determine a lower bound. However, when it comes to the upper bound, I found an error in my reasoning: I assumed the color with the maximum contrast for a given color A is the inverse of A. This seems correct for colors close to the edges of the RGB cube, but becomes more and more inaccurate as we move towards the center of the cube. It’s especially incorrect for gray, as the inverse of gray is …gray.

Another possible way to calculate this (that I need to test more) is that the color with the maximum (linear) distance in the RGB cube could be the one that produces the maximum contrast. If that fails too, there are strictly defined mathematical algorithms to find the maximum(s) of any function, which could be applied to the contrast ratio function. I'm quite rusty on multivariable calculus, but I could give it a shot.

I haven't given up on this, and am still looking into it. Any help would be immensely appreciated. I've tried googling for algorithms about maximizing color contrast for a given color, but can't seem to find anything. All I could find was tutorials for designers about when to use white or black text on certain backgrounds or research papers about monitor display contrast :( There must be prior research on this, I'm probably just too useless at searching to find it.


Lea Verou
W3C developer relations
http://w3.org/people/all#leahttp://lea.verou.me ✿ @leaverou






On Sep 6, 2012, at 11:38, Shadi Abou-Zahra wrote:

> Hi Lea,
> 
> Sounds very conclusive. Interesting.
> 
> You say you've tested this with color combinations other than white text on a semi-transparent black background and it seems to work?
> 
> If so then it seems apt to submit to WCAG WG as a proposed technique:
> - <http://www.w3.org/WAI/GL/WCAG20/TECHS-SUBMIT/>
> 
> Best,
>  Shadi
> 
> 
> On 5.9.2012 16:24, Lea Verou wrote:
>> I just had a brainwave and I think I've figured it out! The extremes, i.e. the backgrounds that produce the minimum and maximum contrast are not fixed. They depend on the foreground color (text color). The background* that produces the minimum contrast if combined with the semi transparent background color is the text color, since this is as close as you can get to it. Similarly, the background that produces the maximum contrast is the inverse of the text color. Therefore, the contrast ratio range, is the contrast ratios produced by these two colors.
>> 
>> Allow me to illustrate with a (quite common in modern web design) example:
>> Text color: white (#FFFFFF)
>> Background color: rgba(0, 0, 0, .5) (50% semi-transparent black)
>> 
>> The inverse of white is black.
>> rgba(0, 0, 0, .5) on white produces gray (#808080). The contrast ratio of gray and white is 4.
>> rgba(0, 0, 0, .5)  on black produces black (#000000). The contrast ratio of white and black is 21.
>> 
>> Therefore, the contrast ratio range of white text on rgba(0, 0, 0, .5) is 4 - 21. In other words, the contrast ratio of these two colors is 12.5±8.5. This tells us that we can use this for large text (if AA is sufficient), but we should be wary of using it for body text, since it can go below the 4.5 threshold. Increasing the transparency to .55 passes AA for any size text.
>> 
>> I've tested this algorithm with a number of different colors and it seems to produce accurate results. Please let me know if you see an error in my reasoning, or my math.
>> 
>> If I'm correct, I think this should be added in WCAG. Authors use semi-transparent colors all the time in modern web design, and it would immensely help them to be able to determine whether their backgrounds might produce illegible text.
>> 
>> * I'm using the word "background" for two separate things: The (semi-transparent) background color and what's going to be behind it. I hope the meaning is clear through the context.
>> 
>> Lea Verou
>> W3C developer relations
>> http://w3.org/people/all#leahttp://lea.verou.me ✿ @leaverou
>> 
>> 
>> 
>> 
>> 
>> 
>> On Sep 4, 2012, at 23:48, Lea Verou wrote:
>> 
>>> Hi all,
>>> 
>>> I was studying the color contrast section of WCAG 2.0 [1] and I observed that the algorithm presented assumes opaque colors. In the case of an opaque background, it's easy to calculate the color contrast using the same algorithm, even when the foreground is translucent. However, when the background is semi-transparent (for example, CSS rgba() or hsla() colors) and the colors it’s going to be on are unknown, there is no single contrast ratio, but a range of potential contrast ratios. How would somebody calculate this range? It’s obviously impractical to calculate the ratios for every possible color that might be underneath, these could be in the millions.
>>> 
>>> I tried calculating the min and max ratios by using white and black, but it did not yield accurate results. Consider the example of gray text on hsla(0,0%,50%,0.5) background (gray with 50% transparency). On white, this is gives us gray on hsl(0,0%,75%) which has a ratio of 2.1:1. On black, it gives us gray on hsl(0,0%,25%) which has a ratio of 2.7:1. However, this doesn’t mean that every potential color will yield a ratio between 2.1 and 2.7: If we have gray underneath, the resulting ratio is 1:1.
>>> 
>>> My question is: Which (and how many) colors should be sampled to get an accurate minimum and maximum contrast ratio?
>>> 
>>> [1]: http://www.w3.org/TR/WCAG/#visual-audio-contrast-contrast
>>> 
>>> Lea Verou
>>> W3C developer relations
>>> http://w3.org/people/all#leahttp://lea.verou.me ✿ @leaverou
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>> 
>> 
> 
> -- 
> Shadi Abou-Zahra - http://www.w3.org/People/shadi/
> Activity Lead, W3C/WAI International Program Office
> Evaluation and Repair Tools Working Group (ERT WG)
> Research and Development Working Group (RDWG)
> 
Received on Thursday, 6 September 2012 11:33:14 UTC

This archive was generated by hypermail 2.3.1 : Wednesday, 5 February 2014 07:13:30 UTC