forked from MaterialDesignInXAML/MaterialDesignInXamlToolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColorUtils.cs
More file actions
238 lines (210 loc) · 7.61 KB
/
ColorUtils.cs
File metadata and controls
238 lines (210 loc) · 7.61 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
namespace MaterialColorUtilities;
/// <summary>
/// Color science utilities and conversions not covered by HCT or CAM16.
/// </summary>
public static class ColorUtils
{
internal static readonly double[][] SRGB_TO_XYZ =
[
[0.41233895, 0.35762064, 0.18051042],
[0.2126, 0.7152, 0.0722],
[0.01932141, 0.11916382, 0.95034478]
];
internal static readonly double[][] XYZ_TO_SRGB =
[
[3.2413774792388685, -1.5376652402851851, -0.49885366846268053],
[-0.9691452513005321, 1.8758853451067872, 0.04156585616912061],
[0.05562093689691305, -0.20395524564742123, 1.0571799111220335]
];
internal static readonly double[] WHITE_POINT_D65 = [95.047, 100.0, 108.883];
/// <summary>
/// Converts a color from RGB components to ARGB format.
/// </summary>
public static int ArgbFromRgb(int red, int green, int blue) => (255 << 24) | ((red & 255) << 16) | ((green & 255) << 8) | (blue & 255);
/// <summary>
/// Converts a color in ARGB format to a <see cref="System.Windows.Media.Color"/>.
/// </summary>
public static System.Windows.Media.Color ColorFromArgb(int argb) =>
System.Windows.Media.Color.FromArgb(
(byte)AlphaFromArgb(argb),
(byte)RedFromArgb(argb),
(byte)GreenFromArgb(argb),
(byte)BlueFromArgb(argb));
/// <summary>
/// Converts a <see cref="System.Windows.Media.Color"/> to ARGB format.
/// </summary>
public static int ArgbFromColor(System.Windows.Media.Color color) =>
(color.A << 24) | (color.R << 16) | (color.G << 8) | color.B;
/// <summary>
/// Converts a color from linear RGB components to ARGB format.
/// </summary>
public static int ArgbFromLinrgb(double[] linrgb)
{
int r = Delinearized(linrgb[0]);
int g = Delinearized(linrgb[1]);
int b = Delinearized(linrgb[2]);
return ArgbFromRgb(r, g, b);
}
/// <summary>
/// Returns the alpha component of a color in ARGB format.
/// </summary>
public static int AlphaFromArgb(int argb) => (argb >> 24) & 255;
/// <summary>
/// Returns the red component of a color in ARGB format.
/// </summary>
public static int RedFromArgb(int argb) => (argb >> 16) & 255;
/// <summary>
/// Returns the green component of a color in ARGB format.
/// </summary>
public static int GreenFromArgb(int argb) => (argb >> 8) & 255;
/// <summary>
/// Returns the blue component of a color in ARGB format.
/// </summary>
public static int BlueFromArgb(int argb) => argb & 255;
/// <summary>
/// Returns whether a color in ARGB format is opaque.
/// </summary>
public static bool IsOpaque(int argb) => AlphaFromArgb(argb) >= 255;
/// <summary>
/// Converts a color from XYZ to ARGB.
/// </summary>
public static int ArgbFromXyz(double x, double y, double z)
{
double[][] m = XYZ_TO_SRGB;
double linearR = m[0][0] * x + m[0][1] * y + m[0][2] * z;
double linearG = m[1][0] * x + m[1][1] * y + m[1][2] * z;
double linearB = m[2][0] * x + m[2][1] * y + m[2][2] * z;
int r = Delinearized(linearR);
int g = Delinearized(linearG);
int b = Delinearized(linearB);
return ArgbFromRgb(r, g, b);
}
/// <summary>
/// Converts a color from ARGB to XYZ.
/// </summary>
public static double[] XyzFromArgb(int argb)
{
double r = Linearized(RedFromArgb(argb));
double g = Linearized(GreenFromArgb(argb));
double b = Linearized(BlueFromArgb(argb));
return MathUtils.MatrixMultiply([r, g, b], SRGB_TO_XYZ);
}
/// <summary>
/// Converts a color represented in Lab color space into an ARGB integer.
/// </summary>
public static int ArgbFromLab(double l, double a, double b)
{
double fy = (l + 16.0) / 116.0;
double fx = a / 500.0 + fy;
double fz = fy - b / 200.0;
double xNormalized = LabInvf(fx);
double yNormalized = LabInvf(fy);
double zNormalized = LabInvf(fz);
double x = xNormalized * WHITE_POINT_D65[0];
double y = yNormalized * WHITE_POINT_D65[1];
double z = zNormalized * WHITE_POINT_D65[2];
return ArgbFromXyz(x, y, z);
}
/// <summary>
/// Converts a color from ARGB representation to L*a*b* representation.
/// </summary>
public static double[] LabFromArgb(int argb)
{
double linearR = Linearized(RedFromArgb(argb));
double linearG = Linearized(GreenFromArgb(argb));
double linearB = Linearized(BlueFromArgb(argb));
double[][] m = SRGB_TO_XYZ;
double x = m[0][0] * linearR + m[0][1] * linearG + m[0][2] * linearB;
double y = m[1][0] * linearR + m[1][1] * linearG + m[1][2] * linearB;
double z = m[2][0] * linearR + m[2][1] * linearG + m[2][2] * linearB;
double xNorm = x / WHITE_POINT_D65[0];
double yNorm = y / WHITE_POINT_D65[1];
double zNorm = z / WHITE_POINT_D65[2];
double fx = LabF(xNorm);
double fy = LabF(yNorm);
double fz = LabF(zNorm);
double l = 116.0 * fy - 16.0;
double a = 500.0 * (fx - fy);
double b = 200.0 * (fy - fz);
return [l, a, b];
}
/// <summary>
/// Converts an L* value to an ARGB representation.
/// </summary>
public static int ArgbFromLstar(double lstar)
{
double y = YFromLstar(lstar);
int component = Delinearized(y);
return ArgbFromRgb(component, component, component);
}
/// <summary>
/// Computes the L* value of a color in ARGB representation.
/// </summary>
public static double LstarFromArgb(int argb)
{
double y = XyzFromArgb(argb)[1];
return 116.0 * LabF(y / 100.0) - 16.0;
}
/// <summary>
/// Converts an L* value to a Y value.
/// </summary>
public static double YFromLstar(double lstar) => 100.0 * LabInvf((lstar + 16.0) / 116.0);
/// <summary>
/// Converts a Y value to an L* value.
/// </summary>
public static double LstarFromY(double y) => LabF(y / 100.0) * 116.0 - 16.0;
/// <summary>
/// Linearizes an RGB component.
/// </summary>
public static double Linearized(int rgbComponent)
{
double normalized = rgbComponent / 255.0;
if (normalized <= 0.040449936)
{
return normalized / 12.92 * 100.0;
}
return Math.Pow((normalized + 0.055) / 1.055, 2.4) * 100.0;
}
/// <summary>
/// Delinearizes an RGB component.
/// </summary>
public static int Delinearized(double rgbComponent)
{
double normalized = rgbComponent / 100.0;
double delinearized;
if (normalized <= 0.0031308)
{
delinearized = normalized * 12.92;
}
else
{
delinearized = 1.055 * Math.Pow(normalized, 1.0 / 2.4) - 0.055;
}
return MathUtils.ClampInt(0, 255, (int)Math.Round(delinearized * 255.0));
}
/// <summary>
/// Returns the standard white point; white on a sunny day.
/// </summary>
public static double[] WhitePointD65() => WHITE_POINT_D65;
internal static double LabF(double t)
{
double e = 216.0 / 24389.0;
double kappa = 24389.0 / 27.0;
if (t > e)
{
return Math.Pow(t, 1.0 / 3.0);
}
return (kappa * t + 16.0) / 116.0;
}
internal static double LabInvf(double ft)
{
double e = 216.0 / 24389.0;
double kappa = 24389.0 / 27.0;
double ft3 = ft * ft * ft;
if (ft3 > e)
{
return ft3;
}
return (116.0 * ft - 16.0) / kappa;
}
}