• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

DomCR / ACadSharp / 16262122430

14 Jul 2025 08:38AM UTC coverage: 75.03% (-0.02%) from 75.054%
16262122430

Pull #710

github

web-flow
Merge a22ae33b2 into 9d213afc7
Pull Request #710: dimension reference points

5864 of 8589 branches covered (68.27%)

Branch coverage included in aggregate %.

32 of 47 new or added lines in 8 files covered. (68.09%)

23 existing lines in 2 files now uncovered.

23297 of 30277 relevant lines covered (76.95%)

81442.58 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.06
/src/ACadSharp/Color.cs
1
using CSUtilities.Converters;
2
using System;
3

4
namespace ACadSharp
5
{
6
        /// <summary>
7
        /// Represents an ACI color that also supports true color.
8
        /// </summary>
9
        public readonly struct Color : IEquatable<Color>
10
        {
11
                /// <summary>
12
                /// Defines a default blue color.
13
                /// </summary>
14
                public static Color Blue
15
                {
UNCOV
16
                        get { return new Color(5); }
×
17
                }
18

19
                /// <summary>
20
                /// Gets the ByBlock color.
21
                /// </summary>
22
                public static Color ByBlock
23
                {
24
                        get { return new Color((short)0); }
161,718✔
25
                }
26

27
                /// <summary>
28
                /// This is found in some header variables but is not valid for Entities or Objects
29
                /// </summary>
30
                public static Color ByEntity
31
                {
UNCOV
32
                        get { return new Color((short)257); }
×
33
                }
34

35
                /// <summary>
36
                /// Gets the ByLayer color.
37
                /// </summary>
38
                public static Color ByLayer
39
                {
40
                        get { return new Color((short)256); }
2,627,358✔
41
                }
42

43
                /// <summary>
44
                /// Defines a default cyan color.
45
                /// </summary>
46
                public static Color Cyan
47
                {
UNCOV
48
                        get { return new Color(4); }
×
49
                }
50

51
                /// <summary>
52
                /// Defines a default dark gray color.
53
                /// </summary>
54
                public static Color DarkGray
55
                {
UNCOV
56
                        get { return new Color(8); }
×
57
                }
58

59
                /// <summary>
60
                /// Defines a default white/black color.
61
                /// </summary>
62
                public static Color Default
63
                {
UNCOV
64
                        get { return new Color(7); }
×
65
                }
66

67
                /// <summary>
68
                /// Defines a default green color.
69
                /// </summary>
70
                public static Color Green
71
                {
UNCOV
72
                        get { return new Color(3); }
×
73
                }
74

75
                /// <summary>
76
                /// Defines a default light gray color.
77
                /// </summary>
78
                public static Color LightGray
79
                {
UNCOV
80
                        get { return new Color(9); }
×
81
                }
82

83
                /// <summary>
84
                /// Defines a default magenta color.
85
                /// </summary>
86
                public static Color Magenta
87
                {
UNCOV
88
                        get { return new Color(6); }
×
89
                }
90

91
                /// <summary>
92
                /// Defines a default red color.
93
                /// </summary>
94
                public static Color Red
95
                {
UNCOV
96
                        get { return new Color(1); }
×
97
                }
98

99
                /// <summary>
100
                /// Defines a default yellow color.
101
                /// </summary>
102
                public static Color Yellow
103
                {
UNCOV
104
                        get { return new Color(2); }
×
105
                }
106

107
                /// <summary>
108
                /// Blue component of the color
109
                /// </summary>
110
                public byte B { get { return this.GetRgb()[2]; } }
2,241✔
111

112
                /// <summary>
113
                /// Green component of the color
114
                /// </summary>
115
                public byte G { get { return this.GetRgb()[1]; } }
2,241✔
116

117
                /// <summary>
118
                /// Indexed color.  If the color is stored as a true color, returns -1;
119
                /// </summary>
120
                public short Index => this.IsTrueColor ? (short)-1 : (short)this._color;
46,700✔
121

122
                /// <summary>
123
                /// Defines if the color is defined by block.
124
                /// </summary>
125
                public bool IsByBlock
126
                {
127
                        get { return this.Index == 0; }
50,805✔
128
                }
129

130
                /// <summary>
131
                /// Defines if the color is defined by layer.
132
                /// </summary>
133
                public bool IsByLayer
134
                {
135
                        get { return this.Index == 256; }
51,387✔
136
                }
137

138
                /// <summary>
139
                /// True if the stored color is a true color.  False if the color is an indexed color.
140
                /// </summary>
141
                public bool IsTrueColor
142
                {
143
                        get
144
                        {
68,687✔
145
                                return this._color > 257 || this._color < 0;
68,687✔
146
                        }
68,687✔
147
                }
148

149
                /// <summary>
150
                /// Red component of the color
151
                /// </summary>
152
                public byte R { get { return this.GetRgb()[0]; } }
2,241✔
153

154
                /// <summary>
155
                /// True color.  If the color is stored as an indexed color, returns -1;
156
                /// </summary>
157
                public int TrueColor => this.IsTrueColor ? (int)(this._color ^ (1 << 30)) : -1;
2,313✔
158

159
                private const int _maxTrueColor = 0b0001_0000_0000_0000_0000_0000_0000;
160

161
                private const int _trueColorFlag = 0b0100_0000_0000_0000_0000_0000_0000_0000;
162

163
                private static readonly byte[][] _indexRgb = new byte[][]
1✔
164
                                                                                                                                                                                                                                                                                                                                                                                {
1✔
165
                        new byte[] { 0, 0, 0 },        //Dummy entry
1✔
166
                        new byte[] { 255, 0, 0 },
1✔
167
                        new byte[] { 255, 255, 0 },
1✔
168
                        new byte[] { 0, 255, 0 },
1✔
169
                        new byte[] { 0, 255, 255 },
1✔
170
                        new byte[] { 0, 0, 255 },
1✔
171
                        new byte[] { 255, 0, 255 },
1✔
172
                        new byte[] { 255, 255, 255 },
1✔
173
                        new byte[] { 128, 128, 128 },
1✔
174
                        new byte[] { 192, 192, 192 },
1✔
175
                        new byte[] { 255, 0, 0 },
1✔
176
                        new byte[] { 255, 127, 127 },
1✔
177
                        new byte[] { 165, 0, 0 },
1✔
178
                        new byte[] { 165, 82, 82 },
1✔
179
                        new byte[] { 127, 0, 0 },
1✔
180
                        new byte[] { 127, 63, 63 },
1✔
181
                        new byte[] { 76, 0, 0 },
1✔
182
                        new byte[] { 76, 38, 38 },
1✔
183
                        new byte[] { 38, 0, 0 },
1✔
184
                        new byte[] { 38, 19, 19 },
1✔
185
                        new byte[] { 255, 63, 0 },
1✔
186
                        new byte[] { 255, 159, 127 },
1✔
187
                        new byte[] { 165, 41, 0 },
1✔
188
                        new byte[] { 165, 103, 82 },
1✔
189
                        new byte[] { 127, 31, 0 },
1✔
190
                        new byte[] { 127, 79, 63 },
1✔
191
                        new byte[] { 76, 19, 0 },
1✔
192
                        new byte[] { 76, 47, 38 },
1✔
193
                        new byte[] { 38, 9, 0 },
1✔
194
                        new byte[] { 38, 28, 19 },
1✔
195
                        new byte[] { 255, 127, 0 },
1✔
196
                        new byte[] { 255, 191, 127 },
1✔
197
                        new byte[] { 165, 82, 0 },
1✔
198
                        new byte[] { 165, 124, 82 },
1✔
199
                        new byte[] { 127, 63, 0 },
1✔
200
                        new byte[] { 127, 95, 63 },
1✔
201
                        new byte[] { 76, 38, 0 },
1✔
202
                        new byte[] { 76, 57, 38 },
1✔
203
                        new byte[] { 38, 19, 0 },
1✔
204
                        new byte[] { 38, 28, 19 },
1✔
205
                        new byte[] { 255, 191, 0 },
1✔
206
                        new byte[] { 255, 223, 127 },
1✔
207
                        new byte[] { 165, 124, 0 },
1✔
208
                        new byte[] { 165, 145, 82 },
1✔
209
                        new byte[] { 127, 95, 0 },
1✔
210
                        new byte[] { 127, 111, 63 },
1✔
211
                        new byte[] { 76, 57, 0 },
1✔
212
                        new byte[] { 76, 66, 38 },
1✔
213
                        new byte[] { 38, 28, 0 },
1✔
214
                        new byte[] { 38, 33, 19 },
1✔
215
                        new byte[] { 255, 255, 0 },
1✔
216
                        new byte[] { 255, 255, 127 },
1✔
217
                        new byte[] { 165, 165, 0 },
1✔
218
                        new byte[] { 165, 165, 82 },
1✔
219
                        new byte[] { 127, 127, 0 },
1✔
220
                        new byte[] { 127, 127, 63 },
1✔
221
                        new byte[] { 76, 76, 0 },
1✔
222
                        new byte[] { 76, 76, 38 },
1✔
223
                        new byte[] { 38, 38, 0 },
1✔
224
                        new byte[] { 38, 38, 19 },
1✔
225
                        new byte[] { 191, 255, 0 },
1✔
226
                        new byte[] { 223, 255, 127 },
1✔
227
                        new byte[] { 124, 165, 0 },
1✔
228
                        new byte[] { 145, 165, 82 },
1✔
229
                        new byte[] { 95, 127, 0 },
1✔
230
                        new byte[] { 111, 127, 63 },
1✔
231
                        new byte[] { 57, 76, 0 },
1✔
232
                        new byte[] { 66, 76, 38 },
1✔
233
                        new byte[] { 28, 38, 0 },
1✔
234
                        new byte[] { 33, 38, 19 },
1✔
235
                        new byte[] { 127, 255, 0 },
1✔
236
                        new byte[] { 191, 255, 127 },
1✔
237
                        new byte[] { 82, 165, 0 },
1✔
238
                        new byte[] { 124, 165, 82 },
1✔
239
                        new byte[] { 63, 127, 0 },
1✔
240
                        new byte[] { 95, 127, 63 },
1✔
241
                        new byte[] { 38, 76, 0 },
1✔
242
                        new byte[] { 57, 76, 38 },
1✔
243
                        new byte[] { 19, 38, 0 },
1✔
244
                        new byte[] { 28, 38, 19 },
1✔
245
                        new byte[] { 63, 255, 0 },
1✔
246
                        new byte[] { 159, 255, 127 },
1✔
247
                        new byte[] { 41, 165, 0 },
1✔
248
                        new byte[] { 103, 165, 82 },
1✔
249
                        new byte[] { 31, 127, 0 },
1✔
250
                        new byte[] { 79, 127, 63 },
1✔
251
                        new byte[] { 19, 76, 0 },
1✔
252
                        new byte[] { 47, 76, 38 },
1✔
253
                        new byte[] { 9, 38, 0 },
1✔
254
                        new byte[] { 23, 38, 19 },
1✔
255
                        new byte[] { 0, 255, 0 },
1✔
256
                        new byte[] { 125, 255, 127 },
1✔
257
                        new byte[] { 0, 165, 0 },
1✔
258
                        new byte[] { 82, 165, 82 },
1✔
259
                        new byte[] { 0, 127, 0 },
1✔
260
                        new byte[] { 63, 127, 63 },
1✔
261
                        new byte[] { 0, 76, 0 },
1✔
262
                        new byte[] { 38, 76, 38 },
1✔
263
                        new byte[] { 0, 38, 0 },
1✔
264
                        new byte[] { 19, 38, 19 },
1✔
265
                        new byte[] { 0, 255, 63 },
1✔
266
                        new byte[] { 127, 255, 159 },
1✔
267
                        new byte[] { 0, 165, 41 },
1✔
268
                        new byte[] { 82, 165, 103 },
1✔
269
                        new byte[] { 0, 127, 31 },
1✔
270
                        new byte[] { 63, 127, 79 },
1✔
271
                        new byte[] { 0, 76, 19 },
1✔
272
                        new byte[] { 38, 76, 47 },
1✔
273
                        new byte[] { 0, 38, 9 },
1✔
274
                        new byte[] { 19, 88, 23 },
1✔
275
                        new byte[] { 0, 255, 127 },
1✔
276
                        new byte[] { 127, 255, 191 },
1✔
277
                        new byte[] { 0, 165, 82 },
1✔
278
                        new byte[] { 82, 165, 124 },
1✔
279
                        new byte[] { 0, 127, 63 },
1✔
280
                        new byte[] { 63, 127, 95 },
1✔
281
                        new byte[] { 0, 76, 38 },
1✔
282
                        new byte[] { 38, 76, 57 },
1✔
283
                        new byte[] { 0, 38, 19 },
1✔
284
                        new byte[] { 19, 88, 28 },
1✔
285
                        new byte[] { 0, 255, 191 },
1✔
286
                        new byte[] { 127, 255, 223 },
1✔
287
                        new byte[] { 0, 165, 124 },
1✔
288
                        new byte[] { 82, 165, 145 },
1✔
289
                        new byte[] { 0, 127, 95 },
1✔
290
                        new byte[] { 63, 127, 111 },
1✔
291
                        new byte[] { 0, 76, 57 },
1✔
292
                        new byte[] { 38, 76, 66 },
1✔
293
                        new byte[] { 0, 38, 28 },
1✔
294
                        new byte[] { 19, 88, 88 },
1✔
295
                        new byte[] { 0, 255, 255 },
1✔
296
                        new byte[] { 127, 255, 255 },
1✔
297
                        new byte[] { 0, 165, 165 },
1✔
298
                        new byte[] { 82, 165, 165 },
1✔
299
                        new byte[] { 0, 127, 127 },
1✔
300
                        new byte[] { 63, 127, 127 },
1✔
301
                        new byte[] { 0, 76, 76 },
1✔
302
                        new byte[] { 38, 76, 76 },
1✔
303
                        new byte[] { 0, 38, 38 },
1✔
304
                        new byte[] { 19, 88, 88 },
1✔
305
                        new byte[] { 0, 191, 255 },
1✔
306
                        new byte[] { 127, 223, 255 },
1✔
307
                        new byte[] { 0, 124, 165 },
1✔
308
                        new byte[] { 82, 145, 165 },
1✔
309
                        new byte[] { 0, 95, 127 },
1✔
310
                        new byte[] { 63, 111, 217 },
1✔
311
                        new byte[] { 0, 57, 76 },
1✔
312
                        new byte[] { 38, 66, 126 },
1✔
313
                        new byte[] { 0, 28, 38 },
1✔
314
                        new byte[] { 19, 88, 88 },
1✔
315
                        new byte[] { 0, 127, 255 },
1✔
316
                        new byte[] { 127, 191, 255 },
1✔
317
                        new byte[] { 0, 82, 165 },
1✔
318
                        new byte[] { 82, 124, 165 },
1✔
319
                        new byte[] { 0, 63, 127 },
1✔
320
                        new byte[] { 63, 95, 127 },
1✔
321
                        new byte[] { 0, 38, 76 },
1✔
322
                        new byte[] { 38, 57, 126 },
1✔
323
                        new byte[] { 0, 19, 38 },
1✔
324
                        new byte[] { 19, 28, 88 },
1✔
325
                        new byte[] { 0, 63, 255 },
1✔
326
                        new byte[] { 127, 159, 255 },
1✔
327
                        new byte[] { 0, 41, 165 },
1✔
328
                        new byte[] { 82, 103, 165 },
1✔
329
                        new byte[] { 0, 31, 127 },
1✔
330
                        new byte[] { 63, 79, 127 },
1✔
331
                        new byte[] { 0, 19, 76 },
1✔
332
                        new byte[] { 38, 47, 126 },
1✔
333
                        new byte[] { 0, 9, 38 },
1✔
334
                        new byte[] { 19, 23, 88 },
1✔
335
                        new byte[] { 0, 0, 255 },
1✔
336
                        new byte[] { 127, 127, 255 },
1✔
337
                        new byte[] { 0, 0, 165 },
1✔
338
                        new byte[] { 82, 82, 165 },
1✔
339
                        new byte[] { 0, 0, 127 },
1✔
340
                        new byte[] { 63, 63, 127 },
1✔
341
                        new byte[] { 0, 0, 76 },
1✔
342
                        new byte[] { 38, 38, 126 },
1✔
343
                        new byte[] { 0, 0, 38 },
1✔
344
                        new byte[] { 19, 19, 88 },
1✔
345
                        new byte[] { 63, 0, 255 },
1✔
346
                        new byte[] { 159, 127, 255 },
1✔
347
                        new byte[] { 41, 0, 165 },
1✔
348
                        new byte[] { 103, 82, 165 },
1✔
349
                        new byte[] { 31, 0, 127 },
1✔
350
                        new byte[] { 79, 63, 127 },
1✔
351
                        new byte[] { 19, 0, 76 },
1✔
352
                        new byte[] { 47, 38, 126 },
1✔
353
                        new byte[] { 9, 0, 38 },
1✔
354
                        new byte[] { 23, 19, 88 },
1✔
355
                        new byte[] { 127, 0, 255 },
1✔
356
                        new byte[] { 191, 127, 255 },
1✔
357
                        new byte[] { 165, 0, 82 },
1✔
358
                        new byte[] { 124, 82, 165 },
1✔
359
                        new byte[] { 63, 0, 127 },
1✔
360
                        new byte[] { 95, 63, 127 },
1✔
361
                        new byte[] { 38, 0, 76 },
1✔
362
                        new byte[] { 57, 38, 126 },
1✔
363
                        new byte[] { 19, 0, 38 },
1✔
364
                        new byte[] { 28, 19, 88 },
1✔
365
                        new byte[] { 191, 0, 255 },
1✔
366
                        new byte[] { 223, 127, 255 },
1✔
367
                        new byte[] { 124, 0, 165 },
1✔
368
                        new byte[] { 142, 82, 165 },
1✔
369
                        new byte[] { 95, 0, 127 },
1✔
370
                        new byte[] { 111, 63, 127 },
1✔
371
                        new byte[] { 57, 0, 76 },
1✔
372
                        new byte[] { 66, 38, 76 },
1✔
373
                        new byte[] { 28, 0, 38 },
1✔
374
                        new byte[] { 88, 19, 88 },
1✔
375
                        new byte[] { 255, 0, 255 },
1✔
376
                        new byte[] { 255, 127, 255 },
1✔
377
                        new byte[] { 165, 0, 165 },
1✔
378
                        new byte[] { 165, 82, 165 },
1✔
379
                        new byte[] { 127, 0, 127 },
1✔
380
                        new byte[] { 127, 63, 127 },
1✔
381
                        new byte[] { 76, 0, 76 },
1✔
382
                        new byte[] { 76, 38, 76 },
1✔
383
                        new byte[] { 38, 0, 38 },
1✔
384
                        new byte[] { 88, 19, 88 },
1✔
385
                        new byte[] { 255, 0, 191 },
1✔
386
                        new byte[] { 255, 127, 223 },
1✔
387
                        new byte[] { 165, 0, 124 },
1✔
388
                        new byte[] { 165, 82, 145 },
1✔
389
                        new byte[] { 127, 0, 95 },
1✔
390
                        new byte[] { 127, 63, 111 },
1✔
391
                        new byte[] { 76, 0, 57 },
1✔
392
                        new byte[] { 76, 38, 66 },
1✔
393
                        new byte[] { 38, 0, 28 },
1✔
394
                        new byte[] { 88, 19, 88 },
1✔
395
                        new byte[] { 255, 0, 127 },
1✔
396
                        new byte[] { 255, 127, 191 },
1✔
397
                        new byte[] { 165, 0, 82 },
1✔
398
                        new byte[] { 165, 82, 124 },
1✔
399
                        new byte[] { 127, 0, 63 },
1✔
400
                        new byte[] { 127, 63, 95 },
1✔
401
                        new byte[] { 76, 0, 38 },
1✔
402
                        new byte[] { 76, 38, 57 },
1✔
403
                        new byte[] { 38, 0, 19 },
1✔
404
                        new byte[] { 88, 19, 28 },
1✔
405
                        new byte[] { 255, 0, 63 },
1✔
406
                        new byte[] { 255, 127, 159 },
1✔
407
                        new byte[] { 165, 0, 41 },
1✔
408
                        new byte[] { 165, 82, 103 },
1✔
409
                        new byte[] { 127, 0, 31 },
1✔
410
                        new byte[] { 127, 63, 79 },
1✔
411
                        new byte[] { 76, 0, 19 },
1✔
412
                        new byte[] { 76, 38, 47 },
1✔
413
                        new byte[] { 38, 0, 9 },
1✔
414
                        new byte[] { 88, 19, 23 },
1✔
415
                        new byte[] { 0, 0, 0 },
1✔
416
                        new byte[] { 101, 101, 101 },
1✔
417
                        new byte[] { 102, 102, 102 },
1✔
418
                        new byte[] { 153, 153, 153 },
1✔
419
                        new byte[] { 204, 204, 204 },
1✔
420
                        new byte[] { 255, 255, 255 }
1✔
421
                };
1✔
422

423
                // 1 << 24;
424

425
                //1 << 30
426
                /// <summary>
427
                /// Represents the actual stored color.  Either a True Color or an indexed color.
428
                /// </summary>
429
                /// <remarks>
430
                /// If the number is >= 0, then the stored color is an indexed color ranging from 0 to 256.
431
                /// If the number is &lt; 0, then the stored color is a true color, less the negative sign.
432
                /// </remarks>
433
                private readonly uint _color;
434

435
                /// <summary>
436
                /// Creates a new color out of an indexed color.
437
                /// </summary>
438
                /// <param name="index">Index color with a value between 0 to 257</param>
439
                public Color(short index)
440
                {
2,015,962✔
441
                        if (index < 0 || index > 257)
2,015,962!
UNCOV
442
                                throw new ArgumentOutOfRangeException(nameof(index), "True index must be a value between 0 and 257.");
×
443

444
                        this._color = (uint)index;
2,015,962✔
445
                }
2,015,962✔
446

447
                /// <summary>
448
                /// Creates a color out of RGB true color bytes.
449
                /// </summary>
450
                /// <param name="r">Red</param>
451
                /// <param name="g">Green</param>
452
                /// <param name="b">Blue</param>
453
                public Color(byte r, byte g, byte b)
454
                        : this(new[] { r, g, b })
16,200✔
455
                {
16,200✔
456
                }
16,200✔
457

458
                /// <summary>
459
                /// Creates a color out of a RGB true color byte array.
460
                /// </summary>
461
                /// <param name="rgb">Red Green Blue</param>
462
                public Color(byte[] rgb)
463
                        : this(getInt24(rgb))
17,201✔
464
                {
17,201✔
465
                }
17,201✔
466

467
                private Color(uint trueColor)
468
                {
17,205✔
469
                        if (trueColor < 0 || trueColor > _maxTrueColor)
17,205!
UNCOV
470
                                throw new ArgumentOutOfRangeException(nameof(trueColor), "True color must be a 24 bit color.");
×
471

472
                        // Shift to set the 30th bit indicating a true color.
473
                        this._color = (uint)(trueColor | _trueColorFlag);
17,205✔
474
                }
17,205✔
475

476
                /// <summary>
477
                /// Approximates color from a true color RGB.
478
                /// </summary>
479
                /// <param name="r">Red</param>
480
                /// <param name="g">Green</param>
481
                /// <param name="b">Blue</param>
482
                /// <returns>Approximate RGB color.</returns>
483
                public static byte ApproxIndex(byte r, byte g, byte b)
484
                {
110✔
485
                        var prevDist = -1;
110✔
486
                        for (var i = 0; i < _indexRgb.Length; i++)
910!
487
                        {
455✔
488
                                var dist = (r - _indexRgb[i][0]) + (g - _indexRgb[i][1]) + (b - _indexRgb[i][2]);
455✔
489
                                if (dist == 0)
455!
UNCOV
490
                                        return (byte)i;
×
491

492
                                if (dist < prevDist)
455✔
493
                                {
110✔
494
                                        prevDist = dist;
110✔
495
                                        return (byte)i;
110✔
496
                                }
497
                        }
345✔
498

499
                        return 0;
×
500
                }
110✔
501

502
                /// <summary>
503
                /// Creates a color out of a true color int32.
504
                /// </summary>
505
                /// <param name="color">True color int 32.</param>
506
                public static Color FromTrueColor(uint color)
507
                {
4✔
508
                        return new Color(color);
4✔
509
                }
4✔
510

511
                /// <summary>
512
                /// Returns the RGB color code which matches the passed indexed color.
513
                /// </summary>
514
                /// <returns>Approximate RGB color from indexed color.</returns>
515
                public static ReadOnlySpan<byte> GetIndexRGB(byte index)
516
                {
66✔
517
                        return _indexRgb[index].AsSpan();
66✔
518
                }
66✔
519

520
                /// <inheritdoc/>
521
                public bool Equals(Color other)
522
                {
1✔
523
                        return this._color == other._color;
1✔
524
                }
1✔
525

526
                /// <inheritdoc/>
527
                public override bool Equals(object obj)
UNCOV
528
                {
×
UNCOV
529
                        return obj is Color other && this.Equals(other);
×
UNCOV
530
                }
×
531

532
                /// <summary>
533
                /// Approximates color from a true color RGB.
534
                /// </summary>
535
                /// <returns>Approximate RGB color.</returns>
536
                public byte GetApproxIndex()
537
                {
631✔
538
                        if (this.IsTrueColor)
631✔
539
                        {
110✔
540
                                return Color.ApproxIndex(this.R, this.G, this.B);
110✔
541
                        }
542
                        else
543
                        {
521✔
544
                                return (byte)this.Index;
521✔
545
                        }
546
                }
631✔
547

548
                /// <inheritdoc/>
549
                public override int GetHashCode()
UNCOV
550
                {
×
UNCOV
551
                        return (int)this._color;
×
UNCOV
552
                }
×
553

554
                /// <summary>
555
                /// Returns the RGB color code
556
                /// </summary>
557
                /// <returns></returns>
558
                public ReadOnlySpan<byte> GetRgb()
559
                {
2,242✔
560
                        if (this.IsTrueColor)
2,242✔
561
                        {
2,176✔
562
                                return this.GetTrueColorRgb();
2,176✔
563
                        }
564
                        else
565
                        {
66✔
566
                                return GetIndexRGB((byte)this._color);
66✔
567
                        }
568
                }
2,242✔
569

570
                /// <summary>
571
                /// Returns the RGB color code using the true color value.
572
                /// </summary>
573
                /// <remarks>
574
                /// If the color is not <see cref="IsTrueColor"/> it will return the default values for RGB
575
                /// </remarks>
576
                /// <returns></returns>
577
                public ReadOnlySpan<byte> GetTrueColorRgb()
578
                {
3,434✔
579
                        if (this.IsTrueColor)
3,434✔
580
                        {
3,177✔
581
                                return getRGBfromTrueColor(this._color);
3,177✔
582
                        }
583

584
                        return default;
257✔
585
                }
3,434✔
586

587
                /// <inheritdoc/>
588
                public override string ToString()
589
                {
3✔
590
                        if (this._color == 0)
3✔
591
                        {
1✔
592
                                return "ByBlock";
1✔
593
                        }
594

595
                        if (this._color == 256)
2✔
596
                        {
1✔
597
                                return "ByLayer";
1✔
598
                        }
599

600
                        if (this.IsTrueColor)
1!
601
                        {
1✔
602
                                var parts = this.GetTrueColorRgb();
1✔
603
                                return $"True Color RGB:{parts[0]},{parts[1]},{parts[2]}";
1✔
604
                        }
605

UNCOV
606
                        return $"Indexed Color:{this.Index}";
×
607
                }
3✔
608

609
                private static uint getInt24(byte[] array)
610
                {
17,201✔
611
                        if (BitConverter.IsLittleEndian)
17,201!
612
                                return (uint)(array[0] | array[1] << 8 | array[2] << 16);
17,201✔
613
                        else
UNCOV
614
                                return (uint)(array[0] << 16 | array[1] << 8 | array[2]);
×
615
                }
17,201✔
616

617
                private static ReadOnlySpan<byte> getRGBfromTrueColor(uint color)
618
                {
3,177✔
619
                        return new ReadOnlySpan<byte>(LittleEndianConverter.Instance.GetBytes(color), 0, 3);
3,177✔
620
                }
3,177✔
621
        }
622
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc