-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParameterColorValue.cs
197 lines (151 loc) · 6.46 KB
/
ParameterColorValue.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Diagnostics;
#if !NET35
using System.Runtime.CompilerServices;
#endif
namespace ConnectorLib.JSON
{
///<remarks>This is a simplified version of System.Drawing.Color with no external dependencies.</remarks>
[DebuggerDisplay("{NameAndARGBValue}")]
#if NETSTANDARD1_3_OR_GREATER
[Serializable]
#endif
#if !NET35
[TypeForwardedFrom("System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
#endif
public readonly struct ParameterColorValue : IEquatable<ParameterColorValue>
{
public static readonly ParameterColorValue Empty = new();
// NOTE : The "zero" pattern (all members being 0) must represent
// : "not set". This allows "Color c;" to be correct.
//private const short StateKnownColorValid = 0x0001;
private const short StateARGBValueValid = 0x0002;
private const short StateValueMask = StateARGBValueValid;
private const short StateNameValid = 0x0008;
private const long NotDefinedValue = 0;
// Shift counts and bit masks for A, R, G, B components in ARGB mode
internal const int ARGBAlphaShift = 24;
internal const int ARGBRedShift = 16;
internal const int ARGBGreenShift = 8;
internal const int ARGBBlueShift = 0;
internal const uint ARGBAlphaMask = 0xFFu << ARGBAlphaShift;
internal const uint ARGBRedMask = 0xFFu << ARGBRedShift;
internal const uint ARGBGreenMask = 0xFFu << ARGBGreenShift;
internal const uint ARGBBlueMask = 0xFFu << ARGBBlueShift;
// Standard 32bit sRGB (ARGB)
private readonly long value; // Do not rename (binary serialization)
// State flags.
private readonly short state; // Do not rename (binary serialization)
private ParameterColorValue(long value, short state)
{
this.value = value;
this.state = state;
}
public byte R => unchecked((byte)(Value >> ARGBRedShift));
public byte G => unchecked((byte)(Value >> ARGBGreenShift));
public byte B => unchecked((byte)(Value >> ARGBBlueShift));
public byte A => unchecked((byte)(Value >> ARGBAlphaShift));
public bool IsEmpty => state == 0;
// Used for the [DebuggerDisplay]. Inlining in the attribute is possible, but
// against best practices as the current project language parses the string with
// language specific heuristics.
private long Value
{
get
{
if ((state & StateValueMask) != 0)
{
return value;
}
return NotDefinedValue;
}
}
private static ParameterColorValue FromArgb(uint argb) => new(argb, StateARGBValueValid);
public static ParameterColorValue FromArgb(int argb) => FromArgb(unchecked((uint)argb));
public static ParameterColorValue FromArgb(byte alpha, byte red, byte green, byte blue)
{
return FromArgb(
(uint)alpha << ARGBAlphaShift |
(uint)red << ARGBRedShift |
(uint)green << ARGBGreenShift |
(uint)blue << ARGBBlueShift
);
}
public static ParameterColorValue FromArgb(int alpha, ParameterColorValue baseColor)
{
return FromArgb(
(uint)alpha << ARGBAlphaShift |
(uint)baseColor.Value & ~ARGBAlphaMask
);
}
public static ParameterColorValue FromArgb(byte red, byte green, byte blue) => FromArgb(byte.MaxValue, red, green, blue);
#if !NET35
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
private void GetRgbValues(out byte r, out byte g, out byte b)
{
uint value = (uint)Value;
r = (byte)((value & ARGBRedMask) >> ARGBRedShift);
g = (byte)((value & ARGBGreenMask) >> ARGBGreenShift);
b = (byte)((value & ARGBBlueMask) >> ARGBBlueShift);
}
public float GetBrightness()
{
GetRgbValues(out byte r, out byte g, out byte b);
int min = Math.Min(Math.Min(r, g), b);
int max = Math.Max(Math.Max(r, g), b);
return (max + min) / (byte.MaxValue * 2f);
}
public float GetHue()
{
GetRgbValues(out byte r, out byte g, out byte b);
if (r == g && g == b)
return 0f;
int min = Math.Min(Math.Min(r, g), b);
int max = Math.Max(Math.Max(r, g), b);
float delta = max - min;
float hue;
if (r == max)
hue = (g - b) / delta;
else if (g == max)
hue = (b - r) / delta + 2f;
else
hue = (r - g) / delta + 4f;
hue *= 60f;
if (hue < 0f)
hue += 360f;
return hue;
}
public float GetSaturation()
{
GetRgbValues(out byte r, out byte g, out byte b);
if (r == g && g == b)
return 0f;
int min = Math.Min(Math.Min(r, g), b);
int max = Math.Max(Math.Max(r, g), b);
int div = max + min;
if (div > byte.MaxValue)
div = byte.MaxValue * 2 - max - min;
return (max - min) / (float)div;
}
public int ToArgb() => unchecked((int)Value);
public override string ToString()
{
if ((state & StateValueMask) != 0)
{
return nameof(ParameterColorValue) + " [A=" + A + ", R=" + R + ", G=" + G + ", B=" + B + "]";
}
return nameof(ParameterColorValue) + " [Empty]";
}
public static bool operator ==(ParameterColorValue left, ParameterColorValue right) =>
left.value == right.value
&& left.state == right.state;
public static bool operator !=(ParameterColorValue left, ParameterColorValue right) => !(left == right);
public override bool Equals(object? obj) => obj is ParameterColorValue other && Equals(other);
public bool Equals(ParameterColorValue other) => this == other;
public override int GetHashCode() => unchecked(value.GetHashCode() * 397 ^ state.GetHashCode());
}
}