-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMain.cs
324 lines (281 loc) · 10.4 KB
/
Main.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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
namespace Flow.Launcher.Plugin.ColorsPlugin;
public partial class ColorsPlugin : IPlugin, IPluginI18n
{
const int ImageSize = 32;
const char Separator = ';';
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.
DirectoryInfo _cacheDir;
PluginInitContext _context;
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.
[GeneratedRegex(@"^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$")]
private static partial Regex HexRegex();
[GeneratedRegex(@"^(?:(?:rgb)?(?:\s+|\s*\())?(\d{1,3}),\s?(\d{1,3}),\s?(\d{1,3})\)?$", RegexOptions.IgnoreCase)]
private static partial Regex RgbRegex();
[GeneratedRegex(@"^(?:(?:vec3)?(?:\s+|\s*\())?(\d*\.?\d+)\s*,\s*(\d*\.?\d+)\s*,\s*(\d*\.?\d+)\)?$", RegexOptions.IgnoreCase)]
private static partial Regex Vec3Regex();
[GeneratedRegex(@"^(?:(?:hsl)?(?:\s+|\s*\())?(\d*\.?\d+)\s*,\s*(\d*\.?\d+)%\s*,\s*(\d*\.?\d+)%\)?$", RegexOptions.IgnoreCase)]
private static partial Regex HslRegex();
static string ToString(Color color)
{
return $"{color.R}, {color.G}, {color.B}";
}
static string ToVec3String(Color color)
{
return string.Format("{0}, {1}, {2}",
(color.R / 255f).ToString("0.0###", CultureInfo.InvariantCulture),
(color.G / 255f).ToString("0.0###", CultureInfo.InvariantCulture),
(color.B / 255f).ToString("0.0###", CultureInfo.InvariantCulture));
}
static string ToHslString(Color color)
{
float h = color.GetHue();
float s = color.GetSaturation() * 100;
float l = color.GetBrightness() * 100;
return string.Format("{0:F1}, {1:F1}%, {2:F1}%",
h.ToString("0.#", CultureInfo.InvariantCulture),
s.ToString("0.#", CultureInfo.InvariantCulture),
l.ToString("0.#", CultureInfo.InvariantCulture));
}
// C# port of https://gist.github.com/mjackson/5311256
static float HueToRgb(float p, float q, float t)
{
if (t < 0) t += 1f;
if (t > 1) t -= 1f;
if (t < 1f / 6f ) return p + (q - p) * 6f * t;
if (t < 1f / 2f) return q;
if (t < 2f / 3f) return p + (q - p) * (2f / 3f - t) * 6f;
return p;
}
static Color ToColor(float h, float s, float l)
{
h /= 360f;
s /= 100f;
l /= 100f;
float r, g, b;
if (s == 0)
{
r = g = b = l; // achromatic
}
else
{
float q = l < 0.5f ? l * (1f + s) : l + s - l * s;
float p = 2 * l - q;
r = HueToRgb(p, q, h + 1f / 3f);
g = HueToRgb(p, q, h);
b = HueToRgb(p, q, h - 1f / 3f);
}
return Color.FromArgb(255, (int)MathF.Round(r * 255f),
(int)MathF.Round(g * 255f),
(int)MathF.Round(b * 255f));
}
static string GetImageFileName(Color color)
{
return ColorTranslator.ToHtml(color) + ".png";
}
string? GetCachedImagePath(Color color)
{
return _cacheDir.GetFiles(GetImageFileName(color), SearchOption.TopDirectoryOnly).FirstOrDefault()?.FullName;
}
string CreateImageCache(Color color)
{
using Bitmap bitmap = new(ImageSize, ImageSize);
using Graphics graphics = Graphics.FromImage(bitmap);
graphics.Clear(color);
string path = Path.Combine(_cacheDir.FullName, GetImageFileName(color));
bitmap.Save(path, ImageFormat.Png);
return path;
}
string GetColorImagePath(Color color)
{
if (GetCachedImagePath(color) is string cachePath)
{
return cachePath;
}
return CreateImageCache(color);
}
bool IsGlobalQuery()
{
return _context.CurrentPluginMetadata.ActionKeyword == "*";
}
public void Init(PluginInitContext context)
{
_context = context;
string path = Path.Combine(_context.CurrentPluginMetadata.PluginDirectory, "cache");
if (!Directory.Exists(path))
{
_cacheDir = Directory.CreateDirectory(path);
}
else
{
_cacheDir = new DirectoryInfo(path);
}
foreach (FileInfo file in _cacheDir.EnumerateFiles())
{
file.Delete();
}
}
public List<Result>? Query(Query query)
{
if (string.IsNullOrEmpty(query.Search))
{
return IsGlobalQuery() ? null : new List<Result>(1)
{
new()
{
Title = _context.API.GetTranslation("flowlauncher_plugin_color_plugin_multi_code_format"),
SubTitle = _context.API.GetTranslation("flowlauncher_plugin_color_plugin_multi_code_format_suggestion"),
IcoPath = _context.CurrentPluginMetadata.IcoPath,
Action = _ =>
{
_context.API.CopyToClipboard("99,197,34;(39,0,152)");
return true;
}
}
};
}
string[] inputs = query.Search.Split(Separator, StringSplitOptions.TrimEntries);
List<Result> results = new(inputs.Length * 4);
foreach (string input in inputs)
{
Color color = Color.Black;
bool isSet = false;
{
if (HexRegex().IsMatch(input))
{
color = ColorTranslator.FromHtml(input);
isSet = true;
}
}
if (!isSet)
{
Match rgbMatch = RgbRegex().Match(input);
if (rgbMatch.Success)
{
byte r = byte.Parse(rgbMatch.Groups[1].Value);
byte g = byte.Parse(rgbMatch.Groups[2].Value);
byte b = byte.Parse(rgbMatch.Groups[3].Value);
color = Color.FromArgb(255, r, g, b);
isSet = true;
}
}
if (!isSet)
{
Match vec3Match = Vec3Regex().Match(input);
if (vec3Match.Success)
{
float r = float.Parse(vec3Match.Groups[1].Value, CultureInfo.InvariantCulture);
float g = float.Parse(vec3Match.Groups[2].Value, CultureInfo.InvariantCulture);
float b = float.Parse(vec3Match.Groups[3].Value, CultureInfo.InvariantCulture);
if (r <= 1 && g <= 1 && b <= 1)
{
color = Color.FromArgb(255, (int)MathF.Round(r * 255f),
(int)MathF.Round(g * 255f),
(int)MathF.Round(b * 255f));
isSet = true;
}
}
}
if (!isSet)
{
Match hslMatch = HslRegex().Match(input);
if (hslMatch.Success)
{
float h = float.Parse(hslMatch.Groups[1].Value, CultureInfo.InvariantCulture);
float s = float.Parse(hslMatch.Groups[2].Value, CultureInfo.InvariantCulture);
float l = float.Parse(hslMatch.Groups[3].Value, CultureInfo.InvariantCulture);
if (h <= 360 && s <= 100 && l <= 100)
{
color = ToColor(h, s, l);
isSet = true;
}
}
}
if (!isSet)
{
return IsGlobalQuery() ? null : new List<Result>(1)
{
new()
{
Title = _context.API.GetTranslation("flowlauncher_plugin_color_plugin_name"),
SubTitle = _context.API.GetTranslation("flowlauncher_plugin_color_plugin_conversion_error"),
IcoPath = _context.CurrentPluginMetadata.IcoPath,
}
};
}
string imgPath = GetColorImagePath(color);
string hexStr = ColorTranslator.ToHtml(color);
results.Add(
new()
{
Title = hexStr,
SubTitle = "hex",
IcoPath = imgPath,
Action = _ =>
{
_context.API.CopyToClipboard(hexStr);
return true;
}
}
);
string rgbStr = ToString(color);
results.Add(
new()
{
Title = rgbStr,
SubTitle = "rgb",
IcoPath = imgPath,
Action = _ =>
{
_context.API.CopyToClipboard(rgbStr);
return true;
}
}
);
string vec3Str = ToVec3String(color);
results.Add(
new()
{
Title = vec3Str,
SubTitle = "vec3",
IcoPath = imgPath,
Action = _ =>
{
_context.API.CopyToClipboard(vec3Str);
return true;
}
}
);
string hslStr = ToHslString(color);
results.Add(
new()
{
Title = hslStr,
SubTitle = "hsl",
IcoPath = imgPath,
Action = _ =>
{
_context.API.CopyToClipboard(hslStr);
return true;
}
}
);
}
return results;
}
public string GetTranslatedPluginTitle()
{
return _context.API.GetTranslation("flowlauncher_plugin_color_plugin_name");
}
public string GetTranslatedPluginDescription()
{
return _context.API.GetTranslation("flowlauncher_plugin_color_plugin_description");
}
}