Linear RGB to / from HSL

Does anyone know how to get HSL from linear RGB color (and not sRGB color)? I have seen many sRGB ↔ HSL conversions, but nothing for linearRGB ↔ HSL. Not sure if this is a fundamental transformation with slight changes, but I would appreciate any understanding that anyone might have about this.

Linear RGB does not match the linearization of sRGB (which takes [0.255] and makes it [0.1]). The linear conversion of RGB from / to sRGB is at http://en.wikipedia.org/wiki/SRGB . In VBA, this will be expressed (assuming linearized sRGB values ​​[0,1]):

Public Function sRGB_to_linearRGB(value As Double) If value < 0# Then sRGB_to_linearRGB = 0# Exit Function End If If value <= 0.04045 Then sRGB_to_linearRGB = value / 12.92 Exit Function End If If value <= 1# Then sRGB_to_linearRGB = ((value + 0.055) / 1.055) ^ 2.4 Exit Function End If sRGB_to_linearRGB = 1# End Function Public Function linearRGB_to_sRGB(value As Double) If value < 0# Then linearRGB_to_sRGB = 0# Exit Function End If If value <= 0.0031308 Then linearRGB_to_sRGB = value * 12.92 Exit Function End If If value < 1# Then linearRGB_to_sRGB = 1.055 * (value ^ (1# / 2.4)) - 0.055 Exit Function End If linearRGB_to_sRGB = 1# End Function 

I tried to send values ​​to the standard RGB_to_HSL routines in Linear RGB and return from HSL_to_RGB, but it does not work. Maybe because the current HSL ↔ RGB algorithm takes gamma correction into account and linear RGB does not correct gamma - I don’t know for sure. I hardly saw any links that this can be done, with the exception of two:

I intend to:

  • send from sRGB (e.g. FF99FF ( R=255, G=153, B=255 )) to Linear RGB ( R=1.0, G=0.318546778125092, B=1.0 )
    • using the code above (for example, G = 153 will be obtained in linear RGB from sRGB_to_linearRGB(153 / 255) )
  • in HSL
  • change / modulate saturation 350%
  • send back from HSL-> Linear RGB-> sRGB, the result will be FF19FF ( R=255, G=25, B=255 ).

Using available functions from .NET, such as .getHue from System.Drawing.Color , does not work in any sRGB space, except for 100% modulation of any HSL value, therefore, it is necessary to send linear RGB instead of sRGB.

+4
source share
4 answers

It doesn't make sense to convert to linear RGB, since HSL is defined in terms of gamma encoded values. Instead, write your own function, convert sRGB to HSL, modulate the saturation with these values ​​(allowing saturation values ​​to be out of range), and then convert back to sRGB, clamp intensities that fall outside the sRGB range (or inhibit saturation changes that cannot be encoded in sRGB).

+4
source

The System.Windows.Media.Color class allows you to get or set scRGB through ScA, ScR, ScG, ScB or RGB through A, R, G, B.

So you can convert RGB to HSL, manipulate it, and then convert it back to RGB and save it in a Color instance. You can then read the converted scRGB properties.

Not perfect, and may require some loss of information. But this is an option!

+1
source

Based on your comment here , your problem is not doing the wrong conversion; it is that you perform sequential conversions over quantized values. In other words, you take the result sRGB => RGB => HSL => HSL => RGB => sRGB and use this for subsequent color operations. The easiest way to maintain accuracy is to always maintain the original RGB value and accumulate changes in the HSL space that you want to apply. That way, you always apply HSL space operations to the original color, and you don’t have to worry about repeatedly processing the quantized values.

+1
source

Does this help? There are a lot of interesting links in this question, perhaps something that works in your case too ...

+1
source

All Articles