-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathUtils.cs
More file actions
204 lines (192 loc) · 10.8 KB
/
Utils.cs
File metadata and controls
204 lines (192 loc) · 10.8 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
using Wacton.Unicolour.Datasets;
namespace Wacton.Unicolour.Example.Web;
internal static class Utils
{
internal static readonly Unicolour Dark = new("404046");
internal static readonly Unicolour Light = new("e8e8ff");
internal static string ToCss(Unicolour colour, double alpha)
{
if (HasConversionError(colour))
{
return "transparent";
}
var (r, g, b) = colour.Rgb.Clipped;
return $"rgb({r * 100}% {g * 100}% {b * 100}% / {alpha}%)";
}
internal static bool HasConversionError(Unicolour colour)
{
// if the clipped RGB has no hex value, RGB is invalid (likely a NaN during conversion)
return colour.Rgb.Byte255.Clipped.Hex == "-";
}
internal static string TextOnColourCssClass(Unicolour colour)
{
if (HasConversionError(colour)) return "light-text-with-contrast";
var inGamut = colour.MapToRgbGamut(GamutMap.RgbClipping);
return inGamut.Contrast(Light) > inGamut.Contrast(Dark)
? "light-text-with-contrast"
: "dark-text-with-contrast";
}
internal static readonly Dictionary<ColourSpace, Range[]> SpaceToRange = new()
{
{ ColourSpace.Rgb255, [new(0, 255), new(0, 255), new(0, 255)] },
{ ColourSpace.Rgb, [new(0, 1), new(0, 1), new(0, 1)] },
{ ColourSpace.RgbLinear, [new(0, 1), new(0, 1), new(0, 1)] },
{ ColourSpace.Hsb, [new(0, 360), new(0, 1), new(0, 1)] },
{ ColourSpace.Hsl, [new(0, 360), new(0, 1), new(0, 1)] },
{ ColourSpace.Hwb, [new(0, 360), new(0, 1), new(0, 1)] },
{ ColourSpace.Hsi, [new(0, 360), new(0, 1), new(0, 1)] },
{ ColourSpace.Xyz, [new(0, 1.1), new(0, 1.1), new(0, 1.1)] },
{ ColourSpace.Xyy, [new(0, 1), new(0, 1), new(0, 1)] },
{ ColourSpace.Wxy, [new(360, 700), new(0, 1), new(0, 1)] },
{ ColourSpace.Lab, [new(0, 100), new(-128, 128), new(-128, 128)] },
{ ColourSpace.Lchab, [new(0, 100), new(0, 230), new(0, 360)] },
{ ColourSpace.Luv, [new(0, 100), new(-100, 100), new(-100, 100)] },
{ ColourSpace.Lchuv, [new(0, 100), new(0, 230), new(0, 360)] },
{ ColourSpace.Hsluv, [new(0, 360), new(0, 100), new(0, 100)] },
{ ColourSpace.Hpluv, [new(0, 360), new(0, 100), new(0, 100)] },
{ ColourSpace.Ypbpr, [new(0, 1), new(-0.5, 0.5), new(-0.5, 0.5)] },
{ ColourSpace.Ycbcr, [new(0, 255), new(0, 255), new(0, 255)] },
{ ColourSpace.Ycgco, [new(0, 1), new(-0.5, 0.5), new(-0.5, 0.5)] },
{ ColourSpace.Yuv, [new(0, 1), new(-0.44, 0.44), new(-0.62, 0.62)] },
{ ColourSpace.Yiq, [new(0, 1), new(-0.60, 0.60), new(-0.53, 0.53)] },
{ ColourSpace.Ydbdr, [new(0, 1), new(-1.333, 1.333), new(-1.333, 1.333)] },
{ ColourSpace.Tsl, [new(0, 360), new(0, 1), new(0, 1)] },
{ ColourSpace.Xyb, [new(-0.03, 0.03), new(0, 1.0), new(-0.4, 0.4)] },
{ ColourSpace.Lms, [new(0, 1), new(0, 1), new(0, 1)] },
{ ColourSpace.Ipt, [new(0, 1), new(-0.75, 0.75), new(-0.75, 0.75)] },
{ ColourSpace.Ictcp, [new(0, 0.6), new(-0.3, 0.3), new(-0.3, 0.3)] },
{ ColourSpace.Jzazbz, [new(0, 0.25), new(-0.2, 0.2), new(-0.2, 0.2)] },
{ ColourSpace.Jzczhz, [new(0, 0.25), new(0, 0.2), new(0, 360)] },
{ ColourSpace.Oklab, [new(0, 1), new(-0.5, 0.5), new(-0.5, 0.5)] },
{ ColourSpace.Oklch, [new(0, 1), new(0, 0.5), new(0, 360)] },
{ ColourSpace.Okhsv, [new(0, 360), new(0, 1), new(0, 1)] },
{ ColourSpace.Okhsl, [new(0, 360), new(0, 1), new(0, 1)] },
{ ColourSpace.Okhwb, [new(0, 360), new(0, 1), new(0, 1)] },
{ ColourSpace.Oklrab, [new(0, 1), new(-0.5, 0.5), new(-0.5, 0.5)] },
{ ColourSpace.Oklrch, [new(0, 1), new(0, 0.5), new(0, 360)] },
{ ColourSpace.Cam02, [new(0, 100), new(-50, 50), new(-50, 50)] },
{ ColourSpace.Cam16, [new(0, 100), new(-50, 50), new(-50, 50)] },
{ ColourSpace.Hct, [new(0, 360), new(0, 120), new(0, 100)] },
{ ColourSpace.Munsell, [new(0, 360), new(1, 10), new(0, 26)] }
};
internal static readonly Dictionary<ColourSpace, string[]> SpaceToAxes = new()
{
{ ColourSpace.Rgb255, ["R", "G", "B"] },
{ ColourSpace.Rgb, ["R", "G", "B"] },
{ ColourSpace.RgbLinear, ["R", "G", "B"] },
{ ColourSpace.Hsb, ["H", "S", "B"] },
{ ColourSpace.Hsl, ["H", "S", "L"] },
{ ColourSpace.Hwb, ["H", "W", "B"] },
{ ColourSpace.Hsi, ["H", "S", "I"] },
{ ColourSpace.Xyz, ["X", "Y", "Z"] },
{ ColourSpace.Xyy, ["x", "y", "Y"] },
{ ColourSpace.Wxy, ["W", "X", "Y"] },
{ ColourSpace.Lab, ["L", "A", "B"] },
{ ColourSpace.Lchab, ["L", "C", "H"] },
{ ColourSpace.Luv, ["L", "U", "V"] },
{ ColourSpace.Lchuv, ["L", "C", "H"] },
{ ColourSpace.Hsluv, ["H", "S", "L"] },
{ ColourSpace.Hpluv, ["H", "S", "L"] },
{ ColourSpace.Ypbpr, ["Y", "Pb", "Pr"] },
{ ColourSpace.Ycbcr, ["Y", "Cb", "Cr"] },
{ ColourSpace.Ycgco, ["Y", "Cg", "Co"] },
{ ColourSpace.Yuv, ["Y", "U", "V"] },
{ ColourSpace.Yiq, ["Y", "I", "Q"] },
{ ColourSpace.Ydbdr, ["Y", "Db", "Dr"] },
{ ColourSpace.Tsl, ["T", "S", "L"] },
{ ColourSpace.Xyb, ["X", "Y", "B"] },
{ ColourSpace.Lms, ["L", "M", "S"] },
{ ColourSpace.Ipt, ["I", "P", "T"] },
{ ColourSpace.Ictcp, ["I", "Ct", "Cp"] },
{ ColourSpace.Jzazbz, ["J", "A", "B"] },
{ ColourSpace.Jzczhz, ["J", "C", "H"] },
{ ColourSpace.Oklab, ["L", "A", "B"] },
{ ColourSpace.Oklch, ["L", "C", "H"] },
{ ColourSpace.Okhsv, ["H", "S", "V"] },
{ ColourSpace.Okhsl, ["H", "S", "L"] },
{ ColourSpace.Okhwb, ["H", "W", "B"] },
{ ColourSpace.Oklrab, ["L", "A", "B"] },
{ ColourSpace.Oklrch, ["L", "C", "H"] },
{ ColourSpace.Cam02, ["J", "A", "B"] },
{ ColourSpace.Cam16, ["J", "A", "B"] },
{ ColourSpace.Hct, ["H", "C", "T"] },
{ ColourSpace.Munsell, ["H", "V", "C"] }
};
internal static readonly Dictionary<Pigment, string> PigmentToName = new()
{
{ ArtistPaint.BoneBlack, "Bone Black" },
{ ArtistPaint.TitaniumWhite, "Titanium White" },
{ ArtistPaint.BismuthVanadateYellow, "Bismuth Vanadate Yellow" },
{ ArtistPaint.HansaYellowOpaque, "Hansa Yellow" },
{ ArtistPaint.DiarylideYellow, "Diarylide Yellow" },
{ ArtistPaint.CadmiumOrange, "Cadmium Orange" },
{ ArtistPaint.PyrroleOrange, "Pyrrole Orange" },
{ ArtistPaint.CadmiumRedLight, "Cadmium Red Light" },
{ ArtistPaint.PyrroleRed, "Pyrrole Red" },
{ ArtistPaint.QuinacridoneRed, "Quinacridone Red" },
{ ArtistPaint.QuinacridoneMagenta, "Quinacridone Magenta" },
{ ArtistPaint.DioxazinePurple, "Dioxazine Purple" },
{ ArtistPaint.PhthaloBlueRedShade, "Phthalo Blue (Red Shade)" },
{ ArtistPaint.PhthaloBlueGreenShade, "Phthalo Blue (Green Shade)" },
{ ArtistPaint.UltramarineBlue, "Ultramarine Blue" },
{ ArtistPaint.CobaltBlue, "Cobalt Blue" },
{ ArtistPaint.CeruleanBlueChromium, "Cerulean Blue Chromium" },
{ ArtistPaint.PhthaloGreenBlueShade, "Phthalo Green (Blue Shade)" },
{ ArtistPaint.PhthaloGreenYellowShade, "Phthalo Green (Yellow Shade)" }
};
internal static readonly Dictionary<Pigment, Unicolour> PigmentToColour = new()
{
{ ArtistPaint.BoneBlack, GetSinglePigmentColour(ArtistPaint.BoneBlack) },
{ ArtistPaint.TitaniumWhite, GetSinglePigmentColour(ArtistPaint.TitaniumWhite) },
{ ArtistPaint.BismuthVanadateYellow, GetSinglePigmentColour(ArtistPaint.BismuthVanadateYellow) },
{ ArtistPaint.HansaYellowOpaque, GetSinglePigmentColour(ArtistPaint.HansaYellowOpaque) },
{ ArtistPaint.DiarylideYellow, GetSinglePigmentColour(ArtistPaint.DiarylideYellow) },
{ ArtistPaint.CadmiumOrange, GetSinglePigmentColour(ArtistPaint.CadmiumOrange) },
{ ArtistPaint.PyrroleOrange, GetSinglePigmentColour(ArtistPaint.PyrroleOrange) },
{ ArtistPaint.CadmiumRedLight, GetSinglePigmentColour(ArtistPaint.CadmiumRedLight) },
{ ArtistPaint.PyrroleRed, GetSinglePigmentColour(ArtistPaint.PyrroleRed) },
{ ArtistPaint.QuinacridoneRed, GetSinglePigmentColour(ArtistPaint.QuinacridoneRed) },
{ ArtistPaint.QuinacridoneMagenta, GetSinglePigmentColour(ArtistPaint.QuinacridoneMagenta) },
{ ArtistPaint.DioxazinePurple, GetSinglePigmentColour(ArtistPaint.DioxazinePurple) },
{ ArtistPaint.PhthaloBlueRedShade, GetSinglePigmentColour(ArtistPaint.PhthaloBlueRedShade) },
{ ArtistPaint.PhthaloBlueGreenShade, GetSinglePigmentColour(ArtistPaint.PhthaloBlueGreenShade) },
{ ArtistPaint.UltramarineBlue, GetSinglePigmentColour(ArtistPaint.UltramarineBlue) },
{ ArtistPaint.CobaltBlue, GetSinglePigmentColour(ArtistPaint.CobaltBlue) },
{ ArtistPaint.CeruleanBlueChromium, GetSinglePigmentColour(ArtistPaint.CeruleanBlueChromium) },
{ ArtistPaint.PhthaloGreenBlueShade, GetSinglePigmentColour(ArtistPaint.PhthaloGreenBlueShade) },
{ ArtistPaint.PhthaloGreenYellowShade, GetSinglePigmentColour(ArtistPaint.PhthaloGreenYellowShade) }
};
private static Unicolour GetSinglePigmentColour(Pigment pigment) => new Unicolour([pigment], [1]).MapToRgbGamut(GamutMap.RgbClipping);
internal static string[] IccAxes(string space)
{
return space switch
{
"XYZ " => ["X", "Y", "Z"],
"Lab " => ["L", "A", "B"],
"Luv " => ["L", "U", "V"],
"YCbr" => ["Y", "Cb", "Cr"],
"Yxy " => ["Y", "x", "y"],
"RGB " => ["R", "G", "B"],
"GRAY" => ["K"],
"HSV " => ["H", "S", "V"],
"HLS " => ["H", "L", "S"],
"CMYK" => ["C", "M", "Y", "K"],
"CMY " => ["C", "M", "Y"],
"2CLR" => ["C1", "C2"],
"3CLR" => ["C1", "C2", "C3"],
"4CLR" => ["C1", "C2", "C3", "C4"],
"5CLR" => ["C1", "C2", "C3", "C4", "C5"],
"6CLR" => ["C1", "C2", "C3", "C4", "C5", "C6"],
"7CLR" => ["C1", "C2", "C3", "C4", "C5", "C6", "C7"],
"8CLR" => ["C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8"],
"9CLR" => ["C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9"],
"ACLR" => ["C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "C10"],
"BCLR" => ["C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "C10", "C11"],
"CCLR" => ["C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "C10", "C11", "C12"],
"DCLR" => ["C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "C10", "C11", "C12", "C13"],
"ECLR" => ["C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "C10", "C11", "C12", "C13", "C14"],
"FCLR" => ["C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "C10", "C11", "C12", "C13", "C14", "C15"],
_ => []
};
}
}