Exemplo n.º 1
0
        private void SetHSV(ColorHSV hsv)
        {
            if (!(bool)_spinButtonH.Tag)
            {
                _spinButtonH.Value = hsv.H;
            }
            if (!(bool)_sliderH.Tag)
            {
                _sliderH.Value = hsv.H;
            }

            if (!(bool)_spinButtonS.Tag)
            {
                _spinButtonS.Value = hsv.S;
            }
            if (!(bool)_sliderS.Tag)
            {
                _sliderS.Value = hsv.S;
            }

            if (!(bool)_spinButtonV.Tag)
            {
                _spinButtonV.Value = hsv.V;
            }
            if (!(bool)_sliderV.Tag)
            {
                _sliderV.Value = hsv.V;
            }
        }
Exemplo n.º 2
0
        private void OnColorChanged(ColorHSV h)
        {
            var c = h.ToRGB();

            c.A = 255;
            OnColorChanged(c, h);
        }
Exemplo n.º 3
0
        private void SubscribeHSV(SpinButton spinButton, Slider slider)
        {
            spinButton.Tag = false;
            slider.Tag     = false;

            spinButton.ValueChangedByUser += (s, a) =>
            {
                if (spinButton.Value == null)
                {
                    return;
                }

                try
                {
                    spinButton.Tag = true;

                    var hsv = new ColorHSV
                    {
                        H = (int)_sliderH.Value,
                        S = (int)_sliderS.Value,
                        V = (int)_sliderV.Value
                    };
                    SetHSV(hsv);

                    _suppressHSV = true;
                    var rgb = hsv.ToRGB();
                    Color = new Color(rgb.R, rgb.G, rgb.B, Color.A);
                }
                finally
                {
                    spinButton.Tag = false;
                    _suppressHSV   = false;
                }
            };

            slider.ValueChangedByUser += (s, a) =>
            {
                try
                {
                    slider.Tag = true;

                    var hsv = new ColorHSV
                    {
                        H = (int)_sliderH.Value,
                        S = (int)_sliderS.Value,
                        V = (int)_sliderV.Value
                    };
                    SetHSV(hsv);

                    _suppressHSV = true;
                    var rgb = hsv.ToRGB();
                    Color = new Color(rgb.R, rgb.G, rgb.B, Color.A);
                }
                finally
                {
                    slider.Tag   = false;
                    _suppressHSV = false;
                }
            };
        }
Exemplo n.º 4
0
        private void HsvInputChanged(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(_inputHSV.Text))
            {
                return;
            }

            string[] st = _inputHSV.Text.Split(',');
            if (st.Length != 3)
            {
                return;
            }
            if (int.TryParse(st[0], out int h) && byte.TryParse(st[1], out byte s) && byte.TryParse(st[2], out byte v))
            {
                if (h > 360 || s > 100 || v > 100)
                {
                    return;
                }
                ColorHSV hsv = new ColorHSV()
                {
                    H = h,
                    S = s,
                    V = v
                };
                _inputHSV.Tag = true;
                OnColorChanged(hsv);
                _inputHSV.Tag = false;
            }
        }
Exemplo n.º 5
0
        private void HsPickerMove(Point p)
        {
            int   r        = WheelHeight / 2;
            int   x        = p.X - _colorWheel.Bounds.Location.X - r - _hsPicker.Bounds.Height / 2;
            int   y        = p.Y - _colorWheel.Bounds.Location.Y - r - _hsPicker.Bounds.Width / 2;
            float angle    = (float)Math.Atan2(x, y);
            float rsquared = Math.Min(x * x + y * y, r * r);
            float radius   = (float)Math.Sqrt(rsquared);

            _hsPicker.Left = (int)(radius * Math.Sin(angle));
            _hsPicker.Top  = (int)(radius * Math.Cos(angle));

            int h = (int)(angle * RadToDeg) + 90;

            if (h < 0)
            {
                h += 360;
            }
            ColorHSV hsv = new ColorHSV()
            {
                H = h,
                S = (int)(radius / r * 100),
                V = colorHSV.V
            };

            _hsPicker.Tag = true;
            OnColorChanged(hsv);
            _hsPicker.Tag = false;
        }
Exemplo n.º 6
0
        private void OnColorChanged(ColorHSV h)
        {
            var c = h.ToRGB();

            c = ColorStorage.CreateColor(c.R, c.G, c.B, 255);
            OnColorChanged(c, h);
        }
Exemplo n.º 7
0
        private void VPickerMove(Point p)
        {
            int x = p.Y - _gradient.Bounds.Location.Y - _hsPicker.Bounds.Height / 2;

            x            = Math.Max(0, Math.Min(x, WheelHeight));
            _vPicker.Top = x;

            ColorHSV hsv = new ColorHSV()
            {
                H = colorHSV.H,
                S = colorHSV.S,
                V = 100 * (WheelHeight - x) / WheelHeight
            };

            _vPicker.Tag = true;
            OnColorChanged(hsv);
            _vPicker.Tag = false;
        }
Exemplo n.º 8
0
        private void OnColorChanged(Color rgb, ColorHSV hsv)
        {
            if (!(bool)_inputRGB.Tag)
            {
                _inputRGB.Text = string.Format("{0},{1},{2}", rgb.R, rgb.G, rgb.B);
            }
            if (!(bool)_inputHSV.Tag)
            {
                _inputHSV.Text = string.Format("{0},{1},{2}", hsv.H, hsv.S, hsv.V);
            }
            if (!(bool)_inputHEX.Tag)
            {
                _inputHEX.Text = rgb.ToHexString().Substring(1, 6);
            }
            if (!(bool)_inputAlpha.Tag)
            {
                _inputAlpha.Text = DisplayAlpha.ToString();
            }
            if (!(bool)_sliderAlpha.Tag)
            {
                _sliderAlpha.Value = DisplayAlpha;
            }
            if (!(bool)_hsPicker.Tag)
            {
                _hsPicker.Top  = (int)(hsv.S / 200f * WheelHeight * Math.Sin(DegToRad * (-hsv.H + 180)));
                _hsPicker.Left = (int)(hsv.S / 200f * WheelHeight * Math.Cos(DegToRad * (-hsv.H + 180)));
            }
            if (!(bool)_vPicker.Tag)
            {
                _vPicker.Top = (int)(hsv.V / -100f * WheelHeight) + WheelHeight;
            }

            _colorWheel.Color = new Color(hsv.V / 100f, hsv.V / 100f, hsv.V / 100f);

            _colorDisplay.Color = rgb;

            colorHSV = hsv;
        }
Exemplo n.º 9
0
        /// <summary>
        /// Converts HSV color system to RGB
        /// </summary>
        /// <returns></returns>
        public static Color ToRGB(this ColorHSV colorHSV)
        {
            float h = colorHSV.H;

            if (colorHSV.H == 360)
            {
                h = 359;
            }
            float s = colorHSV.S;
            float v = colorHSV.V;

            int   i;
            float f, p, q, t;

            h  = Math.Max(0.0f, Math.Min(360.0f, h));
            s  = Math.Max(0.0f, Math.Min(100.0f, s));
            v  = Math.Max(0.0f, Math.Min(100.0f, v));
            s /= 100;
            v /= 100;
            h /= 60;
            i  = (int)Math.Floor(h);
            f  = h - i;
            p  = v * (1 - s);
            q  = v * (1 - s * f);
            t  = v * (1 - s * (1 - f));

            int r, g, b;

            switch (i)
            {
            case 0:
                r = (int)Math.Round(255 * v);
                g = (int)Math.Round(255 * t);
                b = (int)Math.Round(255 * p);
                break;

            case 1:
                r = (int)Math.Round(255 * q);
                g = (int)Math.Round(255 * v);
                b = (int)Math.Round(255 * p);
                break;

            case 2:
                r = (int)Math.Round(255 * p);
                g = (int)Math.Round(255 * v);
                b = (int)Math.Round(255 * t);
                break;

            case 3:
                r = (int)Math.Round(255 * p);
                g = (int)Math.Round(255 * q);
                b = (int)Math.Round(255 * v);
                break;

            case 4:
                r = (int)Math.Round(255 * t);
                g = (int)Math.Round(255 * p);
                b = (int)Math.Round(255 * v);
                break;

            default:
                r = (int)Math.Round(255 * v);
                g = (int)Math.Round(255 * p);
                b = (int)Math.Round(255 * q);
                break;
            }

            return(new Color((byte)r, (byte)g, (byte)b, (byte)255));
        }