-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
305 lines (246 loc) · 9.64 KB
/
Form1.cs
File metadata and controls
305 lines (246 loc) · 9.64 KB
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TextBox;
namespace ColorHelper
{
public partial class Form1 : Form
{
private readonly ColorPickerHelper _picker = new ColorPickerHelper();
private readonly Converters _converters = new Converters();
bool _pickingScreen = false;
bool _pickingMouseDown = false;
bool buttonPressed = false;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
trkAlpha.Minimum = 0;
trkAlpha.Maximum = 100;
trkAlpha.Value = 100;
txtOpacity.Text = "100%";
txtAlpha.Text = "255";
txtColor.Text = "#";
pctColorSpectrum.Image = _picker.GerarGradiente(pctColorSpectrum.Width, pctColorSpectrum.Height);
CheckForIllegalCrossThreadCalls = false;
Thread thread = new Thread(BackgroundThread) { IsBackground = true };
thread.Start();
}
Color GetColorAt(Point location)
{
using (Bitmap pixelContainer = new Bitmap(1, 1))
{
using (Graphics graphics = Graphics.FromImage(pixelContainer))
{
graphics.CopyFromScreen(location, Point.Empty, pixelContainer.Size);
}
return pixelContainer.GetPixel(0, 0);
}
}
void BackgroundThread()
{
while (true)
{
if (buttonPressed)
{
Point cursorPosition = Cursor.Position;
Color selectedColor = GetColorAt(cursorPosition);
lblSmallScreen.BackColor = selectedColor;
string hexValue = ColorTranslator.ToHtml(selectedColor);
txtColor.Text = hexValue;
}
}
}
#region Mouse Events
private void btnPickScreenColor_MouseDown(object sender, MouseEventArgs e)
{
buttonPressed = true;
Cursor = Cursors.Cross;
}
private void btnPickScreenColor_MouseUp(object sender, MouseEventArgs e)
{
buttonPressed = false;
Cursor = Cursors.Default;
}
// ==============================================================
private void pctColorSpectrum_MouseMove(object sender, MouseEventArgs e)
{
_picker.OnMouseMove(pctColorSpectrum, lblSmallScreen,
txtColor, txtAlpha, txtRed, txtGreen, txtBlue, e);
}
private void pctColorSpectrum_MouseDown(object sender, MouseEventArgs e)
{
if (_pickingScreen) return;
_picker.OnMouseDown(pctColorSpectrum, lblSmallScreen,
txtColor, txtAlpha, txtRed, txtGreen, txtBlue, e);
}
private void pctColorSpectrum_MouseUp(object sender, MouseEventArgs e)
{
_picker.OnMouseUp();
}
private void pctColorSpectrum_MouseEnter(object sender, EventArgs e)
{
Cursor = Cursors.Cross;
}
private void pctColorSpectrum_MouseLeave(object sender, EventArgs e)
{
Cursor = Cursors.Default;
}
private void btnCopyHex_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtColor.Text))
{
Clipboard.SetText(txtColor.Text);
MessageBox.Show($"HEX: {txtColor.Text}", "HEX COPIED", MessageBoxButtons.OK);
}
}
#endregion
private void trkAlpha_Scroll(object sender, EventArgs e)
{
int percent = trkAlpha.Value; // 0–100
int alpha255 = _converters.PercentageToAlpha(percent); // 0–255
Color baseColor = lblSmallScreen.BackColor;
Color newColor = Color.FromArgb(alpha255, baseColor.R, baseColor.G, baseColor.B);
lblSmallScreen.BackColor = newColor;
txtAlpha.Text = alpha255.ToString();
txtOpacity.Text = percent + "%";
}
private void txtColor_TextChanged(object sender, EventArgs e)
{
if (!txtColor.Text.StartsWith("#"))
{
int textPosition = txtColor.SelectionStart;
txtColor.Text = "#" + txtColor.Text.TrimStart('#');
txtColor.SelectionStart = Math.Max(textPosition, 1);
return;
}
string hex = txtColor.Text.Trim();
if (string.IsNullOrWhiteSpace(hex) || hex == "#")
return;
try
{
Color c = _picker.HexToColor(hex); // #RRGGBB ou #AARRGGBB
lblSmallScreen.BackColor = c;
int percent = _converters.AlphaToPercentage(c.A);
txtAlpha.Text = c.A.ToString(); // 0–100
txtRed.Text = c.R.ToString();
txtGreen.Text = c.G.ToString();
txtBlue.Text = c.B.ToString();
trkAlpha.Value = percent;
txtOpacity.Text = percent + "%";
}
catch
{
// ignorar enquanto o usuário digita algo inválido
}
}
#region ARGB TextChanged
private void txtAlpha_TextChanged(object sender, EventArgs e)
{
// permite vazio ou um número parcial enquanto digita
if (string.IsNullOrWhiteSpace(txtAlpha.Text))
return;
if (!int.TryParse(txtAlpha.Text, out _))
return;
UpdateColorFromChannels();
}
private void txtRed_TextChanged(object sender, EventArgs e)
{
UpdateColorFromChannels();
}
private void txtGreen_TextChanged(object sender, EventArgs e)
{
UpdateColorFromChannels();
}
private void txtBlue_TextChanged(object sender, EventArgs e)
{
UpdateColorFromChannels();
}
#endregion
private void UpdateColorFromChannels()
{
if (!int.TryParse(txtAlpha.Text, out int a255)) a255 = 255;
if (!int.TryParse(txtRed.Text, out int r)) r = 0;
if (!int.TryParse(txtGreen.Text, out int g)) g = 0;
if (!int.TryParse(txtBlue.Text, out int b)) b = 0;
a255 = Math.Max(0, Math.Min(255, a255));
r = Math.Max(0, Math.Min(255, r));
g = Math.Max(0, Math.Min(255, g));
b = Math.Max(0, Math.Min(255, b));
int percent = _converters.AlphaToPercentage(a255); // 0–100
trkAlpha.Value = percent;
txtOpacity.Text = percent + "%";
Color c = Color.FromArgb(a255, r, g, b);
lblSmallScreen.BackColor = c;
txtColor.TextChanged -= txtColor_TextChanged;
txtColor.Text = $"#{c.A:X2}{c.R:X2}{c.G:X2}{c.B:X2}";
txtColor.TextChanged += txtColor_TextChanged;
// garante que o valor visível esteja clampado, sem recursão
txtAlpha.TextChanged -= txtAlpha_TextChanged;
txtAlpha.Text = a255.ToString();
txtAlpha.TextChanged += txtAlpha_TextChanged;
}
private void pctColorSpectrum_Click(object sender, EventArgs e)
{
}
private void btnPickScreenColor_Click(object sender, EventArgs e)
{
_pickingScreen = true;
Cursor = Cursors.Cross;
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (!_pickingScreen || e.Button != MouseButtons.Left)
return;
_pickingMouseDown = true;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (!_pickingScreen || !_pickingMouseDown)
return;
Point screenPos = Cursor.Position;
Color c = _picker.GetScreenColorAt(screenPos);
_picker.ApplyColorToControls(
c, lblSmallScreen, txtColor, txtAlpha, txtRed, txtGreen, txtBlue);
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
if (!_pickingScreen || e.Button != MouseButtons.Left)
return;
_pickingMouseDown = false;
_pickingScreen = false;
Cursor = Cursors.Default;
}
private void txtColor_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Back && txtColor.SelectionStart <= 1)
e.SuppressKeyPress = true;
}
private void txtOpacity_TextChanged(object sender, EventArgs e)
{
string text = txtOpacity.Text.Replace("%", "").Trim();
if (!int.TryParse(text, out int percent))
return;
percent = Math.Max(0, Math.Min(100, percent));
trkAlpha.Value = percent;
int a255 = _converters.PercentageToAlpha(percent);
Color baseColor = lblSmallScreen.BackColor;
lblSmallScreen.BackColor = Color.FromArgb(a255, baseColor.R, baseColor.G, baseColor.B);
txtAlpha.Text = a255.ToString(); // mostra 0–255
txtOpacity.TextChanged -= txtOpacity_TextChanged;
txtOpacity.Text = percent + "%";
txtOpacity.TextChanged += txtOpacity_TextChanged;
}
}
}