Convert grayscale value to RGB representation?

102,789

Solution 1

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.

Solution 2

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.

Solution 3

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.

Solution 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?

Share:
102,789
Rabarberski
Author by

Rabarberski

Updated on April 08, 2020

Comments

  • Rabarberski
    Rabarberski about 4 years

    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)