Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pick good contrast RGB colors programmatically?

Tags:

colors

rgb

Suppose, in your program:

  1. color A is a color we randomly select

  2. Knowing color A, how can I pick a color B that will be in high contrast with color A?

The problem can be further reduced to: "imagine 2 squares filled with color next to one another. It should be unambiguously clear to a human eye that colors are not the same"

Example:

  • Black --> White
  • Blue --> White
like image 717
James Raitsev Avatar asked Aug 31 '11 17:08

James Raitsev


People also ask

How do you find a good contrasting color?

Color combinations Two colors that are on opposite sides of the color wheel. This combination provides a high contrast and high impact color combination – together, these colors will appear brighter and more prominent.

How do you find RGB with contrast?

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.

Does RGB go to 255 or 256?

RGB (Red, Green, Blue) are 8 bit each. The range for each individual colour is 0-255 (as 2^8 = 256 possibilities).


2 Answers

There is some information in the Web Content Accessibility Guidelines (WCAG) 2.0 (http://www.w3.org/TR/2008/REC-WCAG20-20081211)

  1. Visual contrast: http://www.w3.org/TR/2008/REC-WCAG20-20081211/#visual-audio-contrast-contrast

  2. Contrast ratio: http://www.w3.org/TR/2008/REC-WCAG20-20081211/#contrast-ratiodef

  3. Relative luminance : http://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef

There's a good example in this site but he calculate where two colors are enough, not how to get them.

To choose a color with good contrast, I'd go with complementary colors: for example, choose the random color A, transform it to a HSV space, get the complementary hue.

Complementary hue: after you transform color from RGB to HSV, complementary hue will be 180 degrees appart (or 0.5, in a 0-1 normalized hue value). This site has something about it in PHP

like image 54
woliveirajr Avatar answered Sep 23 '22 18:09

woliveirajr


As I was searching for a better way to do this, i stumbled across the Adobe Illustrator guide which mentions how they create complementary colors. They say:

Complement Changes each component of a color to a new value based on the sum of the highest and lowest RGB values in the selected color. Illustrator adds the lowest and highest RGB values of the current color, and then subtracts the value of each component from that number to create new RGB values. For example, suppose you select a color with an RGB value of 102 for red, 153 for green, and 51 for blue. Illustrator adds the high (153) and low (51) values, to end up with a new value (204). Each of the RGB values in the existing color is subtracted from the new value to create new complementary RGB values: 204 – 102 (the current red value) = 102 for the new red value, 204 – 153 (the current green value) = 51 for the new green value, and 204 – 51 (the current blue value) = 153 for the new blue value.

It wouldn't be too hard to do this programmatically and think this time that it might actually work for what you are trying to do.

Good Luck!

like image 29
Kyle Rosenbluth Avatar answered Sep 25 '22 18:09

Kyle Rosenbluth