示例#1
0
        public static IRGB hsv2rgb(float h, float s, float v)
        {
            s = s / 100f;
            v = v / 100f;

            float[] rgb;

            float c  = v * s;
            float hh = h / 60;
            float x  = c * (1 - Math.Abs((hh % 2) - 1));
            float m  = v - c;

            switch (Math.Floor(hh))
            {
            case 0:
                rgb = new[] { c, x, 0 };
                break;

            case 1:
                rgb = new[] { x, c, 0 };
                break;

            case 2:
                rgb = new[] { 0, c, x };
                break;

            case 3:
                rgb = new[] { 0, x, c };
                break;

            case 4:
                rgb = new[] { x, 0, c };
                break;

            case 5:
                rgb = new[] { c, 0, x };
                break;

            default:
                throw new NotSupportedException();
            }

            return(new Color
            {
                r = (int)Shades.Round(MAX_COLOR_RGBA * (rgb[0] + m)),
                g = (int)Shades.Round(MAX_COLOR_RGBA * (rgb[1] + m)),
                b = (int)Shades.Round(MAX_COLOR_RGBA * (rgb[2] + m))
            });
        }
示例#2
0
        public static IHSV rgb2hsv(int r, int g, int b)
        {
            float h = float.NaN;
            float s;
            float v;
            float max   = Math.Max(Math.Max(r, g), b);
            float min   = Math.Min(Math.Min(r, g), b);
            float delta = max - min;

            // hue
            if (delta == 0)
            {
                h = 0;
            }
            else if (r == max)
            {
                h = ((g - b) / delta) % 6;
            }
            else if (g == max)
            {
                h = (b - r) / delta + 2;
            }
            else if (b == max)
            {
                h = (r - g) / delta + 4;
            }

            h = Shades.Round(h * 60);

            if (h < 0)
            {
                h += 360;
            }

            // saturation
            s = Shades.Round((max == 0 ? 0 : delta / max) * 100);

            // value
            v = Shades.Round((max / 255) * 100);

            return(new Color
            {
                h = h,
                s = s,
                v = v
            });
        }