tangxu
2024-12-24 91105b77c916d06dd30380e20594e29f85eae3da
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#region Imports
 
using DPumpHydr.WinFrmUI.RLT.Properties;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
 
#endregion
 
namespace DPumpHydr.WinFrmUI.RLT.Controls
{
    #region ParrotColorPicker
 
    public class ParrotColorPicker : Control
    {
        public ParrotColorPicker()
        {
            base.Size = new Size(133, 133);
            base.SetStyle(ControlStyles.UserPaint | ControlStyles.ResizeRedraw | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
        }
 
        [Category("Parrot")]
        [Browsable(true)]
        [Description("The color picker image")]
        public Image PickerImage
        {
            get => image;
            set
            {
                image = value;
                Invalidate();
            }
        }
 
        [Category("Parrot")]
        [Browsable(true)]
        [Description("The selected color")]
        public Color SelectedColor { get; set; }
 
        [Category("Parrot")]
        [Browsable(true)]
        [Description("Returns the selected color hex value")]
        public string SelectedColorHex => ColorTranslator.ToHtml(SelectedColor);
 
        [Category("Parrot")]
        [Browsable(true)]
        [Description("Returns the selected color rgb value")]
        public string SelectedColorRgb => $"{ColorTranslator.FromHtml(SelectedColorHex).R}, {ColorTranslator.FromHtml(SelectedColorHex).G}, {ColorTranslator.FromHtml(SelectedColorHex).B}";
 
        [Category("Parrot")]
        [Browsable(true)]
        [Description("Show the selected color preview")]
        public bool ShowColorPreview { get; set; } = true;
 
        private void GetColor(int x, int y)
        {
            if (x > 1 && y > 1 && x < base.Width - 2 && y < base.Height - 2)
            {
                Bitmap bitmap = (Bitmap)new Bitmap(image, base.Width - 3, base.Height - 3).Clone();
                if (bitmap.GetPixel(x - 1, y - 1).A > 0)
                {
                    try
                    {
                        SelectedColor = bitmap.GetPixel(x, y);
                    }
                    catch
                    {
                    }
                }
                bitmap.Dispose();
                x1 = x;
                y1 = y;
                OnSelectedColorChanged();
                Invalidate();
            }
        }
 
        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            isSelectingColor = true;
            GetColor(e.X, e.Y);
        }
 
        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            isSelectingColor = false;
            Invalidate();
        }
 
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            base.CreateGraphics();
            if (isSelectingColor)
            {
                GetColor(e.X, e.Y);
            }
        }
 
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            BufferedGraphicsContext bufferedGraphicsContext = BufferedGraphicsManager.Current;
            bufferedGraphicsContext.MaximumBuffer = new Size(base.Width + 1, base.Height + 1);
            bufferedGraphics = bufferedGraphicsContext.Allocate(base.CreateGraphics(), base.ClientRectangle);
            bufferedGraphics.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            bufferedGraphics.Graphics.InterpolationMode = InterpolationMode.HighQualityBilinear;
            bufferedGraphics.Graphics.CompositingQuality = CompositingQuality.HighQuality;
            bufferedGraphics.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
            bufferedGraphics.Graphics.Clear(BackColor);
            bufferedGraphics.Graphics.DrawImage(new Bitmap(image, base.Width - 3, base.Height - 3), 1, 1);
            if (isSelectingColor && ShowColorPreview)
            {
                bufferedGraphics.Graphics.FillRectangle(new SolidBrush(SelectedColor), new RectangleF(x1 - 10, y1 - 10, 20f, 20f));
            }
            bufferedGraphics.Render(e.Graphics);
        }
 
        public event EventHandler SelectedColorChanged;
 
        protected virtual void OnSelectedColorChanged()
        {
            SelectedColorChanged?.Invoke(this, new EventArgs());
        }
 
        private BufferedGraphics bufferedGraphics;
 
        public Image image = Resources.color_picker;
        private int x1;
 
        private int y1;
 
        private bool isSelectingColor;
    }
 
    #endregion
}