Easy color contrast ratios

I was always interested in accessibility, but I never had to comply with any guidelines before. At W3C, accessibility is considered very important, so everything we make needs to pass WCAG 2.0 AA level. Therefore, I found myself calculating color contrast ratios very frequently. It was a very enlightening experience. I used to think that WCAG-mandated contrast ratios were too restrictive and basically tried to force you to use black and white, a sentiment shared by many designers I’ve spoken to. Surprisingly, in practice, I found that in most cases they are very reasonable: When a color combination doesn’t pass WCAG, it usually *is* hard to read. After all, the possible range for a contrast ratio is 1-21 but only ratios lower than 3 don’t pass WCAG AA (4.5 if you have smaller, non-bold text). So, effectively 90% of combinations will pass (82.5% for smaller, non-bold text).

There are plenty of tools out there for this. However, I found that my workflow for checking a contrast ratio with them was far from ideal. I had to convert my CSS colors to hex notation (which I don’t often use myself anymore), check the contrast ratio, then adjust the colors as necessary, covert again etc. In addition, I had to adjust the lightness of the colors with a blindfold, without being able to see the difference my adjustments would make to the contrast ratio. When using semi-transparent colors, it was even worse: Since WCAG only describes an algorithm for opaque colors, all contrast tools only expect that. So, I had to calculate the resulting opaque colors after alpha blending had taken place. After doing that for a few days, I got so fed up that I decided to make my own tool.

In addition, I discovered that there was no documented way of calculating the contrast ratio range that can be produced with a semi-transparent background, so I came up with an algorithm (after many successive failures to find the range intuitively), published it in the w3c-wai-ig mailing list and used the algorithm in my app, effectively making it the first one that can accept semi-transparent colors. If your math is less rusty than mine, I’d appreciate any feedback on my reasoning there.

Below is a list of features that make this tool unique for calculating color contrast ratios:

  • Accepts any CSS color the browser does, not just hex colors. To do this, it defers parsing of the color to the browser, and queries the computed style, which is always rgb() or rgba() with 0-255 ranges which be parsed much more easily than the multitude of different formats than modern browsers accept (and the even more that are coming in the future).
  • Updates as you type, when what you’ve typed can be parsed as a valid CSS color.
  • Accepts semi transparent colors. For semi-transparent backgrounds, the contrast ratio is presented with an error margin, since it can vary depending on the backdrop. In that case, the result circle will not have a solid background, but a visualization of the different possible results and their likelihood (see screenshot).
  • You can share your results by sharing the URL. The URL hashes have a reasonable structure of the form #foreground-on-background, e.g. #black-on-yellow so you can even adjust the URL as a form of input.
  • You can adjust the color by incrementing or decrementing its components with the keyboard arrow keys until you get the contrast right. This is achieved by including my Incrementable library.

Browser support is IE10 and modern versions of Firefox, Safari, Chrome, Opera. Basic support for IE9. No responsive version yet, sorry (but you can always send pull requests!)

Save the link: leaverou.github.com/contrast-ratio

  • http://twitter.com/saertus Andres Botero

    I use accesibility tools all the time for all the designs I make, it is required at my job for being a public service but even if it wasn’t I would be more than happy to do it. This tool will be really helpful! Thanks a lot for giving so much!

  • http://twitter.com/prisca_eyedea Prisca Schmarsow

    Lea,
    you are amazing – sharing such a treasure chest of wonderful tools with us all ;-)

    This is brilliant ~ thank you so much! x

  • http://www.xiel.de/ Felix

    i used contrast ratio tools before, but the possibility to insert rgb(a) & hsl(a) colors directly is a real time saver! thx for sharing this awesome tool!

    maybe one could a additional optional input for bg colors (or even bgimages, gradients, patterns) behind the transparent color. but see this would make a ratio calculation very hard and also displaying the results, as there would be a lot of different ratios. mhhh… :)

    • http://lea.verou.me/ Lea Verou

      It would be tricky for gradients, patterns, images etc, but not when it’s a solid color. Actually, I was thinking about this myself yesterday. :)

  • Pingback: ☞ Easy color contrast ratios | Lea Verou · Pixelscheucher · accessibility, color, contrast, pxsit

  • http://www.rewdy.com/ Rewdy

    Very cool. I work on a lot of government sites where it’s a very important requirement to follow standards. Great tool. Thanks.

  • Pingback: Contrast Ratio Tool - Web Design Weekly

  • Pingback: Some links for light reading (17/10/12) | Max Design

  • Pingback: CSSquirrel : Goggles Are In | opinions and news on web design by Kyle Weems

  • Pingback: | Web Design Florida Organization

  • nniico

    It’s so simple, yet impressive as usual. Should you try to reinvent the wheel, you would come up with interesting results ;)

  • http://rolling-webdesign.com/ Theo

    I use the colour contrast analyser (CCA2.2), however your tool is great and save time, thank you!

  • Pingback: Fronteers 2012 — лучшая конференция по клиентской разработке, часть 1 | NEWS.ptaah.org.ua

  • http://beben-koben.blogspot.com/ Beben Koben

    WOW tools anymore master Lea, it’s one good ♥
    just info, i’m used it too : https://github.com/matthewbj/Colors

  • Pingback: Tweet Parade (no.42 Oct 2012) | gonzoblog

  • http://twitter.com/laukstein Binyamin Laukstein

    Well done, Lea!

    • boo

      foo bar

  • Pingback: Images & Colour - eyelearn

  • Pingback: Revision 93: Farbkontraste und die volle Responsive Dröhnung! | Working Draft

  • http://www.facebook.com/verafice Verah Fice

    Haha, I like the fact you update the icon, too.

  • Andres Gaitan

    Gotcha Lea, I saw this line: “Therefore, I found myself calculating color contrast ratios very frequently.” and I knew you build a calculator… I ♥ your tools, thanks. IIRC from Colombia, It’s a shame I live so far therefore I can’t go to your workshop :(

  • Pingback: Create More Accessible Color Schemes With ‘Contrast Ratio’ | PJExploration

  • http://twitter.com/_mandynicole Mandy McClausky

    I’ve been scouring the web for something nearly identical to this as a Sass function, but alas. Thanks for the awesome tool, Lea!

    • Tobias Michaelsen

      Could this be something like what you were looking for? https://gist.github.com/tobiashm/243103

      It can calculate contrast ratios and generate color combinations that conforms to WCAG. Doesn’t do transparent colors though. So I might need to revisit it sometime.

  • http://www.darklightdusk.nl Luuk Lamers

    Wow! That looks and functions awesome! Did you notice the Contrast | Ratio is one pixel off at some screen sizes? I always wondered if and how one could prevent that.

  • Pingback: Responsive Retrofilling

  • Seth Meranda

    How does `text-shadow` affect the contrast rules, if at all? It seems a light foreground color issue could be negated when an appropriate text-shadow is used. Obviously, this will be very difficult to programmatically test, so I’m asking more in terms of theory.

    • http://lea.verou.me/ Lea Verou

      It does affect it, but keep in mind you can’t rely on that since it won’t be there for every browser.