3
votes

I'm able to capture the RGB values of colors from a photo, but I want to be able to programmatically detect if that color is a value of Red, Orange, Yellow, Green, Blue, Purple, Tan or White or Black.

So I would need to specify RGB ranges that would return, for example, a value of red... or blue... and so on. I've got something like this currently, but it doesn't include all the possible RGB varieties.

RED

R = 255, G <= 102, B = 0

R = 255, G = 0, B <= 150

ORANGE & YELLOW

R = 255, G >= 108 & <= 252, B = 0

R = 240, G = 255, B = 0

and so on...

Does anyone know how to take ANY RGB value and detect if it's a value of Red, Orange, Yellow, Green, Blue, Purple, Tan, White or Black?

3
Beware, the boundaries aren't hard and fast. In particular people will get into arguments over yellow vs. orange. - Mark Ransom
Looks like you want something to calculate the hue. Wikipedia has a decent article on hue including various formulae that might get you on your way. See hue - user1864610
@MikeW hue is a good start, but it doesn't help when trying to classify e.g. tan. - Mark Ransom
@MarkRansom - thanks. I'm aware of the differences of opinions on color. Mike W - thanks, I'll check that link. So far, most answers I've come across recommend converting to HSL, so I'll look into that. - mapk
@mapk that's good, but remember too that this question and any answers will live on long after you've gotten the answer you're looking for and left. The next person finding this via Google may not be so wise. - Mark Ransom

3 Answers

1
votes

Calculating visual similarity of colors from RGB values is difficult because of the way human perception works. A popular method is CIE 2000. There are some libraries implementing it available, for example python-colormath. You could then simply compare the color distance to each of your colors (you can find reasonable RGB values for common color names here) and choose the one with the smallest distance.

0
votes

I think this classification is dependent on the person. For instance for a lot of my male friends(myself among them) there are only a few colors(red, blue, green, yellow), while a lot of my female friends have at least 20 words for pink. Even if we ignore that and we decide to stick to the smaller number of values we often quarrel if something is red or brown or if something is purple or pink. If humans can't give a definite answer than there is no way you can teach a computer to do that.

That being said your problem is in fact a clustering/classification algorithm and there are many algorithms to solve it. I would recommend using a neural network of some kind(for instance use backpropagation with 3 input neurons - for red, green and blue). Still you will need a lot of examples - i.e. provide colors and their classification(this color is blue etc.). Even if you decide to use another algorithm you will still need to provide an educational set - set of examples that you've already classified and the more examples this set contains the better the classification will be.

0
votes

That's hard to decide, even for humans. I would just see the rgb value as 3d color room. Just give in some colors (red: 255, 0, 0, ...) and say that everything in the euclidean distance of x is the same color.

distance(red, color) = sqrt((red.r - color.r)^2 + (red.g - color.g)^2 + (red.b - color.b)^2)

Or just measure all distances and get the shortest as color.