-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
ColorExtensions.cs
219 lines (171 loc) · 6.08 KB
/
ColorExtensions.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
using System;
using Xamarin.Forms;
namespace DeveloperSample.Core.Helpers
{
public static class ColorExtensions
{
#region ToString
public static string ToRgbString(this Color c)
{
return $"RGB({c.GetByteRed()},{c.GetByteGreen()},{c.GetByteBlue()})";
}
public static string ToRgbaString(this Color c)
{
return $"RGBA({c.GetByteRed()}, {c.GetByteGreen()}, {c.GetByteBlue()}, {c.GetByteAlpha()})";
}
public static string ToHexRgbString(this Color c)
{
return $"#{c.GetHexR():X2}{c.GetHexG():X2}{c.GetHexB():X2}";
}
public static string ToHexRgbaString(this Color c)
{
return $"#{c.GetHexR():X2}{c.GetHexG():X2}{c.GetHexB():X2}{c.GetHexA():X2}";
}
public static string ToCmyString(this Color c)
{
return $"CMYK({c.GetCyan():P},{c.GetMagenta():P},{c.GetYellow():P})";
}
public static string ToCmykString(this Color c)
{
return $"CMYK({c.GetCyan():P},{c.GetMagenta():P},{c.GetYellow():P},{c.GetBlackKey():P})";
}
#endregion
#region Setters
public static Color WithRed(this Color baseColor, double newR)
{
return Color.FromRgba(newR, baseColor.G, baseColor.B, baseColor.A);
}
public static Color WithGreen(this Color baseColor, double newG)
{
return Color.FromRgba(baseColor.R, newG, baseColor.B, baseColor.A);
}
public static Color WithBlue(this Color baseColor, double newB)
{
return Color.FromRgba(baseColor.R, baseColor.G, newB, baseColor.A);
}
public static Color WithAlpha(this Color baseColor, double newA)
{
return Color.FromRgba(baseColor.R, baseColor.G, baseColor.B, newA);
}
public static Color WithCyan(this Color baseColor, double newC)
{
return Color.FromRgba(
(1 - newC) * (1 - baseColor.GetBlackKey()),
(1 - baseColor.GetMagenta()) * (1 - baseColor.GetBlackKey()),
(1 - baseColor.GetYellow()) * (1 - baseColor.GetBlackKey()),
baseColor.A);
}
public static Color WithMagenta(this Color baseColor, double newM)
{
return Color.FromRgba(
(1 - baseColor.GetCyan()) * (1 - baseColor.GetBlackKey()),
(1 - newM) * (1 - baseColor.GetBlackKey()),
(1 - baseColor.GetYellow()) * (1 - baseColor.GetBlackKey()),
baseColor.A);
}
public static Color WithYellow(this Color baseColor, double newY)
{
return Color.FromRgba(
(1 - baseColor.GetCyan()) * (1 - baseColor.GetBlackKey()),
(1 - baseColor.GetMagenta()) * (1 - baseColor.GetBlackKey()),
(1 - newY) * (1 - baseColor.GetBlackKey()),
baseColor.A);
}
#endregion
#region Getters Byte
public static byte GetByteRed(this Color c)
{
return NumericExtensions.EnsureByte(c.R * 255);
}
public static byte GetByteGreen(this Color c)
{
return NumericExtensions.EnsureByte(c.G * 255);
}
public static byte GetByteBlue(this Color c)
{
return NumericExtensions.EnsureByte(c.B * 255);
}
public static byte GetByteAlpha(this Color c)
{
return NumericExtensions.EnsureByte(c.A * 255);
}
public static byte GetByteCyan(this Color c)
{
return NumericExtensions.EnsureByte((1 - c.R) * 255);
}
public static byte GetByteMagenta(this Color c)
{
return NumericExtensions.EnsureByte((1 - c.G) * 255);
}
public static byte GetByteYellow(this Color c)
{
return NumericExtensions.EnsureByte((1 - c.B) * 255);
}
#endregion
#region Getters Hex
public static int GetHexR(this Color c)
{
return c.GetByteRed();
}
public static int GetHexG(this Color c)
{
return c.GetByteGreen();
}
public static int GetHexB(this Color c)
{
return c.GetByteBlue();
}
public static int GetHexA(this Color c)
{
return c.GetByteAlpha();
}
#endregion
#region Getters double
public static double GetBlackKey(this Color c)
{
return 1 - Math.Max(Math.Max(c.R, c.G), c.B);
}
public static double GetCyan(this Color c)
{
return (1 - c.R - c.GetBlackKey()) / (1 - c.GetBlackKey());
}
public static double GetMagenta(this Color c)
{
return (1 - c.G - c.GetBlackKey()) / (1 - c.GetBlackKey());
}
public static double GetYellow(this Color c)
{
return (1 - c.B - c.GetBlackKey()) / (1 - c.GetBlackKey());
}
#endregion
#region Converters
public static Color Inverse(this Color baseColor)
{
return Color.FromRgb(1 - baseColor.R, 1 - baseColor.G, 1 - baseColor.B);
}
public static Color ToBlackOrWhite(this Color baseColor)
{
return baseColor.IsDark() ? Color.Black : Color.White;
}
public static Color ToBlackOrWhiteForText(this Color baseColor)
{
return baseColor.IsDarkForTheEye() ? Color.White : Color.Black;
}
public static Color ToGrayScale(this Color baseColor)
{
var avg = (baseColor.R + baseColor.B + baseColor.G) / 3;
return Color.FromRgb(avg, avg, avg);
}
#endregion
#region Booleans
public static bool IsDarkForTheEye(this Color c)
{
return c.GetHexR() * 0.299 + c.GetHexG() * 0.587 + c.GetHexB() * 0.114 <= 186;
}
public static bool IsDark(this Color c)
{
return c.GetHexR() + c.GetHexG() + c.GetHexB() <= 127 * 3;
}
#endregion
}
}