-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathColorPicker.cs
More file actions
301 lines (279 loc) · 9.96 KB
/
ColorPicker.cs
File metadata and controls
301 lines (279 loc) · 9.96 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
using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
namespace DarkNotepad
{
// This class is responsible for picking a color for the 'Stylize' class.
// The user can choose a color with a number(textboxes) with a slider(TrackBars)
// or with the Color Spectrum(Picturebox).
// The Color Spectrum is custom made.
public partial class ColorPicker : Form
{
private Stylize St;
private Button global_btn;
public ColorPicker(Button btn)
{
InitializeComponent();
global_btn = btn;
textBox1.Leave += new EventHandler((sender,e) => textBoxGeneral_Leave(sender,e,textBox1));
textBox2.Leave += new EventHandler((sender,e) => textBoxGeneral_Leave(sender,e,textBox2));
textBox3.Leave += new EventHandler((sender,e) => textBoxGeneral_Leave(sender,e,textBox3));
textBox1.Text = btn.BackColor.R.ToString();
textBox2.Text = btn.BackColor.G.ToString();
textBox3.Text = btn.BackColor.B.ToString();
textBox1_TextChanged(null, null);
textBox2_TextChanged(null, null);
textBox3_TextChanged(null, null);
}
private void updateSt(object sender, EventArgs e)
{
if (St == null)
{
if (Application.OpenForms.OfType<Stylize>().Any())
{
St = Application.OpenForms.OfType<Stylize>().First();
}
}
}
// This function looks into where the mouse is on the Color Spectrum,
// and picks that color.
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
try
{
Bitmap pixelData = (Bitmap)pictureBox1.Image;
if (e.X >= pixelData.Width || e.Y >= pixelData.Height
|| e.X < 0 || e.Y < 0) return;
Color clr = pixelData.GetPixel(e.X, e.Y);
textBox1.Text = clr.R.ToString();
textBox2.Text = clr.G.ToString();
textBox3.Text = clr.B.ToString();
updatePreviewColor();
}catch{}
}
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
pictureBox1_MouseMove(sender, e);
}
private void updatePreviewColor()
{
try
{
panel1.BackColor = Color.FromArgb(trackBar1.Value,
trackBar2.Value,
trackBar3.Value);
}catch{}
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
textBox1.Text = trackBar1.Value.ToString();
}
private void trackBar2_Scroll(object sender, EventArgs e)
{
textBox2.Text = trackBar2.Value.ToString();
}
private void trackBar3_Scroll(object sender, EventArgs e)
{
textBox3.Text = trackBar3.Value.ToString();
}
// This function makes sure that the user did not enter values that are too high.
// It also updates the 'Track Bars' and color preview.
private void textBox1_TextChanged(object sender, EventArgs e)
{
try
{
if (int.Parse(textBox1.Text) > 255)
{
textBox1.Text = "255";
}
if (int.Parse(textBox1.Text) < 0)
{
textBox1.Text = "0";
}
trackBar1.Value = int.Parse(textBox1.Text);
colorToHex();
}
catch{}
updatePreviewColor();
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
try
{
if (int.Parse(textBox2.Text) > 255)
{
textBox2.Text = "255";
}
if (int.Parse(textBox2.Text) < 0)
{
textBox2.Text = "0";
}
trackBar2.Value = int.Parse(textBox2.Text);
colorToHex();
}
catch{}
updatePreviewColor();
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
try
{
if (int.Parse(textBox3.Text) > 255)
{
textBox3.Text = "255";
}
if (int.Parse(textBox3.Text) < 0)
{
textBox3.Text = "0";
}
trackBar3.Value = int.Parse(textBox3.Text);
colorToHex();
}
catch{}
updatePreviewColor();
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
if (!textBox4.Focused) return;
Color BackUpcolor = Color.FromArgb(trackBar1.Value,trackBar2.Value,trackBar3.Value);
try
{
Color color = ColorTranslator.FromHtml(String.Format("#{0}", textBox4.Text));
textBox1.Text = color.R.ToString();
textBox2.Text = color.G.ToString();
textBox3.Text = color.B.ToString();
}
catch
{
textBox1.Text = BackUpcolor.R.ToString();
textBox2.Text = BackUpcolor.G.ToString();
textBox3.Text = BackUpcolor.B.ToString();
}
finally
{
updatePreviewColor();
}
}
private void colorToHex()
{
textBox4.Text = trackBar1.Value.ToString("X2") +
trackBar2.Value.ToString("X2") +
trackBar3.Value.ToString("X2");
}
// These functions check whether the user has entered numbers into the textboxes.
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = checkChar(e);
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.C)
{
copyFunc(textBox1);
}
if (e.Control && e.KeyCode == Keys.V)
{
pasteFunc(textBox1);
}
}
private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = checkChar(e);
}
private void textBox2_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.C)
{
copyFunc(textBox2);
}
if (e.Control && e.KeyCode == Keys.V)
{
pasteFunc(textBox2);
}
}
private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = checkChar(e);
}
private void textBox3_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.C)
{
copyFunc(textBox3);
}
if (e.Control && e.KeyCode == Keys.V)
{
pasteFunc(textBox3);
}
}
private bool checkChar(KeyPressEventArgs e)
{
string[] legalChar = new string[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
foreach (string num in legalChar)
{
if (e.KeyChar.ToString() == num ||
e.KeyChar == (Char)Keys.Back)
{
return false;
}
}
return true;
}
private void pasteFunc(TextBox TB)
{
IDataObject id = Clipboard.GetDataObject();
if (id.GetDataPresent(DataFormats.UnicodeText)
|| id.GetDataPresent(DataFormats.Text)
|| id.GetDataPresent(DataFormats.Html))
{
string clipboardText = Clipboard.GetText(TextDataFormat.Text);
string txtbxValue = "";
string[] legalChar = new string[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
foreach (char ch in clipboardText)
{
foreach (string lc in legalChar)
{
if (ch.ToString().Equals(lc))
txtbxValue = txtbxValue + ch;
}
}
TB.SelectedText = txtbxValue;
}
}
private void copyFunc(TextBox TB)
{
if (TB.SelectedText.Length <= 0) return;
Clipboard.SetText(TB.SelectedText);
}
private void Exit_Button_Click(object sender, EventArgs e)
{
this.Dispose();
updateSt(sender, e);
St.Focus();
}
private void Apply_Button_Click(object sender, EventArgs e)
{
updateSt(sender, e);
if (St != null)
{
global_btn.BackColor = panel1.BackColor;
global_btn.FlatAppearance.MouseOverBackColor = panel1.BackColor;
global_btn.FlatAppearance.MouseDownBackColor = panel1.BackColor;
St.UpdatePreviewWindow();
}
this.Dispose();
St.Focus();
}
private void textBoxGeneral_Leave(object sender, EventArgs e, TextBox textBox)
{
if (textBox.Text == String.Empty)
{
textBox.Text = "0";
}
}
}
}