-
Notifications
You must be signed in to change notification settings - Fork 0
/
YilmazturkThemeSelector.cs
355 lines (310 loc) · 10.7 KB
/
YilmazturkThemeSelector.cs
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using Button = System.Windows.Forms.Button;
using CheckBox = System.Windows.Forms.CheckBox;
using ComboBox = System.Windows.Forms.ComboBox;
using Control = System.Windows.Forms.Control;
using Label = System.Windows.Forms.Label;
using ListBox = System.Windows.Forms.ListBox;
using MessageBox = System.Windows.Forms.MessageBox;
using Panel = System.Windows.Forms.Panel;
public static class YilmazturkThemeSelector
{
#region This script was made by Emre Yılmaztürk for Yılmaztürk Software
//Don't forget to follow our Instagram account.
//Mail: yilmazturksoftware@gmail.com
//Instagram: https://instagram.com/yilmazturksoftware
#endregion
#region Variables
private const double BrightnessThreshold = 128.0;
private static Control controls;
#endregion
#region Trigger Code
public static async void Apply(string theme,Control control)
{
try{
if(theme == "light")
{
ApplyLightTheme(control);
}
else if(theme == "dark")
{
ApplyDarkTheme(control);
}
else if(theme == "system")
{
if(GetWindowsTheme() == "Default")
{
ApplyLightTheme(control);
}
else
{
ApplyDarkTheme(control);
}
}
else if (theme == "systemChange")
{
controls = control;
await tema(control);
}
else
{
ApplyAnotherTheme(control);
}
}
catch{
//linux users, windows 7 and older
}
}
#endregion
#region Main Codes
static async Task tema(Control contr)
{
if (GetWindowsTheme() == "Default")
{
ApplyLightTheme(contr);
}
else
{
ApplyDarkTheme(contr);
}
await Task.Delay(1000); tema(contr);
}
public static void ApplyAnotherTheme(Control control, bool pictureBoxDark = false)
{
var controlNames = ListControlItems(control, 0);
foreach (var controlName in controlNames)
{
var currentControl = GetControlByName(control, controlName);
if (currentControl != null)
{
ChangeThemeObjects(currentControl, pictureBoxDark);
}
if (control is Form form)
{
if (IsColorDarker(form.BackColor))
{
form.BackColor = Color.WhiteSmoke;
}
}
}
}
public static void ApplyDarkTheme(Control control, bool pictureBoxDark = false)
{
var controlNames = ListControlItems(control, 0);
foreach (var controlName in controlNames)
{
var currentControl = GetControlByName(control, controlName);
if (currentControl != null)
{
ChangeThemeObjectsDarkMode(currentControl);
}
if (control is Form form)
{
form.BackColor = Color.FromArgb(24,24,24);
}
}
}
public static void ApplyLightTheme(Control control, bool pictureBoxDark = false)
{
var controlNames = ListControlItems(control, 0);
foreach (var controlName in controlNames)
{
var currentControl = GetControlByName(control, controlName);
if (currentControl != null)
{
ChangeThemeObjectsLightMode(currentControl);
}
if (control is Form form)
{
form.BackColor = Color.WhiteSmoke;
}
}
}
private static void ChangeImageColor(Color newColor, PictureBox pictureBox)
{
Bitmap originalImage = new Bitmap(pictureBox.Image);
float[][] colorMatrixElements = {
new float[] { newColor.R / 255f, 0, 0, 0, 0 },
new float[] { 0, newColor.G / 255f, 0, 0, 0 },
new float[] { 0, 0, newColor.B / 255f, 0, 0 },
new float[] { 0, 0, 0, 1, 0 },
new float[] { 0, 0, 0, 0, 1 }
};
ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);
ImageAttributes imageAttributes = new ImageAttributes();
imageAttributes.SetColorMatrix(colorMatrix);
Bitmap newImage = new Bitmap(originalImage.Width, originalImage.Height);
using (Graphics g = Graphics.FromImage(newImage))
{
g.DrawImage(originalImage,
new Rectangle(0, 0, originalImage.Width, originalImage.Height),
0, 0, originalImage.Width, originalImage.Height,
GraphicsUnit.Pixel, imageAttributes);
}
pictureBox.Image = newImage;
}
private static void ChangeThemeObjectsLightMode(Control control)
{
if (control is Label label)
{
label.ForeColor = Color.Black;
}
else if (control is Panel panel)
{
panel.BackColor = Color.Transparent;
}
else if (control is ListBox listBox)
{
listBox.BackColor = Color.WhiteSmoke;
listBox.ForeColor = Color.Black;
}
else if (control is Button button)
{
button.BackColor = Color.Transparent;
button.ForeColor = Color.Black;
}
else if (control is ComboBox comboBox)
{
comboBox.BackColor = Color.Black;
comboBox.ForeColor = Color.White;
}
else if (control is CheckBox checkBox)
{
checkBox.BackColor = Color.Black;
checkBox.ForeColor = Color.White;
}
else if (control is ContextMenuStrip contextMenu)
{
contextMenu.BackColor = Color.Black;
contextMenu.ForeColor = Color.White;
}
else if (control is PictureBox pictureBox)
{
ChangeImageColor(Color.Black, pictureBox);
}
}
private static void ChangeThemeObjectsDarkMode(Control control)
{
if (control is Label label)
{
label.ForeColor = Color.White;
}
else if (control is Panel panel)
{
panel.BackColor = Color.Transparent;
}
else if (control is ListBox listBox)
{
listBox.BackColor = Color.FromArgb(24,24,24);
listBox.ForeColor = Color.White;
}
else if (control is ComboBox comboBox)
{
comboBox.BackColor = Color.White;
comboBox.ForeColor = Color.Black;
}
else if (control is Button button)
{
button.BackColor = Color.Transparent;
button.ForeColor = Color.White;
}
else if (control is CheckBox checkBox)
{
checkBox.BackColor = Color.White;
checkBox.ForeColor = Color.Black;
}
else if (control is ContextMenuStrip contextMenu)
{
contextMenu.BackColor = Color.White;
contextMenu.ForeColor = Color.Black;
}
}
private static void ChangeThemeObjects(Control control, bool dark)
{
if (control is Label label)
{
label.ForeColor = IsColorDarker(label.ForeColor) ? Color.White : Color.Black;
}
else if (control is Panel panel)
{
panel.BackColor = IsColorDarker(panel.BackColor) ? Color.White : Color.Black;
}
else if (control is ListBox listBox)
{
listBox.BackColor = IsColorDarker(listBox.BackColor) ? Color.White : Color.Black;
listBox.ForeColor = IsColorDarker(listBox.ForeColor) ? Color.Black : Color.White;
}
else if (control is ComboBox comboBox)
{
comboBox.BackColor = IsColorDarker(comboBox.BackColor) ? Color.White : Color.Black;
comboBox.ForeColor = IsColorDarker(comboBox.ForeColor) ? Color.Black : Color.White;
}
else if (control is CheckBox checkBox)
{
checkBox.BackColor = IsColorDarker(checkBox.BackColor) ? Color.White : Color.Black;
checkBox.ForeColor = IsColorDarker(checkBox.ForeColor) ? Color.Black : Color.White;
}
else if (control is ContextMenuStrip contextMenu)
{
contextMenu.BackColor = IsColorDarker(contextMenu.BackColor) ? Color.White : Color.Black;
contextMenu.ForeColor = IsColorDarker(contextMenu.ForeColor) ? Color.Black : Color.White;
}
else if (control is PictureBox pictureBox)
{
ChangeImageColor(Color.Black, pictureBox);
}
}
#endregion
#region Extra Codes
private static string GetWindowsTheme()
{
string RegistryKeyPath = @"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize";
string RegistryValueName = "AppsUseLightTheme";
if (SystemParameters.HighContrast)
return "High Contrast";
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(RegistryKeyPath))
{
object registryValueObject = key?.GetValue(RegistryValueName);
if (registryValueObject == null)
return "Default";
int registryValue = (int)registryValueObject;
return registryValue > 0 ? "Default" : "Dark Theme";
}
}
private static Control GetControlByName(Control control, string controlName)
{
return control.Controls.Find(controlName, true)?.FirstOrDefault();
}
private static List<string> ListControlItems(Control control, int indentLevel = 0)
{
var itemsList = new List<string>();
System.Windows.Forms.ComboBox combo = new System.Windows.Forms.ComboBox();
combo.Items.Add(control.Name);
foreach (object item in combo.Items)
{
itemsList.Add(item.ToString());
}
foreach (Control subControl in control.Controls)
{
itemsList.AddRange(ListControlItems(subControl, indentLevel + 1));
}
return itemsList;
}
public static bool IsColorDarker(Color color)
{
double brightness = 0.299 * color.R + 0.587 * color.G + 0.114 * color.B;
return brightness < BrightnessThreshold;
}
#endregion
}