Assistance with RGB to HSL Conversion in Python or C#

Hello everyone,

I am currently working on a project where I need to convert colors from the RGB format to HSL or HSV, and have 3 input sliders between “0 To 1” to change the hue, saturation and lightness relatively.

I have attempted to write the conversion code in both Python and C# as, but I’m facing some challenges and my methods aren’t working as expected.

In Python, I tried using basic mathematical formulas for the conversion, but the results seem off. In C#, I attempted to use the built-in methods from the System.Drawing namespace, but even the most somple results don’t match.

 // Retrieve HSL values from the RGB color
    hue = rgbColor.GetHue(); // Hue in degrees (0-360)
    saturation = rgbColor.GetSaturation(); // Saturation (0-1)
    lightness = rgbColor.GetBrightness(); // Lightness (0-1)

RGB_to_HSL.gh (4.6 KB)

I would really appreciate it if anyone could share a snippet or point me towards a reliable method/library that can achieve this accurately.

Thank you in advance for your help!

You can use the ColorHSL.CreateFromRGBA method:

Or this constructor:

Depending on your RGB color type.

1 Like