سؤال

Basically I need to know the valid range for H, L, and S in the win32 functions like ColorHLSToRGB(H, L, S).


edit: The basic premise of this question is wrong. Inverting in this way does not always give a contrasting color especially when the color to be contrasted against is near gray. I ended up using black or white as my contrast color. It would still be nice to have this answered and know what HLSMAX is by default though.


I'm trying to get a contrasting color for text given a background color. To that end, I've written the following code but I'm not sure what the range of values for H, L, and S are. 240 seems to be the number given on a few sites (see references I've used below and/or google HLSMAX for yourself) but that's so low that precision will be lost. Doesn't seem right... Not to mention I'm not getting a contrasting color. Color looks the same.

Questionable part is value of HLSMAX at first line in contrastColor

        [DllImport("shlwapi.dll")]
        static extern void ColorRGBToHLS(int RGB, ref int H, ref int L, ref int S);

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

        [StructLayout(LayoutKind.Sequential)]
        public struct RGB
        {
            public byte r, g, b;
            public RGB(Color colorIn)
            {
                r = colorIn.R;
                g = colorIn.G;
                b = colorIn.B;
            }
            public Int32 ToInt32()
            { return BitConverter.ToInt32(new byte[4] {r, g, b, 0},0); }
            public Color ToColor()
            { return Color.FromRgb(r, g, b); }
        }

        private Color contrastColor(Color color)
        {
            int HLSMAX=240;//Is this right??? It seems too low!
            RGB rgb = new RGB(color);
            int h=0, l=0, s=0;
            ColorRGBToHLS(rgb.ToInt32(), ref h, ref l, ref s);
            h=(h+(HLSMAX/2))%HLSMAX;
            l = HLSMAX - l;
            rgb = ColorHLSToRGB(h, l, s);
            return Color.FromArgb(color.A, rgb.r, rgb.g, rgb.b);
        }

References used:

هل كانت مفيدة؟

المحلول

Microsoft's website doesn't seem to include the values. However, the WINE API documentation states the values are 0-240.

https://source.winehq.org/WineAPI/ColorRGBToHLS.html

نصائح أخرى

Rich Newman has created an HSLColor Class. You might find using this is better then using Win32 functions.

To answer your original question, Rich Newman's class seems to use 240 as the value you are seeking.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top