private void Update()
        {
            var h = this.hSlider.Value;
            var s = this.sSlider.Value / 100.0;
            var v = this.vSlider.Value / 100.0;

            var hsv = HsvColor.FromHsv(h, s, v);
            var c   = hsv.ToRgb();

            var l = Luminosity.FromRgb(c);
            var w = l <= 128;

            this.colorbox.Background = new SolidColorBrush(c);
            this.colorbox.Foreground = w ? Brushes.White : Brushes.Black;
            this.colorbox.Text       = $"Color: {c}, Luminosity: {l}";
        }
示例#2
0
        public static HsvColor RgbToHsv(Color c)
        {
            var    max = Math.Max(Math.Max(c.R, c.G), c.B);
            var    min = Math.Min(Math.Min(c.R, c.G), c.B);
            double h, s, v;

            if (max == min)
            {
                h = 0;
            }
            else if (max == c.R)
            {
                h = (60 * (c.G - c.B) / ((double)max - min) + 360) % 360;
            }
            else if (max == c.G)
            {
                h = (60 * (c.B - c.R) / ((double)max - min)) + 120;
            }
            else if (max == c.B)
            {
                h = (60 * (c.R - c.G) / ((double)max - min)) + 240;
            }
            else
            {
                throw new ArgumentException();
            }

            if (max == 0)
            {
                s = 0;
            }
            else
            {
                s = ((max - min) / (double)max);
            }
            v = max / 255.0;
            return(HsvColor.FromHsv(h, s, v));
        }
示例#3
0
        public static Color Lighter(Color c)
        {
            var hsv = RgbToHsv(c);

            return(HsvToRgb(HsvColor.FromHsv(hsv.H, hsv.S, Math.Min(1.0, hsv.V * 1.2))));
        }
示例#4
0
        public static Color Darker(Color c)
        {
            var hsv = RgbToHsv(c);

            return(HsvToRgb(HsvColor.FromHsv(hsv.H, hsv.S, hsv.V * 0.8)));
        }