Last post Feb 07, 2007 11:07 PM by SilverTab
Feb 02, 2007 12:21 PM|Sharbel_|LINK
I am working on a project that needs me to calculate simlar colors that the user selects from a 'color wheel' type control. So if the user chooses a Red based color, I need to return all the products that are Red (the products RGB or HEX color will be
stored in the database).
My question is, is there a formula that would determin if 2 colors are related? Example: RGB: 236,19,233 and RGB:117,56,116 are both Purples, and if the second one was stored in the Database, a search by a user who selects the first one as the color, should
return the one in the database. Now if the selected color was RGB: 230,9,9 (Red base), it would *not* be returned because its not similar.
I have a feeling there is a formula where you can set a certain variance and it will 'score' the similarity, but I cant seem to find it.
Any help would be greatly appreciated!
Feb 02, 2007 05:12 PM|SilverTab|LINK
I hope this helps getting you in the right direction.
Working in the RGB color space will be a bit difficult to achieve your objectives. You will first want to convert your selected and target colors to HSB since it what you are really after is matching the Hue of two colors regardless if they differ in Saturation
or Brightness. Hues are measured from 0 to 360 degrees, and Saturaion and Brightness are measured from 0 to 100 percent. Since the interpretation of colors can be a bit subjective you will need to set your allowable Hue tolerance. To keep things simple lets
consider the following colors in the HSB color space and assume you are only after Hue matches
Offset = 20 degrees.
GREEN (120,100,100) - Your Target Color.
PURPLE(290, 70, 50) - Your Selected Color
Calling the following method would return false.
if (IsColorMatch(20, 120, 290))
// Get Color from Database
// Do something else
private bool IsColorMatch(int offset, int targetHue, int selectedHue)
bool retVal = true;
int maxTarget = targetHue + offset;
int minTarget = targetHue - offset;
if ((selectedHue < minTarget) || (selectedHue > maxTarget)
retVal = false;
Hope this helps.
Feb 03, 2007 06:01 PM|Sharbel_|LINK
Thank-you very much, this defintely puts me on the right track. The color picker I am using returns the HEX or RGB color, but I did a quick search on Google and saw some methods to conver to HSB.
I think based on your help I can tackle this on Monday so thanks in advance!
Feb 05, 2007 07:07 PM|Sharbel_|LINK
Ok that defintely helped! One thing that i have noticed is that black, white, and grey colors need to be 'handled' somehow because they can exist in pretty much any Hue.
I was thinking to set a flag if the brightness/saturation was under a certain level to store a special value for white, black (-1 and -2 i was thinking). Do you have a better approach?
I ended up setting those flags for white, grey and black by checking the Saturation and Brightness.. my search results are working exactly as expected.
Feb 07, 2007 11:07 PM|SilverTab|LINK
Your solution for grayscale colors sounds good. You could also test for grayscale prior to converting to HSB by first testing if the RGB values are all the same.
Glad to help.