Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to compare 2 colors in JS, like 'If Color A is darker than #202020'

The background color for one of my pages is set pulled from the background color the users set as their twitter background color. I have a page that has a rounded box with a black border. The border doesnt look good if the background color is dark, so i'd like to remove the border of the background is darker than an arbitrary hex color.

The way I was thinking about doing this was using a regex to pull the 3 RGB values and summing them, and comparing that to my reference color. Is there a better, way to accomplish this?

like image 961
D-Nice Avatar asked Sep 17 '10 00:09

D-Nice


People also ask

How do you compare two colors?

The most common method would be a visual color comparison by looking at two physical color samples side by side under a light source. Color is very relative, so you can compare colors in terms of the other color across dimensions such as hue, lightness and saturation (brightness).

How can you tell if a color is dark or light?

Value refers to the lightness or darkness of a color. A light color is a tint. For example, pink is a tint of red. A dark color is called a shade.


1 Answers

You could write a function that converts between RGB and HSL or HSV, and use the lightness or brightness value.

Wikipedia has the math for HSV -> RGB conversion, but not the other way.

http://en.wikipedia.org/wiki/HSL_and_HSV#Converting_to_RGB

You could also probably pull some JS from this page.

http://www.csgnetwork.com/csgcolorsel4.html

like image 77
haydenmuhl Avatar answered Oct 07 '22 00:10

haydenmuhl