Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I classify some color to color ranges?

Tags:

c#

colors

wpf

rgb

If I get a lightgray color(for example R=G=B=200) and a dark one(for example R=46,G=41,B=35), I'd like to classify both of them to the simple gray color group(imagine a table).

So, how can I organize the colors to color groups?

like image 987
laszlokiss88 Avatar asked Dec 10 '11 15:12

laszlokiss88


1 Answers

For visual classification of colors, it is often easier to convert the color to HSL or HSV first. To detect grays, you check if the Saturation is below some threshold. To detect any other color, you check the Hue.

public string Classify(Color c)
{
    float hue = c.GetHue();
    float sat = c.GetSaturation();
    float lgt = c.GetLightness();

    if (lgt < 0.2)  return "Blacks";
    if (lgt > 0.8)  return "Whites";

    if (sat < 0.25) return "Grays";

    if (hue < 30)   return "Reds";
    if (hue < 90)   return "Yellows";
    if (hue < 150)  return "Greens";
    if (hue < 210)  return "Cyans";
    if (hue < 270)  return "Blues";
    if (hue < 330)  return "Magentas";
    return "Reds";
}

You could of course use some other divisions.

I made a simple JavaScript application to test this: Color classification

like image 107
Markus Jarderot Avatar answered Nov 05 '22 04:11

Markus Jarderot