views:

5684

answers:

5

Hello,

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)

+5  A: 

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.

Erik
Well, no. Actually you will NOT have a black and white image. You will have an image composed of various shades of gray.At a lower level, the actual pixels will be composed of red, green and blue dots, at levels of intensity such that the person viewing this from a distance will see a neutral image. Take an eye loop and look at nominally gray pixels on your monitor. You will see a composite of red, green and blue dots.
woodchips
You are correct that the rendred image on the screen will be composed of all RGB looking at the maginification. So will all colors be represented on a screen. I have a hard time understanding how "You will have an image composed of various shades of gray" differs from a gray scale image.
Erik
Erik, it doesn't, but "gray scale image" differs quite a lot from "black and white image" (which is only two colours).
Aistina
Note: "black and white" means "greyscale" in colloquial usage
Stefan Monov
+2  A: 

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

jlew
Not exactly. It depends on how you will view the color. How is your monitor or printer calibrated? What viewing environment will you see the color in? IF the color will be printed, how is your printer calibrated, characterized? What settings have been employed for the printer and any ICC profiles? White points? What illuminant is used to view the print? Sunlight? D50 simulator bulb? Tungsten lamp?To a reasonable degree of approximation, R = G = B = gray value is a good start though. It will get you close.
woodchips
+7  A: 

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.

Ates Goral
+4  A: 

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.

woodchips
+5  A: 

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.

izb
+1 for straightforward multiplication. The instinct is to dive into bit shifts and bitwise ORs, but this is a lot simpler and just works.
Ates Goral