Question

How can I convert a grayscale value (0-255) to an RGB value/representation? It is for using in an SVG image, which doesn't seem to come with a grayscale support, only RGB...

Note: this is not RGB -> grayscale, which is already answered in another question, e.g. Converting RGB to grayscale/intensity)

Was it helpful?

Solution

The quick and dirty approach is to repeat the grayscale intensity for each component of RGB. So, if you have grayscale 120, it translates to RGB (120, 120, 120).

This is quick and dirty because the effective luminance you get depends on the actual luminance of the R, G and B subpixels of the device that you're using.

OTHER TIPS

If you have the greyscale value in a variable and want to produce a new value in the form 0x00RRGGBB, then a quick way to do this is:

int rgb = grey * 0x00010101;

or equivalent in your chosen language.

Conversion of a grayscale to RGB is simple. Simply use R = G = B = gray value. The basic idea is that color (as viewed on a monitor in terms of RGB) is an additive system.

http://en.wikipedia.org/wiki/Additive_color

Thus adding red to green yields yellow. Add in some blue to that mix in equal amounts, and you get a neutral color. Full on [red, green, blue] = [255 255 255] yields white. [0,0,0] yields monitor black. Intermediate values, when R=G=B are all equal will yield nominally neutral colors of the given level of gray.

A minor problem is depending on how you view the color, it may not be perfectly neutral. This will depend on how your monitor (or printer) is calibrated. There are interesting depths of color science we could go into from this point. I'll stop here.

Grey-scale means that all values have the same intensity. Set all channels (in RGB) equal to the the grey value and you will have the an RGB black and white image.

Woudln't setting R,G,and B to the same value (the greyscale value) for each pixel get you a correct shade of gray?

You may also take a look at my solution Faster assembly optimized way to convert RGB8 image to RGB32 image. Gray channel is simply repeated in all other channels.

The purpose was to find the fasted possible solution for conversion using x86/SSE.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top