IDS peak ICV v1.3.0
Loading...
Searching...
No Matches
Color.cs
Go to the documentation of this file.
2
8public class Color
9{
32 public Color(byte red, byte green, byte blue, byte numBitsPerPixel = 8)
33 {
34 if (numBitsPerPixel is < 8 or > 16)
35 {
36 throw new IcvOutOfRangeException("Number of bits per pixel must be in the range [8, 16].");
37 }
38
39 ColorCode = CalculateColorCode(red, green, blue, numBitsPerPixel);
40 }
41
55 public Color(ColorConstant constant, byte numBitsPerPixel = 8)
56 {
57 var (r, g, b) = constant switch
58 {
59 ColorConstant.Black => (0, 0, 0),
60 ColorConstant.White => (255, 255, 255),
61 ColorConstant.Red => (255, 0, 0),
62 ColorConstant.Green => (0, 255, 0),
63 ColorConstant.Blue => (0, 0, 255),
64 ColorConstant.DimGray => (105, 105, 105),
65 ColorConstant.Gray => (128, 128, 128),
66 ColorConstant.LightGray => (211, 211, 211),
67 ColorConstant.Cyan => (0, 255, 255),
68 ColorConstant.Magenta => (255, 0, 255),
69 ColorConstant.Yellow => (255, 255, 0),
70 ColorConstant.MediumSlateBlue => (123, 104, 238),
71 ColorConstant.Coral => (255, 127, 80),
72 ColorConstant.SlateBlue => (106, 90, 205),
73 ColorConstant.SpringGreen => (0, 255, 127),
74 ColorConstant.OrangeRed => (255, 69, 0),
75 ColorConstant.DarkOliveGreen => (85, 107, 47),
76 ColorConstant.Pink => (255, 192, 203),
77 ColorConstant.CadetBlue => (95, 158, 160),
78 ColorConstant.Goldenrod => (218, 165, 32),
79 ColorConstant.Orange => (255, 165, 0),
80 ColorConstant.Gold => (255, 215, 0),
81 ColorConstant.ForestGreen => (34, 139, 34),
82 ColorConstant.CornflowerBlue => (100, 149, 237),
83 ColorConstant.Navy => (0, 0, 128),
84 ColorConstant.Turquoise => (64, 224, 208),
85 ColorConstant.DarkSlateBlue => (72, 61, 139),
86 ColorConstant.LightBlue => (173, 216, 230),
87 ColorConstant.IndianRed => (205, 92, 92),
88 ColorConstant.VioletRed => (208, 32, 144),
89 ColorConstant.LightSteelBlue => (176, 196, 222),
90 ColorConstant.MediumBlue => (0, 0, 205),
91 ColorConstant.Khaki => (240, 230, 140),
92 ColorConstant.Violet => (238, 130, 238),
93 ColorConstant.Firebrick => (178, 34, 34),
94 ColorConstant.MidnightBlue => (25, 25, 112),
95 _ => throw new IcvNotSupportedException("The given color constant is not supported.")
96 };
97
98 ColorCode = CalculateColorCode((byte) r, (byte) g, (byte) b, numBitsPerPixel);
99 }
100
105 public ulong ColorCode { get; protected set; }
106
111 public (ushort r, ushort g, ushort b) RGB =>
112 (
113 Red, Green, Blue
114 );
115
120 public ushort Red => (ushort) ((ColorCode >> 32) & 0xFFFF);
121
126 public ushort Green => (ushort) ((ColorCode >> 16) & 0xFFFF);
127
132 public ushort Blue => (ushort) (ColorCode & 0xFFFF);
133
138 public static IEqualityComparer<Color> Comparer { get; } = new ColorEqualityComparer();
139
162 private static ulong CalculateColorCode(byte red, byte green, byte blue,
163 byte numBitsPerPixel)
164 {
165 var max = ((ulong) 1 << numBitsPerPixel) - 1;
166 var r = red * max / 255;
167 var g = green * max / 255;
168 var b = blue * max / 255;
169
170 return (r << 32) | (g << 16) | b;
171 }
172
180 public override string ToString()
181 {
182 var (r, g, b) = RGB;
183 return $"Red: {r}, Green: {g}, Blue: {b}";
184 }
185
186 private sealed class ColorEqualityComparer : IEqualityComparer<Color>
187 {
188 public bool Equals(Color x, Color y)
189 {
190 if (ReferenceEquals(x, y))
191 {
192 return true;
193 }
194
195 if (ReferenceEquals(x, null))
196 {
197 return false;
198 }
199
200 if (ReferenceEquals(y, null))
201 {
202 return false;
203 }
204
205 return x.ColorCode == y.ColorCode;
206 }
207
208 public int GetHashCode(Color obj)
209 {
210 return obj.ColorCode.GetHashCode();
211 }
212 }
213}
Occurs when a requested operation is not implemented or supported (e.g., exporting to an unsupported ...
Definition Exception.cs:62
Occurs when a provided value falls outside the allowed valid range.
Definition Exception.cs:128
Class holding color codes.
Definition Color.cs:9
ushort Blue
The blue value.
Definition Color.cs:132
ulong ColorCode
The color code.
Definition Color.cs:105
ushort Green
The green value.
Definition Color.cs:126
Color(ColorConstant constant, byte numBitsPerPixel=8)
Creates a color from a color constant value.
Definition Color.cs:55
static IEqualityComparer< Color > Comparer
A default equality comparer for comparing Color data values.
Definition Color.cs:138
override string ToString()
Returns a string that represents the Color object.
Definition Color.cs:180
ushort r
The color as a tuple of red, green and blue values.
Definition Color.cs:111
Color(byte red, byte green, byte blue, byte numBitsPerPixel=8)
Creates a color from the given red, green, and blue values.
Definition Color.cs:32
ushort Red
The red value.
Definition Color.cs:120
ushort ushort ushort b RGB
Definition Color.cs:111
ColorConstant
Enum class for color constants.