Creating a C# Color from HSL values

后端 未结 2 1845
逝去的感伤
逝去的感伤 2021-01-01 14:15

How can I create a C# Color from HSL values?

2条回答
  •  执笔经年
    2021-01-01 14:59

    Add the following line below to the "using" namespaces in your code file:

    using System.Runtime.InteropServices
    

    and with P/Invoke, add the following function:

    [DllImport("shlwapi.dll")]
    public static extern int ColorHLSToRGB(int H, int L, int S);
    

    In any class you want in your project.

    If you have the color in hue, luminance and saturation as integers, then give these values to that function, and the desired RGB color will be returned as integer. Then use ColorTranslator.FromWin32 to translate the returned rgb color as integer to System.Drawing.Color structure.

    Another way is to use Color.FromArgb(Int32) overload method, to create the color you want as System.Drawing.Color structure from that integer.

    Another way is to create an instance of ColorConverter class, and then call ConvertFrom method, and give it the RGB Color integer returned from the ColorHLSToRGB function, or call ConvertTo method with the same first argument, but in the second argument input typeof(System.Drawing.Color). Then convert the returned object to System.Drawing.Color structure type.

    The result is exactly what you are seeking and expected!

    You also can try other algorithms or ways that you can search, find out and learn on the internet, about how to convert RGB color as integer to System.Drawing.Color structure OR how to convert HLS values as three integers to System.Drawing.Color structure!

提交回复
热议问题