How to classify color in color ranges?

If I get a light color (for example, R = G = B = 200) and dark (for example, R = 46, G = 41, B = 35), I would like to classify them as a simple group of gray color (imagine a table).

So how can I arrange colors for groups of colors?

+6
source share
3 answers

For visual classification of colors, it is often easier to convert color to HSL or HSV . To determine the gray color, you check if the saturation is below a certain 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"; } 

Of course, you could use some other units.

I made a simple JavaScript application to check this out: Color classification

+12
source

There are several ways to classify colors. One way would be to consider rgb as a 3d coordinate, and then all possible colors can be represented inside the box or cube with black in orig and white in the opposite corner, located at (255,255,255). Then all grayish colors will be located close to the diagonal. And red, green and bluish colors are close to the axis. Thus, the classification problem was transformed into a search for the closest distance between a point (color) and a line (gray diagonal) in three-dimensional space. If the distance is below a predetermined level, the color will be classified as gray.

+1
source

How about implementing the color table below:

 public class ColorTable { public Color ColorName { get; set; } //can set color code too public string GroupName { get; set; } } 

And write the code below to create / get a group of colors:

 //Generating Color Group Table List<ColorTable> MyColorTable = new List<ColorTable>(); MyColorTable.Add(new ColorTable { GroupName = "Gray", ColorName = Color.Gray }); MyColorTable.Add(new ColorTable { GroupName = "Gray", ColorName = Color.LightGray }); MyColorTable.Add(new ColorTable { GroupName = "Green", ColorName = Color.Green }); MyColorTable.Add(new ColorTable { GroupName = "Green", ColorName = Color.LightGreen }); //Getting required Color Group, say "Gray" List<ColorTable> GreenColor = MyColorTable.Where(c => c.GroupName == "Gray").ToList(); 
-1
source

Source: https://habr.com/ru/post/903518/


All Articles