Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically calculate the contrast ratio between two colors?

Pretty straight-forward, take yellow and white:

back_color = {r:255,g:255,b:255}; //white text_color = {r:255,g:255,b:0}; //yellow 

What law of physics on God's Earth of universal constants, makes the fact that yellow text can't be read on white backgrounds but blue text can?

For the sake of my customizable widget I tried all possible color models that I found conversions functions for; neither can say that green can be on white and yellow can't, based on just numerical comparisons.

I looked at Adsense (which is created by the Budda of all Internet) and guess what they did, they made presets and color cells distance calculations. I can't to do that. My users have the right to pick even the most retina-inflammatory, unaesthetic combinations, as long as the text can still be read.

like image 423
Silviu-Marian Avatar asked Mar 16 '12 07:03

Silviu-Marian


People also ask

How do you find the contrast ratio between two colors?

Calculating a Contrast Ratio(L1 + 0.05) / (L2 + 0.05), whereby: L1 is the relative luminance of the lighter of the colors, and. L2 is the relative luminance of the darker of the colors.

What is the minimum color contrast ratio between key parts of images?

3 Contrast (Minimum) (Level AA): The visual presentation of text and images of text has a contrast ratio of at least 4.5:1, except for the following: Large Text. Large-scale text and images of large-scale text have a contrast ratio of at least 3:1; Incidental.

What is color contrast analyzer?

The Color Contrast Analyzer is a feature in Accessibility Insights for Windows that helps developers investigate contrast ratios. Contrast ratio describes the relative brightness of foreground and background colors on a computer display.

How do you calculate RGB from luminance?

x. Note 1: For the sRGB colorspace, the relative luminance of a color is defined as L = 0.2126 * R + 0.7152 * G + 0.0722 * B where R, G and B are defined as: if RsRGB <= 0.03928 then R = RsRGB/12.92 else R = ((RsRGB+0.055)/1.055) ^ 2.4.


2 Answers

According to Wikipedia, when converting to grayscale representation of luminance, "one must obtain the values of its red, green, and blue" and mix them in next proportion: R:30% G:59% B:11%

Therefore white will have 100% luminance and yellow will have 89%. At the same time, green has as small as 59%. 11% is almost four times lower than 41% difference!

And even lime (#00ff00) is not good for reading large amounts of texts.

IMHO for good contrast colors' brightness should differ at least for 50%. And this brightness should be measured as converted to grayscale.

upd: Recently found a comprehensive tool for that on the web which in order uses formula from w3 document Threshold values could be taken from #1.4 Here is an implementation for this more advanced thing.

function luminance(r, g, b) {     var a = [r, g, b].map(function (v) {         v /= 255;         return v <= 0.03928             ? v / 12.92             : Math.pow( (v + 0.055) / 1.055, 2.4 );     });     return a[0] * 0.2126 + a[1] * 0.7152 + a[2] * 0.0722; } function contrast(rgb1, rgb2) {     var lum1 = luminance(rgb1[0], rgb1[1], rgb1[2]);     var lum2 = luminance(rgb2[0], rgb2[1], rgb2[2]);     var brightest = Math.max(lum1, lum2);     var darkest = Math.min(lum1, lum2);     return (brightest + 0.05)          / (darkest + 0.05); } contrast([255, 255, 255], [255, 255, 0]); // 1.074 for yellow contrast([255, 255, 255], [0, 0, 255]); // 8.592 for blue // minimal recommended contrast ratio is 4.5, or 3 for larger font-sizes 

For more information, check the WCAG 2.0 documentation on how to compute this value.

like image 194
kirilloid Avatar answered Oct 11 '22 23:10

kirilloid


There are various ways for calculating contrast, but a common way is this formula:

brightness = (299*R + 587*G + 114*B) / 1000 

You do this for both colors, and then you take the difference. This obviously gives a much greater contrast for blue on white than yellow on white.

like image 30
orlp Avatar answered Oct 12 '22 00:10

orlp