Esempio n. 1
0
        public KColorPicker()
        {
            StackPanel p1 = new();
            StackPanel p2 = new() { Orientation = Orientation.Horizontal, Margin = new Thickness(0, 0, 0, 3) };

            p1.Children.Add(p2);

            TextBox tH = null, tL = null, tS = null;

            var fColor = new TextBlock {
                Text = "Color", ToolTip = "Color RRGGBB", Foreground = Brushes.Black
            };

            p2.Children.Add(fColor);

            var bColor = new Rectangle {
                Width = 9, Height = 9, Fill = Brushes.Black, Margin = new Thickness(4, 2, 4, 0)
            };

            p2.Children.Add(bColor);

            _tColor              = new() { Width = 68, Text = "000000" };
            _tColor.TextChanged += (o, e) => {
                int col = _GetColor(bgr: true);
                if (!_hlsChanging)
                {
                    var(H, L, S) = ColorInt.ToHLS(col, bgr: true);
                    _hlsChanging = true;
                    if (S != 0)
                    {
                        tH.Text = H.ToString();
                    }
                    tL.Text      = L.ToString();
                    tS.Text      = S.ToString();
                    _hlsChanging = false;
                }

                if (!_palSelecting)
                {
                    _pal.SelectColor(col);
                }

                var brush = new SolidColorBrush(System.Windows.Media.Color.FromRgb((byte)col, (byte)(col >> 8), (byte)(col >> 16)));
                fColor.Foreground = brush;
                bColor.Fill       = brush;

                if (ColorChanged != null)
                {
                    if (!BGR)
                    {
                        col = ColorInt.SwapRB(col);
                    }
                    ColorChanged(col);
                }
            };
            p2.Children.Add(_tColor);

            //hls: 0 H, 1 L, 2 S.
            TextBox _AddHLS(int hls, string label, string tooltip)
            {
                p2.Children.Add(new TextBlock {
                    Text = label, Margin = new Thickness(hls == 0 ? 18 : 12, 0, 4, 0), ToolTip = tooltip
                });
                TextBox t = new() { Width = 34, Text = "0" };

                t.TextChanged += (o, e) => {
                    if (_hlsChanging)
                    {
                        return;
                    }
                    var tb = o as TextBox;
                    int i  = tb.Text.ToInt();
                    if (i < 0 || i > 240)
                    {
                        _hlsChanging = true;
                        tb.Text      = Math.Clamp(i, 0, 240).ToS();
                        _hlsChanging = false;
                    }

                    int H = tH.Text.ToInt(), L = tL.Text.ToInt(), S = tS.Text.ToInt();
                    int col = ColorInt.FromHLS(H, L, S, bgr: true);
                    _hlsChanging = true;
                    _SetColor(col, bgr: true);
                    _hlsChanging = false;
                };
                t.MouseWheel += (o, e) => {
                    int d = e.Delta / 15;                     //+-8
                    if (o == tH)
                    {
                        d /= 2;                              //hue is more sensitive; 8 would jump directly from box to box
                    }
                    int i = t.Text.ToInt() + d;
                    if (hls == 0)
                    {
                        if (i < 0)
                        {
                            i += 240;
                        }
                        else if (i > 240)
                        {
                            i -= 240;
                        }
                    }
                    int v = Math.Clamp(i, 0, 240);
                    t.Text = v.ToS();
                };
                p2.Children.Add(t);
                return(t);
            }

            tH = _AddHLS(0, "H", "Hue 0-240. Change with mouse wheel.");
            tL = _AddHLS(1, "L", "Luminance 0-240. Change with mouse wheel.");
            tS = _AddHLS(2, "S", "Saturation 0-240. Change with mouse wheel.");

            _pal = new _Palette(this);
            p1.Children.Add(_pal);

            base.Content = p1;
        }

        class _Palette : HwndHost
        {
            KColorPicker _cp;
            wnd _w;
            int _dpi;
            int _cellSize;
            const int c_nHue = 30;            //number of hue columns. Must be 240-divisible.
            const int c_nLum = 7;             //number of luminance rows. Must be 240-divisible minus 1.