Пример #1
0
            void _Paint(nint dc)
            {
                using var g      = System.Drawing.Graphics.FromHdc(dc);
                using var penSel = new System.Drawing.Pen(System.Drawing.Color.Black)
                      {
                          DashStyle = System.Drawing.Drawing2D.DashStyle.Dot
                      };
                g.Clear(System.Drawing.Color.White);

                var z = _cellSize;

                //colors. Hue in x axis, luminance in y axis.
                int y = 0;

                for (int i = 0, lum = 240; (lum -= 240 / (c_nLum + 1)) > 0; i++)
                {
                    for (int j = 0, hue = 0; hue < 240; j++, hue += 240 / c_nHue)
                    {
                        //					var col=_ColorHLSToRGB(hue, lum, 240);

                        int lum2 = lum;
                        if (lum >= 120)
                        {
                            int d = Math.Abs(80 - hue);                             //diff from green
                            if (d <= 64)
                            {
                                d /= 4;
                                //							if(lum==120) print.it(Math.Max(d, 10), (240-lum)/Math.Max(d, 10));
                                lum2 -= (240 - lum) / Math.Max(d, 8);
                            }
                        }
                        var col = ColorInt.FromHLS(hue, lum2, 240, bgr: true);

                        _ac[j, i] = col;

                        //					if(lum==120 /*&& hue<=180*/) {
                        //						ColorInt k=ColorInt.FromBGR(col, true);
                        //						var b=k.GetPerceivedBrightness();
                        //						print.it(col.ToString("X6"), hue, lum, b);
                        //					}

                        _Draw(col, j * z);
                    }
                    y += z;
                }

                //black...white
                y += _cellSize / 4;
                for (int j = 0, gray = 0; j < c_nHue; j++, gray += gray < 120 ? 10 : 8)
                {
                    //				print.it(gray);
                    if (gray > 255)
                    {
                        gray = 255;
                    }
                    var col = (gray << 16) | (gray << 8) | gray;
                    _ac[j, c_nLum] = col;
                    _Draw(col, j * z);
                }

                //selection
                y = _select.lum * z; if (_select.lum == c_nLum)
                {
                    y += _cellSize / 4;
                }
                g.DrawRectangle(penSel, _select.hue * z, y, z, z);

                void _Draw(int col, int x)
                {
                    RECT r     = (x + 1, y + 1, z - 1, z - 1);
                    var  brush = Api.CreateSolidBrush(col);

                    Api.FillRect(dc, r, brush);
                    Api.DeleteObject(brush);
                }
            }
Пример #2
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.