linearRGB conversion to/from HSL

Posted by Otaku on Stack Overflow See other posts from Stack Overflow or by Otaku
Published on 2009-09-28T07:13:09Z Indexed on 2010/04/15 20:23 UTC
Read the original article Hit count: 284

Filed under:
|
|
|

Does anyone know of a way to get HSL from an linearRGB color (not an sRGB color)? I've seen a lot of sRGB<->HSL conversions, but nothing for linearRGB<->HSL. Not sure if it is fundementally the same conversion with minor tweaks, but I'd appreciate any insight someone may have on this.

Linear RGB is not the same as linearizing sRGB (which is taking [0,255] and making it [0,1]). Linear RGB transformation from/to sRGB is at http://en.wikipedia.org/wiki/SRGB. In VBA, this would be expressed (taking in 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 have tried sending in Linear RGB values to standard RGB_to_HSL routines and back out from HSL_to_RGB, but it does not work. I have seen almost no references that this can be done, except for two:

  1. A reference on http://en.wikipedia.org/wiki/HSL_and_HSV#cite_note-9 (numbered item 10).
  2. A reference on an open source project Grafx2 @ http://code.google.com/p/grafx2/issues/detail?id=63#c22 in which the contributor states that he has done Linear RGB <-> HSL conversion and provides some C code in an attachment to his comment in a .diff file (which I can't really read :( )

My intent is to send from sRGB (for example, 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, the G=153 would be obtained in Linear RGB from sRGB_to_linearRGB(153 / 255)) to HSL, modify the Saturation by 350% and going back from HSL->Linear RGB->sRGB, the result would be FF19FF (R=255, G=25, B=255).

Using available functions from .NET, such as .getHue from a System.Drawing.Color does not work in any sRGB space above 100% modulation of any HSL value, hence the need for Linear RGB to be sent in instead of sRGB.

© Stack Overflow or respective owner

Related posts about hsl

Related posts about color