How to Convert RGB to Grayscale
On a computer monitor, cell phone screen, or digital camera screen, different colors are displayed by varying the amounts of red, green, and blue light that shines through the pixels. This is known as the RGB color model. Pure red, green, and blue look like this:
The RGB encoding of pure red is (255,0,0), pure green (0,255,0), and pure blue (0,0,255). In all RGB encodings, the first value is the amount of red, the second value is the amount of green, and the last value represents the amount of blue. The range of the three numbers is 0 to 255.
Grayscale images are rendered in black, white, and all the shades of gray in between. The RGB encoding of any gray values is a set of three equal numbers, i.e., (x, x, x), where x is some integer between 0 and 255. For instance, white is (255,255,255), black is (0,0,0) and medium gray is (127,127,127). The higher the numbers, the lighter the gray.
To convert a non-neutral color to its equivalent grayscale value, you must compute a weighted average of the red, green, and blue values.
The RGB to Grayscale EquationSuppose the RGB value of a color is (r, g, b), where r, g, and b are integers between 0 and 255. The grayscale weighted average, x, is given by the formula
x = 0.299r + 0.587g + 0.114b.
Notice that the colors are not weighted equally. Since pure green is lighter than pure red and pure blue, it has a higher weight. Pure blue is the darkest of the three, so it receives the least weight.
Computation Example: A shade of dark purple has an RGB value of (100, 0, 150). The weighted average is
x = 0.299(100) + 0.587(0) + 0.114(150),
which simplifies to x = 47. (If the weighted average doesn't come out to a whole number, round it to the nearest whole number.) So the equivalent gray is (47, 47, 47)
Visual Examples: The chart below shows colors on the left and equiavalent grays on the right, along with the RGB values.
|Pure Red (255,0,0)||Equivalent Gray (76,76,76)|
|Pure Green (0,255,0)||Equivalent Gray (150,150,150)|
|Pure Blue (0,0,255)||Equivalent Gray (29,29,29)|
|Cyan (0,255,255)||Equivalent Gray (179,179,179)|
|Magenta (255,0,255)||Equivalent Gray (105,105,105)|
|Yellow (255,255,0)||Equivalent Gray (226,226,226)|
|Brown (158,85,54)||Equivalent Gray (103,103,103)|
|Olive (155,160,52)||Equivalent Gray (146,146,146)|
|Purple (100,0,150)||Equivalent Gray (47,47,47)|
Can you convert a grayscale value back to an RGB color code?Because many different colors can have the same grayscale equivalent, it is not possible to match a unique color to each gray value. For example, the colors below all have the same grayscale equivalent value of (100, 100, 100):
© Had2Know 2010