# Using HSV colour space to compute luminance of an RGB value

This topic is 4633 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Courtesy of the stuff in my journal today, I've been thinking a bit about colour spaces and their properties. My other project is an update of the 'HDR Demo' sample I released a few months back, and I got thinking that I could improve the luminance calculations that the shaders employ. Currently I do a simple inner product on the RGB with a suitable bias to get a greyscale value. It does the job, but it's not particularly scientific. The problem occurs when you have a single channel that is very bright - say RGB(15.0, 0.0, 0.0) - the overall average is brought down by the two very low values. I'm wondering if the Value component of HSV or the Lightness component of HSL would do a better job? Has anyone seen such an implementation, or care to comment on it as a potential idea? Looking at the mathematics on the linked pages, I'm not sure if the respective components will measure luminance any better than the current one [oh] Cheers, Jack

##### Share on other sites
How about doing a simple vector magnetude? Since you don't actually care about the specific colour, it should work fine...

##### Share on other sites
Quote:
 Original post by python_regiousHow about doing a simple vector magnetude? Since you don't actually care about the specific colour, it should work fine...

That sounds to simple, but it'd probably work pretty well [smile]

Hadn't thought about interpretting the values as vectors - was too hung up on them being *colours* rather than just tuples/numbers [headshake]

I think I'd best go run some tests with these 4 metrics and see which one generates the best results...

Cheers,
Jack

##### Share on other sites
You can just take max value of R, G and B. It works pretty well in practice.

##### Share on other sites
Quote:
 Original post by jollyjeffersI'm wondering if the Value component of HSV or the Lightness component of HSL would do a better job?

I was visiting Paul Bourke's web again, and I found this on the RGB to HSV page
Quote:
 Value is similar to Luminance except it also varies the colour saturation.

As a consequence I guess that you should use L rather than V. You can see the difference between HSV and HSL on Bourke's page.

The main problem in your application is your range - I can't see what a luminance > 1 would represent in our physical world. Would it be whiter than white? [smile]

##### Share on other sites
Quote:
 Original post by Emmanuel DelogetThe main problem in your application is your range - I can't see what a luminance > 1 would represent in our physical world. Would it be whiter than white? [smile]

That's called HDR ;)

##### Share on other sites
Quote:
 Original post by jollyjeffersI think I'd best go run some tests with these 4 metrics and see which one generates the best results...

As much for reference as anything else... I've implemented the 4 metrics and they look like so:

Simple Average: GreyValue = dot( color.rgb, float3( 0.33f, 0.33f, 0.33f ) )

Weighted Average: GreyValue = dot( color.rgb, float3( 0.299f, 0.587f, 0.114f ) )

Maximum Value: GreyValue = max( color.r, max( color.g, color.b ) )

Luminance according to HSL colour space: GreyValue = 0.5f * ( max( color.r, max( color.g, color.b ) ) + min( color.r, min( color.g, color.b ) ) )

Can't say any of them are exactly what I wanted, but then again the scene isn't exactly the best for HDR rendering [headshake]

Cheers,
Jack

##### Share on other sites
Did you try the simple magnetude one? Would be interesting to just see what that looks like...

##### Share on other sites
Quote:
 Original post by python_regiousDid you try the simple magnetude one? Would be interesting to just see what that looks like...

Yeah, seems I forgot that one [headshake]

I was expecting it to be about the same as the max() one, but it seems to look a little better/different to me [smile]

Cheers,
Jack

##### Share on other sites
Pure R, G or B is to artificial to look good IMO.
In real life it's only lasers that are even close to pure R, G, B.
The problem is that if a red tap in your eye get's oversaturated it's "bleeds" it's energy over to the G and B taps.
In real life if something is "red enough" it starts to turn white, this is something that is hard to simulate using CG.
I even remeber a very unscientific experiment on some mailing list (or here?), where a gut actually looked directly into a red laser!
He claimed to se a white dot with a red halo.

Another classic in CG games is to have a pure red suface (1, 0, 0) lit be a very strong pure green light (0, 1, 0), what's the resulting color?
Black! In real life it's not black, why?
There are NO pure red surfaces and almost no pure green lights.

It would be cool to se a HDR implementation that tried to mimic the way energy bleeding occurs in the eye between the taps.

PS.
I do NOT recommend any of you to conduct the same experiment!
If you don't understand why, get back to grammer school and start all over.

1. 1
2. 2
3. 3
Rutin
22
4. 4
JoeJ
16
5. 5

• 14
• 29
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631774
• Total Posts
3002288
×