Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert grayscale value to RGB representation?

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)

like image 587
Rabarberski Avatar asked May 07 '09 16:05

Rabarberski


People also ask

What color is 255 in grayscale?

For a grayscale images, the pixel value is a single number that represents the brightness of the pixel. The most common pixel format is the byte image, where this number is stored as an 8-bit integer giving a range of possible values from 0 to 255. Typically zero is taken to be black, and 255 is taken to be white.

How do I convert an image to RGB?

To convert an image to RGB color mode in Photoshop, open the image in Photoshop and go to Image > Mode > RGB Color. This will convert the image from its current color mode to RGB. If the image is already in RGB color mode, this menu option will be greyed out.


2 Answers

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.

like image 67
Ates Goral Avatar answered Oct 18 '22 04:10

Ates Goral


If you have the greyscale value in a 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.

like image 35
izb Avatar answered Oct 18 '22 05:10

izb