NanoXLSX.Core 3.0.0-rc.4
Loading...
Searching...
No Matches
Color.cs
1/*
2 * NanoXLSX is a small .NET library to generate and read XLSX (Microsoft Excel 2007 or newer) files in an easy and native way
3 * Copyright Raphael Stoeckli © 2025
4 * This library is licensed under the MIT License.
5 * You find a copy of the license in project folder or on: http://opensource.org/licenses/MIT
6 */
7
8using System;
9using System.Collections.Generic;
12using NanoXLSX.Themes;
13
14namespace NanoXLSX.Colors
15{
19 public class Color : IComparable
20 {
21
22 #region enums
26 public enum ColorType
27 {
39 System
40 }
41 #endregion
42
43 #region properties
47 public ColorType Type { get; private set; }
48
52 public bool Auto { get; private set; }
53
57 public SrgbColor RgbColor { get; private set; }
58
62 public IndexedColor IndexedColor { get; private set; }
63
67 public ThemeColor ThemeColor { get; private set; }
68
72 public SystemColor SystemColor { get; private set; }
73
79 public double? Tint { get; set; }
80
84 public bool IsDefined => Type != ColorType.None;
85
90 {
91 get
92 {
93 switch (Type)
94 {
95 case ColorType.Rgb:
96 return RgbColor;
97 case ColorType.Indexed:
98 return IndexedColor;
99 case ColorType.Theme:
100 return ThemeColor;
101 case ColorType.System:
102 return SystemColor;
103 case ColorType.Auto:
104 return AutoColor.Instance;
105 default:
106 return null;
107 }
108 }
109 }
110
111 #endregion
112
113 #region constructors
117 private Color() { }
118 #endregion
119
120 #region methods
126 public string GetArgbValue()
127 {
128 if (Type == ColorType.Rgb)
129 {
130 return RgbColor.ColorValue;
131 }
132 else if (Type == ColorType.Indexed)
133 {
134 return IndexedColor.GetArgbValue();
135 }
136 else
137 {
138 return null;
139 }
140 }
141 #endregion
142
143 #region factory methods
148 public static Color CreateNone()
149 {
150 return new Color { Type = ColorType.None };
151 }
152
157 public static Color CreateAuto()
158 {
159 return new Color
160 {
161 Type = ColorType.Auto,
162 Auto = true
163 };
164 }
165
171 public static Color CreateRgb(SrgbColor color)
172 {
173 // If null is passed, error handling is already covered by the string method
174 return new Color
175 {
176 Type = ColorType.Rgb,
177 RgbColor = color
178 };
179 }
180
187 public static Color CreateRgb(string rgbValue)
188 {
189 // Validation is done in SrgbColor class
190 return new Color
191 {
192 Type = ColorType.Rgb,
193 RgbColor = new SrgbColor(rgbValue)
194 };
195 }
196
202 public static Color CreateIndexed(IndexedColor color)
203 {
204 if (color == null)
205 {
206 throw new StyleException("An indexed color cannot be null");
207 }
208 return new Color
209 {
210 Type = ColorType.Indexed,
211 IndexedColor = color
212 };
213 }
214
220 public static Color CreateIndexed(IndexedColor.Value indexValue)
221 {
222 return new Color
223 {
224 Type = ColorType.Indexed,
225 IndexedColor = new IndexedColor(indexValue)
226 };
227 }
228
235 public static Color CreateIndexed(int index)
236 {
237 return new Color
238 {
239 Type = ColorType.Indexed,
240 IndexedColor = new IndexedColor(index)
241 };
242 }
243
250 public static Color CreateTheme(ThemeColor color, double? tint = null)
251 {
252 if (color == null)
253 {
254 throw new StyleException("A theme color cannot be null");
255 }
256 return new Color
257 {
258 Type = ColorType.Theme,
259 ThemeColor = color,
260 Tint = tint
261 };
262 }
263
270 public static Color CreateTheme(Theme.ColorSchemeElement themeColor, double? tint = null)
271 {
272 return new Color
273 {
274 Type = ColorType.Theme,
275 ThemeColor = new ThemeColor(themeColor),
276 Tint = tint
277 };
278 }
279
285 public static Color CreateSystem(SystemColor color)
286 {
287 if (color == null)
288 {
289 throw new StyleException("A system color cannot be null");
290 }
291 return new Color
292 {
293 Type = ColorType.System,
294 SystemColor = color
295 };
296 }
297
303 public static Color CreateSystem(SystemColor.Value systemColorValue)
304 {
305 return new Color
306 {
307 Type = ColorType.System,
308 SystemColor = new SystemColor(systemColorValue)
309 };
310 }
311
312 #endregion
313
319 public static implicit operator Color(string rgbValue)
320 {
321 return CreateRgb(rgbValue);
322 }
323
329 public static implicit operator Color(int colorIndex)
330 {
331 return CreateIndexed(colorIndex);
332 }
333
339 public static implicit operator Color(IndexedColor.Value colorIndex)
340 {
341 return CreateIndexed(colorIndex);
342 }
343
348 public override string ToString()
349 {
350 switch (Type)
351 {
352 case ColorType.Rgb:
353 return "RGBColor:" + RgbColor.StringValue;
354 case ColorType.Indexed:
355 return "IndexedColor:" + IndexedColor.StringValue;
356 case ColorType.Theme:
357 return "ThemeColor:" + ThemeColor.StringValue;
358 case ColorType.System:
359 return "SystemColor:" + SystemColor.StringValue;
360 case ColorType.Auto:
361 return "Auto-Color";
362 default:
363 return "Undefined Color";
364 }
365 }
366
367
373 public override bool Equals(object obj)
374 {
375 return obj is Color color &&
376 Type == color.Type &&
377 Auto == color.Auto &&
378 EqualityComparer<SrgbColor>.Default.Equals(RgbColor, color.RgbColor) &&
379 EqualityComparer<IndexedColor>.Default.Equals(IndexedColor, color.IndexedColor) &&
380 EqualityComparer<ThemeColor>.Default.Equals(ThemeColor, color.ThemeColor) &&
381 EqualityComparer<SystemColor>.Default.Equals(SystemColor, color.SystemColor) &&
382 Tint == color.Tint &&
383 IsDefined == color.IsDefined;
384 }
385
390 public override int GetHashCode()
391 {
392 var hashCode = -1729664991;
393 hashCode = hashCode * -1521134295 + Type.GetHashCode();
394 hashCode = hashCode * -1521134295 + Auto.GetHashCode();
395 hashCode = hashCode * -1521134295 + EqualityComparer<AutoColor>.Default.GetHashCode(AutoColor.Instance);
396 hashCode = hashCode * -1521134295 + EqualityComparer<SrgbColor>.Default.GetHashCode(RgbColor);
397 hashCode = hashCode * -1521134295 + EqualityComparer<IndexedColor>.Default.GetHashCode(IndexedColor);
398 hashCode = hashCode * -1521134295 + EqualityComparer<ThemeColor>.Default.GetHashCode(ThemeColor);
399 hashCode = hashCode * -1521134295 + EqualityComparer<SystemColor>.Default.GetHashCode(SystemColor);
400 hashCode = hashCode * -1521134295 + Tint.GetHashCode();
401 hashCode = hashCode * -1521134295 + IsDefined.GetHashCode();
402 return hashCode;
403 }
404
411 public int CompareTo(object obj)
412 {
413 if (obj == null)
414 {
415 return 1;
416 }
417
418 if (!(obj is Color other))
419 {
420 throw new StyleException("The provided object to compare is not a Color");
421 }
422
423 // 1) Compare by color type first
424 int typeCompare = Type.CompareTo(other.Type);
425 if (typeCompare != 0)
426 {
427 return typeCompare;
428 }
429
430 // 2) Same type -> compare internal representation
431 switch (Type)
432 {
433 case ColorType.None:
434 return 0;
435 case ColorType.Auto:
436 return 0;
437 case ColorType.Rgb:
438 return string.Compare(
439 RgbColor?.StringValue,
440 other.RgbColor?.StringValue,
441 StringComparison.OrdinalIgnoreCase);
442 case ColorType.Indexed:
443 // Numeric comparison of palette index
444 return IndexedColor.ColorValue.CompareTo(other.IndexedColor.ColorValue);
445 case ColorType.Theme:
446 {
447 // Numeric comparison of theme slot
448 int themeCompare = ThemeColor.ColorValue.CompareTo(other.ThemeColor.ColorValue);
449 if (themeCompare != 0)
450 {
451 return themeCompare;
452 }
453 // Same theme slot -> compare tint
454 return Nullable.Compare(Tint, other.Tint);
455 }
456 case ColorType.System:
457 // Enum-based comparison -> not string-based
458 return SystemColor.ColorValue.CompareTo(other.SystemColor.ColorValue);
459 default:
460 // Defensive fallback —> should normally never happen
461 return string.Compare(
462 Value?.StringValue,
463 other.Value?.StringValue,
464 StringComparison.OrdinalIgnoreCase);
465 }
466 }
467
468 }
469}
Class representing an automatic color.
Definition AutoColor.cs:17
static readonly AutoColor Instance
Static instance of the AutoColor class to avoid multiple instances (instances does not deviate).
Definition AutoColor.cs:21
Compound class representing a color in various representations (RGB, indexed, theme,...
Definition Color.cs:20
static Color CreateIndexed(IndexedColor color)
Creates an Color from an indexed color.
Definition Color.cs:202
ThemeColor ThemeColor
Theme-based color when Type is Theme (See Theme.ColorSchemeElement).
Definition Color.cs:67
static Color CreateAuto()
Creates an Color with auto=true.
Definition Color.cs:157
static Color CreateTheme(ThemeColor color, double? tint=null)
Creates an Color from a theme color instance.
Definition Color.cs:250
SrgbColor RgbColor
RGB/ARGB value when Type is Rgb.
Definition Color.cs:57
double? Tint
Optional tint value for colors (-1.0 to 1.0), mainly for theme colors Positive values lighten,...
Definition Color.cs:79
static Color CreateIndexed(int index)
Creates an Color from a color index (0 to 65).
Definition Color.cs:235
static Color CreateIndexed(IndexedColor.Value indexValue)
Creates an Color from an indexed color value (see IndexedColor.Value).
Definition Color.cs:220
override int GetHashCode()
Gets the hash code of the instance.
Definition Color.cs:390
override bool Equals(object obj)
Determines whether the specified object is equal to the current object.
Definition Color.cs:373
bool IsDefined
Checks if this Color is defined (not None).
Definition Color.cs:84
IColor Value
Gets the color value as IColor interface. If no color was defined (ColorType.None or property IsDefin...
Definition Color.cs:90
static Color CreateRgb(string rgbValue)
Creates an Color from an RGB string (e.g., "FFAABBCC").
Definition Color.cs:187
string GetArgbValue()
Gets the ARGB string value of the color, if applicable.
Definition Color.cs:126
static Color CreateNone()
Creates an Color with no color (empty element).
Definition Color.cs:148
static Color CreateRgb(SrgbColor color)
Creates an Color from an RGB/ARGB color.
Definition Color.cs:171
int CompareTo(object obj)
Compares two instances for sorting purpose.
Definition Color.cs:411
SystemColor SystemColor
System color when Type is System (See SystemColor.Value).
Definition Color.cs:72
static Color CreateSystem(SystemColor.Value systemColorValue)
Creates an Color from a system color instance.
Definition Color.cs:303
static Color CreateSystem(SystemColor color)
Creates an Color from a system color.
Definition Color.cs:285
ColorType Type
The type of color this value represents.
Definition Color.cs:47
ColorType
Enum defining the type of color representation.
Definition Color.cs:27
@ None
No color defined.
Definition Color.cs:29
@ Rgb
RGB/ARGB color value.
Definition Color.cs:33
@ Indexed
Legacy indexed color (0-56+).
Definition Color.cs:35
static Color CreateTheme(Theme.ColorSchemeElement themeColor, double? tint=null)
Creates an Color from a theme color scheme element.
Definition Color.cs:270
bool Auto
Auto attribute - if true, color is automatically determined.
Definition Color.cs:52
IndexedColor IndexedColor
Indexed color when Type is Indexed (See IndexedColor.Value).
Definition Color.cs:62
override string ToString()
String representation od a Color instance.
Definition Color.cs:348
Class representing a generic sRGB color (with or without alpha channel).
Definition SrgbColor.cs:20
Class for exceptions regarding Style incidents.
Class representing an Office theme.
Definition Theme.cs:17
ColorSchemeElement
Enum to define the sequence index of color scheme element, used in the implementations of Interfaces....
Definition Theme.cs:36
Interface to represent non typed color, either defined by the system or the user.
Definition IColor.cs:14