27

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)

Community
  • 1
  • 1
Rabarberski
  • 23,854
  • 21
  • 74
  • 96

6 Answers6

39

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
  • 137,716
  • 26
  • 137
  • 190
32

If you have the greyscale value in the range 0..255 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
  • 50,101
  • 39
  • 117
  • 168
  • 7
    +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 Aug 13 '09 at 15:25
  • 1
    Or `rgb = grey << 16 | grey << 8 | grey`, where `<<` is the signed left shift operator, and `|` the bitwise inclusive OR. – sp00m May 13 '15 at 15:47
  • It gives value like 14474460 for gray value 220. How can we get R,G & B value from this ? – Sagar Gautam Feb 28 '20 at 04:38
  • 1
    14474460 is the RGB value. 220 is 0xDC, and 14474460 is 0xDCDCDC. – izb Mar 03 '20 at 14:48
13

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.

7

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
  • 101
  • 2
  • 4
    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. –  May 07 '09 at 17:52
  • 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 May 15 '09 at 20:12
  • 1
    Erik, it doesn't, but "gray scale image" differs quite a lot from "black and white image" (which is only two colours). – Aistina Aug 13 '09 at 13:05
  • 10
    Note: "black and white" means "greyscale" in colloquial usage – Stefan Monov May 27 '10 at 09:50
4

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
  • 10,491
  • 1
  • 35
  • 58
  • 3
    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. –  May 07 '09 at 16:50
0

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.

Community
  • 1
  • 1
bkausbk
  • 2,740
  • 1
  • 36
  • 52