3
votes

How do I programmatically determine whether or not a given pixel is yellow? Or red? Or another color?

Red is usually (255,0,0) in the RGB space, but we also know that (230,0,0) will also appear red.

Is there any standard which splits the RGB space into, say, the eight major colors - red, green, blue, yellow, magenta, cyan, black, and white?

2

2 Answers

4
votes

Determining that in RGB can be complicated, but you can convert a RGB value into HSV, a format that represents colors by Hue, Saturation and Value. This is the system used to pick specific colors in software like Photoshop.

In this system all colors of the spectrum are represented in a single floating pointer number (Hue) that ranges from 0 to 360 degrees, the other 2 numbers only modify this color (tell if it's grayer or brighter).

enter image description here

This makes it easy to compare colors, as you can see on this image every primary color in both the RGB and CMYK systems are exactly 60 degrees apart.

Here you can easily tell that anything between 30 and 90 is a tone of yellow, anything between 330 (or -30) and 30 is a tone of red and so on.

You can even be more specific and determine ranges for tones of orange, purple, and other colors that are not primary.

About black and white, you can determine if a color appears black by checking if it's Value is low, say less than 0.1 for instance (it ranges from 0 to 1), and if it's white if you find the combination of high Value (bigger than 0.9 for instance) and low Saturation (lower than 0.1 for instance). There are no exact numbers of course, so you will have to decide for yourself when a very dark or very bright color becomes black or white.

0
votes

In the RGB color model you have three channels reg, green and blue with values ranging from 0 - 255 (you knew that!).

To cover the whole spectrum you combine the three channels with a certain amount of value options.

So if you have x amount value options for each channel, you'll get x * x * x = x^3 color values.

For two options e.g. 0 and 255 you get 2^3 = 8 colors.

For six options, which constitute five (= 6 - 1) segments, you have the values 0 * (255 / 5), 1 * (255 / 5), 2 * (255 / 5), 3 * (255 / 5), 4 * (255 / 5), 5 * (255 / 5) which makes 6^3 = 216 colors, which you can see as an example here.

The range above showed you how to build your one dimensional array for one channel. Since the three channels are the same, you can use it for all three channels.

With this you build your two-dimensional array:

0, 0, 0

51, 0, 0

102, 0, 0

enter image description here