-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathHSBColor.cs
442 lines (362 loc) · 14 KB
/
HSBColor.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
using System;
using System.Globalization;
using System.Runtime.Serialization;
namespace UnityEngine
{
[Serializable]
public readonly struct HSBColor : IEquatableReadOnlyStruct<HSBColor>, IFormattable, ISerializable
{
public static HSBColor Cyan { get; } = Color.cyan;
public static HSBColor Clear { get; } = Color.clear;
public static HSBColor Grey { get; } = Color.grey;
public static HSBColor Gray { get; } = Color.gray;
public static HSBColor Magenta { get; } = Color.magenta;
public static HSBColor Red { get; } = Color.red;
public static HSBColor Yellow { get; } = Color.yellow;
public static HSBColor Black { get; } = Color.black;
public static HSBColor White { get; } = Color.white;
public static HSBColor Green { get; } = Color.green;
public static HSBColor Blue { get; } = Color.blue;
/// <summary>
/// Hue
/// </summary>
public readonly float H;
/// <summary>
/// Saturation
/// </summary>
public readonly float S;
/// <summary>
/// Brightness
/// </summary>
public readonly float B;
/// <summary>
/// Alpha
/// </summary>
public readonly float A;
public float this[int index]
{
get
{
switch (index)
{
case 0: return this.H;
case 1: return this.S;
case 2: return this.B;
case 3: return this.A;
default: throw new IndexOutOfRangeException();
}
}
}
public HSBColor(float h, float s, float b)
{
this.H = h;
this.S = s;
this.B = b;
this.A = 1f;
}
public HSBColor(float h, float s, float b, float a)
{
this.H = h;
this.S = s;
this.B = b;
this.A = a;
}
public HSBColor(in Color rgb)
{
RGBToHSB(rgb, out this.H, out this.S, out this.B, out this.A);
}
public void Deconstruct(out float h, out float s, out float b)
{
h = this.H;
s = this.S;
b = this.B;
}
public void Deconstruct(out float h, out float s, out float b, out float a)
{
h = this.H;
s = this.S;
b = this.B;
a = this.A;
}
public HSBColor With(float? H = null, float? S = null, float? B = null, float? A = null)
=> new HSBColor(
H ?? this.H,
S ?? this.S,
B ?? this.B,
A ?? this.A
);
public override int GetHashCode()
{
#if USE_SYSTEM_HASHCODE
return HashCode.Combine(this.H, this.S, this.B, this.A);
#endif
#pragma warning disable CS0162 // Unreachable code detected
unchecked
{
var hashCode = -1511096998;
hashCode = hashCode * -1521134295 + this.H.GetHashCode();
hashCode = hashCode * -1521134295 + this.S.GetHashCode();
hashCode = hashCode * -1521134295 + this.B.GetHashCode();
hashCode = hashCode * -1521134295 + this.A.GetHashCode();
return hashCode;
}
#pragma warning restore CS0162 // Unreachable code detected
}
public override bool Equals(object obj)
=> obj is HSBColor other && Equals(other);
public bool Equals(HSBColor other)
=> Mathf.Approximately(this.H, other.H) &&
Mathf.Approximately(this.S, other.S) &&
Mathf.Approximately(this.B, other.B) &&
Mathf.Approximately(this.A, other.A);
public bool Equals(in HSBColor other)
=> Mathf.Approximately(this.H, other.H) &&
Mathf.Approximately(this.S, other.S) &&
Mathf.Approximately(this.B, other.B) &&
Mathf.Approximately(this.A, other.A);
public override string ToString()
=> ToString(null, CultureInfo.InvariantCulture.NumberFormat);
public string ToString(string format)
=> ToString(format, CultureInfo.InvariantCulture.NumberFormat);
public string ToString(string format, IFormatProvider formatProvider)
{
if (string.IsNullOrEmpty(format))
format = "F3";
return string.Format("HSBA({0}, {1}, {2}, {3})",
this.H.ToString(format, formatProvider),
this.S.ToString(format, formatProvider),
this.B.ToString(format, formatProvider),
this.A.ToString(format, formatProvider)
);
}
private HSBColor(SerializationInfo info, StreamingContext context)
{
this.H = info.GetSingleOrDefault(nameof(this.H));
this.S = info.GetSingleOrDefault(nameof(this.S));
this.B = info.GetSingleOrDefault(nameof(this.B));
this.A = info.GetSingleOrDefault(nameof(this.A));
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue(nameof(this.H), this.H);
info.AddValue(nameof(this.S), this.S);
info.AddValue(nameof(this.B), this.B);
info.AddValue(nameof(this.A), this.A);
}
public static bool operator ==(in HSBColor lhs, in HSBColor rhs)
=> Mathf.Approximately(lhs.H, rhs.H) &&
Mathf.Approximately(lhs.S, rhs.S) &&
Mathf.Approximately(lhs.B, rhs.B) &&
Mathf.Approximately(lhs.A, rhs.A);
public static bool operator !=(in HSBColor lhs, in HSBColor rhs)
=> !Mathf.Approximately(lhs.H, rhs.H) ||
!Mathf.Approximately(lhs.S, rhs.S) ||
!Mathf.Approximately(lhs.B, rhs.B) ||
!Mathf.Approximately(lhs.A, rhs.A);
public static implicit operator Color(in HSBColor hsb)
=> HSBToRGB(hsb);
public static implicit operator HSBColor(in Color rgb)
=> RGBToHSB(rgb);
public static implicit operator HSBColor(in Vector4 v)
=> new HSBColor(v.x, v.y, v.z, v.w);
public static implicit operator Vector4(in HSBColor c)
=> new Vector4(c.H, c.S, c.B, c.A);
public static HSBColor operator +(in HSBColor lhs, in HSBColor rhs)
=> new HSBColor(lhs.H + rhs.H, lhs.S + rhs.S, lhs.B + rhs.B, lhs.A + rhs.A);
public static HSBColor operator -(in HSBColor lhs, in HSBColor rhs)
=> new HSBColor(lhs.H - rhs.H, lhs.S - rhs.S, lhs.B - rhs.B, lhs.A - rhs.A);
public static HSBColor operator *(in HSBColor lhs, in HSBColor rhs)
=> new HSBColor(lhs.H * rhs.H, lhs.S * rhs.S, lhs.B * rhs.B, lhs.A * rhs.A);
public static HSBColor operator *(in HSBColor lhs, float rhs)
=> new HSBColor(lhs.H * rhs, lhs.S * rhs, lhs.B * rhs, lhs.A * rhs);
public static HSBColor operator *(float lhs, in HSBColor rhs)
=> new HSBColor(lhs * rhs.H, lhs * rhs.S, lhs * rhs.B, lhs * rhs.A);
public static HSBColor operator *(in HSBColor lhs, in Vector4 rhs)
=> new HSBColor(lhs.H * rhs.x, lhs.S * rhs.y, lhs.B * rhs.z, lhs.A * rhs.w);
public static HSBColor operator *(in Vector4 lhs, in HSBColor rhs)
=> new HSBColor(rhs.H * lhs.x, rhs.S * lhs.y, rhs.B * lhs.z, rhs.A * lhs.w);
public static HSBColor operator /(in HSBColor lhs, float rhs)
=> new HSBColor(lhs.H / rhs, lhs.S / rhs, lhs.B / rhs, lhs.A / rhs);
public static HSBColor operator /(in HSBColor lhs, in Vector4 rhs)
=> new HSBColor(lhs.H / rhs.x, lhs.S / rhs.y, lhs.B / rhs.z, lhs.A / rhs.w);
public static HSBColor operator /(in HSBColor lhs, in HSBColor rhs)
=> new HSBColor(lhs.H / rhs.H, lhs.S / rhs.S, lhs.B / rhs.B, lhs.A / rhs.A);
private const float N1_d_N360 = 1f / 360f;
public static HSBColor RGBToHSB(in Color rgb)
{
RGBToHSB(rgb, out var h, out var s, out var b, out var a);
return new HSBColor(h, s, b, a);
}
public static HSBColor RGBToHSB(float r, float g, float b)
{
RGBToHSB(new Color(r, g, b, 1f), out var hh, out var ss, out var bb, out var aa);
return new HSBColor(hh, ss, bb, aa);
}
public static HSBColor RGBToHSB(float r, float g, float b, float a)
{
RGBToHSB(new Color(r, g, b, a), out var hh, out var ss, out var bb, out var aa);
return new HSBColor(hh, ss, bb, aa);
}
public static void RGBToHSB(in Color rgb, out float h, out float s, out float b)
=> RGBToHSB(rgb, out h, out s, out b, out var _);
public static void RGBToHSB(in Color rgb, out float h, out float s, out float b, out float a)
{
h = 0f;
s = 0f;
b = 0f;
a = rgb.a;
var red = rgb.r;
var green = rgb.g;
var blue = rgb.b;
var max = Mathf.Max(red, Mathf.Max(green, blue));
if (max <= 0f)
return;
var min = Mathf.Min(red, Mathf.Min(green, blue));
var dif = max - min;
if (max <= min)
{
h = 0f;
}
else
{
if (green == max)
{
h = (blue - red) / dif * 60f + 120f;
}
else if (blue == max)
{
h = (red - green) / dif * 60f + 240f;
}
else if (blue > green)
{
h = (green - blue) / dif * 60f + 360f;
}
else
{
h = (green - blue) / dif * 60f;
}
if (h < 0f)
{
h += 360f;
}
}
h *= N1_d_N360;
s = (dif / max) * 1f;
b = max;
}
public static Color HSBToRGB(in HSBColor hsb)
{
HSBToRGB(new HSBColor(hsb.H, hsb.S, hsb.B, hsb.A), out var r, out var g, out var b, out var a);
return new Color(r, g, b, a);
}
public static Color HSBToRGB(float h, float s, float b)
{
HSBToRGB(new HSBColor(h, s, b, 1f), out var rr, out var gg, out var bb, out var aa);
return new Color(rr, gg, bb, aa);
}
public static Color HSBToRGB(float h, float s, float b, float a)
{
HSBToRGB(new HSBColor(h, s, b, a), out var rr, out var gg, out var bb, out var aa);
return new Color(rr, gg, bb, aa);
}
public static void HSBToRGB(in HSBColor hsb, out float r, out float g, out float b)
=> HSBToRGB(hsb, out r, out g, out b, out var _);
public static void HSBToRGB(in HSBColor hsb, out float r, out float g, out float b, out float a)
{
r = hsb.B;
g = hsb.B;
b = hsb.B;
a = hsb.A;
if (hsb.S != 0)
{
var max = hsb.B;
var dif = hsb.B * hsb.S;
var min = hsb.B - dif;
var hue = hsb.H * 360f;
if (hue < 60f)
{
r = max;
g = hue * dif / 60f + min;
b = min;
}
else if (hue < 120f)
{
r = -(hue - 120f) * dif / 60f + min;
g = max;
b = min;
}
else if (hue < 180f)
{
r = min;
g = max;
b = (hue - 120f) * dif / 60f + min;
}
else if (hue < 240f)
{
r = min;
g = -(hue - 240f) * dif / 60f + min;
b = max;
}
else if (hue < 300f)
{
r = (hue - 240f) * dif / 60f + min;
g = min;
b = max;
}
else if (hue <= 360f)
{
r = max;
g = min;
b = -(hue - 360f) * dif / 60f + min;
}
else
{
r = 0f;
g = 0f;
b = 0f;
}
}
}
public static Color Lerp(in Color a, in Color b, float t)
=> Lerp(RGBToHSB(a), RGBToHSB(b), t);
public static HSBColor Lerp(in HSBColor a, in HSBColor b, float t)
{
float h, s;
// check special case black (hsb.b == 0): interpolate neither hue nor saturation!
// check special case grey (hsb.s == 0): don't interpolate hue!
if (a.B == 0f)
{
h = b.H;
s = b.S;
}
else if (b.B == 0f)
{
h = a.H;
s = a.S;
}
else
{
if (a.S == 0)
{
h = b.H;
}
else if (b.S == 0f)
{
h = a.H;
}
else
{
// works around bug with LerpAngle
var angle = Mathf.LerpAngle(a.H * 360f, b.H * 360f, t);
while (angle < 0f)
angle += 360f;
while (angle > 360f)
angle -= 360f;
h = angle / 360f;
}
s = Mathf.Lerp(a.S, b.S, t);
}
return new HSBColor(h, s, Mathf.Lerp(a.B, b.B, t), Mathf.Lerp(a.A, b.A, t));
}
}
}