tangxu
2025-02-26 2fc64c1af548596c4719d4a3abdc10de7a1d7e8f
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#region Imports
 
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
 
#endregion
 
namespace DPumpHydr.WinFrmUI.RLT.Controls
{
    #region CyberColorPicker
 
    public partial class CyberColorPicker : UserControl
    {
        #region Variables
 
        private class ValueBox
        {
            public float Value = 1F;
            public Color Color = Color.White;
        }
 
        private bool ValueBoxMD;
        private PointF CursorPos;
        private bool ColorPickerMD;
 
        #endregion
 
        #region Property Region
 
        private Color tmp_selectedcolor;
        [Category("Cyber")]
        [Description("Selected color")]
        public Color SelectedColor
        {
            get => tmp_selectedcolor;
            set
            {
                tmp_selectedcolor = value;
                ColorChanged(value);
            }
        }
 
        #endregion
 
        #region Constructor Region
 
        public CyberColorPicker()
        {
            InitializeComponent();
 
            Tag = "Cyber";
            ForeColor = Color.WhiteSmoke;
            pictureBox1.Tag = new PointF((float)pictureBox1.Width / 2, (float)pictureBox1.Height / 2);
            pictureBox3.Tag = new ValueBox();
 
            Bitmap Wheel = new(pictureBox1.Width, pictureBox1.Height);
            Graphics g = Graphics.FromImage(Wheel);
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.SmoothingMode = SmoothingMode.AntiAlias;
            DrawWheel((float)Wheel.Width / 2, g);
            pictureBox1.Image = Wheel;
            CursorPos = new PointF((float)pictureBox1.Height / 2, (float)pictureBox1.Height / 2);
        }
 
        #endregion
 
        #region Event Region
 
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
 
            if (label1.ForeColor != ForeColor)
            {
                label1.ForeColor = ForeColor;
            }
 
            if (label2.ForeColor != ForeColor)
            {
                label2.ForeColor = ForeColor;
            }
        }
 
        private void PictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            ColorPickerMD = true;
            PickColor(e.X, e.Y);
        }
 
        private void PictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (ColorPickerMD)
            {
                PickColor(e.X, e.Y);
            }
        }
 
        private void PictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            ColorPickerMD = false;
            PickColor(e.X, e.Y);
        }
 
        private void PictureBox1_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            e.Graphics.DrawEllipse(new Pen(Color.DarkGray, 1F), CursorPos.X - 4, CursorPos.Y - 4, 8, 8);
        }
 
        private void PictureBox3_Paint(object sender, PaintEventArgs e)
        {
            LinearGradientBrush Brush = new(new Point(0, 0), new Point(0, pictureBox3.Height), ((ValueBox)pictureBox3.Tag).Color, Color.Black);
            e.Graphics.FillRectangle(Brush, pictureBox3.ClientRectangle);
            e.Graphics.FillRectangle(new SolidBrush(Color.DarkGray), 0, (pictureBox3.Height * (1 - ((ValueBox)pictureBox3.Tag).Value)) - 2, pictureBox3.Width, 4);
        }
 
        private void PictureBox3_MouseDown(object sender, MouseEventArgs e)
        {
            ValueBoxMD = true;
            if (e.Y > pictureBox3.Height || e.Y < 0)
            {
                return;
            } ((ValueBox)pictureBox3.Tag).Value = (float)(pictureBox3.Height - e.Y) / pictureBox3.Height;
            PickColor(((PointF)pictureBox1.Tag).X, ((PointF)pictureBox1.Tag).Y);
        }
 
        private void PictureBox3_MouseMove(object sender, MouseEventArgs e)
        {
            if (ValueBoxMD)
            {
                if (e.Y > pictureBox3.Height || e.Y < 0)
                {
                    return;
                } ((ValueBox)pictureBox3.Tag).Value = (float)(pictureBox3.Height - e.Y) / pictureBox3.Height;
                PickColor(((PointF)pictureBox1.Tag).X, ((PointF)pictureBox1.Tag).Y);
            }
        }
        private void PictureBox3_MouseUp(object sender, MouseEventArgs e)
        {
            ValueBoxMD = false;
        }
 
        #endregion
 
        #region Event Handler Region
 
        public delegate void EventHandler(Color color);
        [Category("Cyber")]
        [Description("The event is raised when the value of the Text property changes in Control.")]
        public event EventHandler ColorChanged = delegate { };
 
        #endregion
 
        #region Method Region
 
        private void DrawWheel(float radius, Graphics graphics)
        {
            GraphicsPath FillPath = new();
            FillPath.AddEllipse(0, 0, radius * 2, radius * 2);
            FillPath.Flatten();
 
            GraphicsPath BrushPath = new();
            BrushPath.AddEllipse(-1, -1, (radius * 2) + 2, (radius * 2) + 2);
            BrushPath.Flatten();
 
            graphics.FillPath(GetBrush(BrushPath), FillPath);
        }
 
        private Brush GetBrush(GraphicsPath graphicsPath)
        {
            PathGradientBrush Brush = new(graphicsPath) { CenterColor = Color.White };
 
            Color[] Colors = new Color[graphicsPath.PointCount];
            for (int Angle = 0; Angle < Colors.Length; Angle++)
            {
                Colors[Angle] = HSVtoRGB((float)Angle / Colors.Length, 1, 1);
            }
            Brush.SurroundColors = Colors;
 
            return Brush;
        }
 
        private Color GetPixelColor(float x, float y, float value, float radius)
        {
            float R = (float)Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2));
            float S = (float)(R / radius);
            if (S > 1)
            {
                return Color.Transparent;
            }
 
            double Angle;
            if (x > 0 && y > 0)
            {
                Angle = Math.Asin(y / R);
            }
            else if (x <= 0 && y > 0)
            {
                Angle = Math.Acos(y / R) + (Math.PI / 2);
            }
            else if (x <= 0 && y <= 0)
            {
                Angle = Math.Asin(-y / R) + Math.PI;
            }
            else
            {
                Angle = Math.Acos(-y / R) + (3 * Math.PI / 2);
            }
 
            float H = (float)(Angle / Math.PI / 2);
 
            return HSVtoRGB(H, S, value);
        }
 
        private Color PickColor(float x, float y)
        {
            float x1 = x - ((float)pictureBox1.Width / 2);
            float y1 = y - ((float)pictureBox1.Height / 2);
            float Radius = (float)Math.Sqrt(Math.Pow(x1, 2) + Math.Pow(y1, 2));
 
            if (Radius > (float)pictureBox1.Width / 2)
            {
                float mult = (float)pictureBox1.Width / 2 / Radius;
                x1 *= mult;
                y1 *= mult;
            }
 
            Color Color = GetPixelColor(x1, y1, ((ValueBox)pictureBox3.Tag).Value, (float)pictureBox1.Width / 2);
 
            if (Color == Color.Transparent)
            {
                return Color.Empty;
            }
 
            label1.Text = $"RGB: {Color.R}, {Color.G}, {Color.B}";
            label2.Text = $"HEX: #{Color.ToArgb():X}";
            CursorPos = new PointF(x1 + ((float)pictureBox1.Width / 2), y1 + ((float)pictureBox1.Height / 2));
            pictureBox2.BackColor = Color;
            ((ValueBox)pictureBox3.Tag).Color = Color;
            pictureBox1.Tag = new PointF(x, y);
            pictureBox1.Invalidate();
            pictureBox2.Invalidate();
            pictureBox3.Invalidate();
 
            SelectedColor = Color;
            return Color;
        }
 
        private Color HSVtoRGB(double H, double S, double V)
        {
            double R, G, B;
 
            if (S == 0)
            {
                R = V * 255;
                G = V * 255;
                B = V * 255;
            }
            else
            {
                double H1 = H * 6;
                if (H1 == 6)
                {
                    H1 = 0;
                }
 
                int i = (int)Math.Floor(H1);
 
                double i1 = V * (1 - S);
                double i2 = V * (1 - (S * (H1 - i)));
                double i3 = V * (1 - (S * (1 - (H1 - i))));
 
                switch (i)
                {
                    case 0:
                        R = V;
                        G = i3;
                        B = i1;
                        break;
                    case 1:
                        R = i2;
                        G = V;
                        B = i1;
                        break;
                    case 2:
                        R = i1;
                        G = V;
                        B = i3;
                        break;
                    case 3:
                        R = i1;
                        G = i2;
                        B = V;
                        break;
                    case 4:
                        R = i3;
                        G = i1;
                        B = V;
                        break;
                    default:
                        R = V;
                        G = i1;
                        B = i2;
                        break;
                }
 
                R *= 255;
                G *= 255;
                B *= 255;
            }
 
            return Color.FromArgb((int)R, (int)G, (int)B);
        }
 
        #endregion
    }
 
    #endregion
}