Converting RGB to grey scale

Started by
8 comments, last by QuadMV 18 years, 9 months ago
Is there a function/macro that will take either RGB components, or a D3DCOLOR, and convert it to a greyscale? I'm probably expecting too much, but there seems to be everything else out there. If not, any ideas on what the right technique to do so would be? I have a color texture, but I want to render the texture as a greyscale Thanks much
3DMUVE is an amateur game development team, and the designer and developer of a new gaming technology “MUVE” for the gaming industry.
Advertisement
You probably want to just do it in your pixel shader:

float4 main(float2 tc : TEXCOORD0) : COLOR
{
float3 color = tex2D(s, tc);
float grey = dot(color, float3(0.212671f, 0.715160f, 0.072169f));
return float4(grey, grey, grey, 1.0f);
}

xyzzy
Well if white is (255, 255, 255) and black is (0, 0, 0) maybe it's as simple as just adding up the RGB values and dividing by 255 * 3
See section C-9 of the Colorspace FAQ if you are wondering where I got those magic numbers.

xyzzy
Greyscales are just 'colors' with r = g = b.
So there are several ways of calculating the value you are looking for.

1. Quick 'n' dirty: average r, g and b:
gs = (r + g + b) / 3
newColor = RGB(gs, gs, gs)

2. Use HSL as an inbetween. HSL is, like RGB, a way of repesentating colors but not using red, blue and green.
From the PaintShopPro help:
Quote:Hue The color reflected from an object, such as red, yellow, or orange. Each hue value is assigned based on its position on the color wheel. On the Jasc Color Picker’s Color wheel, colors are assigned counter-clockwise from the top. Red is at the top (value 0) and as you move around the wheel the colors go through orange, yellow, green, blue, purple, and back to red.

Saturation The purity or vividness of the color. Saturation represents the amount of grey in the color, from 0 (entirely grey) to 255 (fully saturated color).

Lightness The perceived amount or intensity of light in the color. Lightness ranges from 0 (no light, or black) to 255 (total lightness, or white). At 50 percent lightness, or a value of 128, a color is considered pure. For example, pure red has a hue of 255, a saturation of 255 (100 percent) and a lightness of 128 (50 percent). For pure blue, the hue is 170, saturation is 255 and lightness is 128.



As you can see lightness is the one you want for your grayscale.

To calulate Lightness:

In .NET
In C

Cheers
If you are trying to model how humans percieve brightness, '(R+G+B)/3' is really not the best solution. From colorspace faq:


C-9 WHAT WEIGHTING OF RED, GREEN AND BLUE CORRESPONDS TO BRIGHTNESS?

Direct acquisition of luminance requires use of a very specific spectral
weighting. However, luminance can also be computed as a weighted sum of
red, green and blue components.

If three sources appear red, green and blue, and have the same radiance in
the visible spectrum, then the green will appear the brightest of the three
because the luminous efficiency function peaks in the green region of the
spectrum. The red will appear less bright, and the blue will be the darkest
of the three. As a consequence of the luminous efficiency function, all
saturated blue colors are quite dark and all saturated yellows are quite
light. If luminance is computed from red, green and blue, the coefficients
will be a function of the particular red, green and blue spectral weighting
functions employed, but the green coefficient will be quite large, the red
will have an intermediate value, and the blue coefficient will be the
smallest of the three.

Contemporary CRT phosphors are standardized in Rec. 709 [8], to be
described in section 17. The weights to compute true CIE luminance from
linear red, green and blue (indicated without prime symbols), for the Rec.
709, are these:

Y = 0.212671 * R + 0.715160 * G + 0.072169 * B;

This computation assumes that the luminance spectral weighting can be
formed as a linear combination of the scanner curves, and assumes that the
component signals represent linear-light. Either or both of these
conditions can be relaxed to some extent depending on the application.

Some computer systems have computed brightness using (R+G+B)/3. This is at
odds with the properties of human vision, as will be discussed under What
are HSB and HLS? in section 36.

The coefficients 0.299, 0.587 and 0.114 properly computed luminance for
monitors having phosphors that were contemporary at the introduction of
NTSC television in 1953. They are still appropriate for computing video
luma to be discussed below in section 11. However, these coefficients do
not accurately compute luminance for contemporary monitors.
Thanks for all the suggestions.

The averaging seems to have worked, expect for one small tidbit, the color isn’t necessarily shades of grey. It’s correctly showing me shades, but in my case the color seems to be lavender. Any tricks to making it grey?


3DMUVE is an amateur game development team, and the designer and developer of a new gaming technology “MUVE” for the gaming industry.
XYZZY,

Can you explain what this is doing for me?

and, what is the "s" in the tex2D call?

-------------------------

You probably want to just do it in your pixel shader:

float4 main(float2 tc : TEXCOORD0) : COLOR
{
float3 color = tex2D(s, tc);
float grey = dot(color, float3(0.212671f, 0.715160f, 0.072169f));
return float4(grey, grey, grey, 1.0f);
}


3DMUVE is an amateur game development team, and the designer and developer of a new gaming technology “MUVE” for the gaming industry.
I like these constants more:
inline DWORD toGrayscale ( DWORD color ){   float r = (float) ( ( color >> 16 ) & 0xff );   float g = (float) ( ( color >> 8 ) & 0xff );   float b = (float) ( color & 0xff );   r *= 0.299f;   g *= 0.587f;   b *= 0.114f;   int sum = (int) ( ( r + g + b ) * 0.8f ) & 0xff;   return ( 0xff000000 | sum | ( sum << 8 ) | ( sum << 16 ) );}


>prev. post: it is a pixel shader in HLSL. s is a sampler. it is doing nothing to you
I'm good now. Thanks everyone.

I didn't realize I had the code right all along, but I was adjusting the color by a delta, and I needed to back the delta out before converting it to a grey scale, so now things are working great.

Thanks all, I'll be posting screen shots soon.

3DMUVE is an amateur game development team, and the designer and developer of a new gaming technology “MUVE” for the gaming industry.

This topic is closed to new replies.

Advertisement