Is there a way to compare how close two colors are to each other? If to say both of them are blue.
At the moment the way that we compare them is to manually assign each possible color to a color family(red, green, blue...). And then just compare the strings :)
But surely that manual task can be assigned to a neat little algorithm.
views:
904answers:
4You probably want to convert the colors to an HSL model (Hue, Saturation, Lightness) and then compare the values within thresholds in the order HSL. If the hue is within a tolerance deemed as "close", then check the "closeness" of the saturation, and then the lightness.
Depending on what you mean by close, you can compare the RGB hex strings with your favorite number comparison algorithms. Some things to look at would be the hue, saturation, value, etc. I don't remember how those different attributes affect the hex values, but playing around with any color picker that gives you these sliders, and will display the hex should quickly show how things change.
I'm not sure of any algorithms, you may want to consider converting RGB (Red, Green, Blue) values in to HSB (Hue, Saturation, Brightness).
Hue is essentially "color", so you can compare simply on how close the Hue values are.
My first idea is to treat the RGB space as three-dimensional (cartesian) space, and calculate the distance accordingly as
(sqrt (+ (square diff-red)
(square diff-green)
(square diff-blue)))
edit: The usefulness of this depends on what you want to do with it. Just being darker would be interpreted as a different colour this way, I don't know whether you want that.