NanoXLSX.Core 3.0.0
Loading...
Searching...
No Matches
IndexedColor.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 © 2026
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
10using NanoXLSX.Utils;
11using static NanoXLSX.Colors.IndexedColor;
12
13namespace NanoXLSX.Colors
14{
18 public class IndexedColor : ITypedColor<Value>
19 {
172
176 public const Value DefaultIndexedColor = Value.SystemForeground;
180 public const string DefaultSystemForegroundColorArgb = "FF000000";
184 public const string DefaultSystemBackgroundColorArgb = "FFFFFFFF";
185
186
190 public Value ColorValue { get; set; }
191
196
201 {
203 }
204
209 public IndexedColor(Value color)
210 {
211 ColorValue = color;
212 }
213
219 public IndexedColor(int colorIndex)
220 {
221 if (colorIndex < 0 || colorIndex > 65)
222 {
223 throw new StyleException("Indexed color value must be between 0 and 65.");
224 }
225 ColorValue = (Value)colorIndex;
226 }
227
232 public string GetArgbValue()
233 {
234 return GetArgbValue(ColorValue);
235 }
236
242 {
243 return new SrgbColor(GetArgbValue());
244 }
245
251 public static string GetArgbValue(Value indexedValue)
252 {
253 switch (indexedValue)
254 {
255 // 0–7 (duplicates of 8–15)
256 case Value.Black0:
257 case Value.Black:
258 return "FF000000";
259
260 case Value.White1:
261 case Value.White:
262 return "FFFFFFFF";
263
264 case Value.Red2:
265 case Value.Red:
266 return "FFFF0000";
267
268 case Value.BrightGreen3:
269 case Value.BrightGreen:
270 return "FF00FF00";
271
272 case Value.Blue4:
273 case Value.Blue:
274 case Value.PureBlue:
275 return "FF0000FF";
276
277 case Value.Yellow5:
278 case Value.Yellow:
279 case Value.StrongYellow:
280 return "FFFFFF00";
281
282 case Value.Magenta6:
283 case Value.Magenta:
284 case Value.StrongMagenta:
285 return "FFFF00FF";
286
287 case Value.Cyan7:
288 case Value.Cyan:
289 case Value.StrongCyan:
290 return "FF00FFFF";
291
292 // Extended palette
293 case Value.DarkRed:
294 case Value.DarkMaroon:
295 return "FF800000";
296
297 case Value.DarkGreen:
298 return "FF008000";
299
300 case Value.DarkBlue:
301 case Value.Navy:
302 return "FF000080";
303
304 case Value.Olive:
305 return "FF808000";
306
307 case Value.Purple:
308 case Value.DarkViolet:
309 return "FF800080";
310
311 case Value.Teal:
312 case Value.DarkTeal:
313 return "FF008080";
314
315 case Value.LightGray:
316 return "FFC0C0C0";
317
318 case Value.Gray:
319 return "FF808080";
320
321 case Value.LightCornflowerBlue:
322 return "FF9999FF";
323
324 case Value.DarkRose:
325 case Value.DarkRoseDuplicate:
326 return "FF993366";
327
328 case Value.LightYellow:
329 return "FFFFFFCC";
330
331 case Value.LightCyan:
332 case Value.PaleCyan:
333 return "FFCCFFFF";
334
335 case Value.DarkPurple:
336 return "FF660066";
337
338 case Value.Salmon:
339 return "FFFF8080";
340
341 case Value.MediumBlue:
342 return "FF0066CC";
343
344 case Value.LightLavender:
345 return "FFCCCCFF";
346
347 case Value.SkyBlue:
348 return "FF00CCFF";
349
350 case Value.LightMint:
351 return "FFCCFFCC";
352
353 case Value.PastelYellow:
354 return "FFFFFF99";
355
356 case Value.LightSkyBlue:
357 return "FF99CCFF";
358
359 case Value.Rose:
360 return "FFFF99CC";
361
362 case Value.Lavender:
363 return "FFCC99FF";
364
365 case Value.Peach:
366 return "FFFFCC99";
367
368 case Value.RoyalBlue:
369 return "FF3366FF";
370
371 case Value.Turquoise:
372 return "FF33CCCC";
373
374 case Value.LightOlive:
375 return "FF99CC00";
376
377 case Value.Gold:
378 return "FFFFCC00";
379
380 case Value.Orange:
381 return "FFFF9900";
382
383 case Value.DarkOrange:
384 return "FFFF6600";
385
386 case Value.BlueGray:
387 return "FF666699";
388
389 case Value.MediumGray:
390 return "FF969696";
391
392 case Value.DarkSlateBlue:
393 return "FF003366";
394
395 case Value.SeaGreen:
396 return "FF339966";
397
398 case Value.VeryDarkGreen:
399 return "FF003300";
400
401 case Value.DarkOlive:
402 return "FF333300";
403
404 case Value.Brown:
405 return "FF993300";
406
407 case Value.Indigo:
408 return "FF333399";
409
410 case Value.VeryDarkGray:
411 return "FF333333";
412
413 case Value.SystemBackground:
414 // Excel default: white background
416
417 default:
418 // Excel default: black text
420 }
421 }
422
428 public override bool Equals(object obj)
429 {
430 return obj is IndexedColor color &&
431 ColorValue == color.ColorValue;
432 }
433
438 public override int GetHashCode()
439 {
440 return 800285905 + ColorValue.GetHashCode();
441 }
442
447 public static implicit operator IndexedColor(Value value)
448 {
449 return new IndexedColor(value);
450 }
451 }
452}
Class representing an indexed color from the legacy OOXML / Excel indexed color palette.
IndexedColor()
Default constructor with default indexed color.
Value ColorValue
Value of the indexed color.
IndexedColor(Value color)
Constructor with specified indexed color value.
string GetArgbValue()
Gets the ARGB hex code representation of the indexed color.
string StringValue
String representation of the indexed color value.
const string DefaultSystemBackgroundColorArgb
Default ARGB value for system background color.
static string GetArgbValue(Value indexedValue)
Maps the indexed color value to its ARGB hex code representation.
override int GetHashCode()
Gets the hash code of the instance.
Value
Legacy OOXML / Excel indexed color palette.
@ Magenta6
Magenta (duplicate of index 14).
@ DarkOrange
Dark orange (FF6600).
@ Cyan
Cyan / Aqua (#00FFFF).
@ DarkSlateBlue
Dark slate blue (#003366).
@ Yellow5
Yellow (duplicate of index 13).
@ Cyan7
Cyan (duplicate of index 15).
@ Navy
Dark navy blue (#000080).
@ MediumGray
Medium gray (#969696).
@ White1
White (duplicate of index 9).
@ Blue4
Blue (duplicate of index 12).
@ VeryDarkGray
Very dark gray (#333333).
@ Black0
Black (duplicate of index 8).
@ RoyalBlue
Royal blue (#3366FF).
@ DarkRoseDuplicate
Dark rose (duplicate of index 25).
@ PastelYellow
Light pastel yellow (FFFF99).
@ SystemBackground
System background color.
@ SystemForeground
System foreground color.
@ LightLavender
Light lavender blue (CCCCFF).
@ LightOlive
Light olive green (#99CC00).
@ StrongCyan
Strong cyan (#00FFFF).
@ DarkRed
Dark red / maroon (#800000).
@ DarkBlue
Dark blue / navy (#000080).
@ Indigo
Indigo / dark blue-purple (#333399).
@ DarkOlive
Dark olive (#333300).
@ Gray
Medium gray (#808080).
@ BrightGreen3
Bright green (duplicate of index 11).
@ LightMint
Light mint green (CCFFCC).
@ MediumBlue
Medium blue (#0066CC).
@ BrightGreen
Bright green (#00FF00).
@ Magenta
Magenta / Fuchsia (FF00FF).
@ DarkPurple
Dark purple (#660066).
@ LightCyan
Light cyan (CCFFFF).
@ DarkRose
Dark rose (#993366).
@ Turquoise
Turquoise (#33CCCC).
@ LightSkyBlue
Light sky blue (#99CCFF).
@ Salmon
Salmon pink (FF8080).
@ Red2
Red (duplicate of index 10).
@ StrongYellow
Strong yellow (FFFF00).
@ StrongMagenta
Strong magenta (FF00FF).
@ VeryDarkGreen
Very dark green (#003300).
@ DarkGreen
Dark green (#008000).
@ LightYellow
Light yellow (FFFFCC).
@ DarkViolet
Dark violet (#800080).
@ LightGray
Light gray / silver (C0C0C0).
@ DarkMaroon
Dark maroon (#800000).
@ LightCornflowerBlue
Light cornflower blue (#9999FF).
SrgbColor GetSrgbColor()
Gets the sRGB color representation of the indexed color.
const Value DefaultIndexedColor
Default indexed color (system foreground color).
IndexedColor(int colorIndex)
Constructor with specified indexed color index.
override bool Equals(object obj)
Determines whether the specified object is equal to the current object.
const string DefaultSystemForegroundColorArgb
Default ARGB value for system foreground color.
Class representing a generic sRGB color (with or without alpha channel).
Definition SrgbColor.cs:19
Class for exceptions regarding Style incidents.
Class providing static methods to parse string values to specific types or to print object as languag...
static string ToString(int input)
Transforms an integer to an invariant sting.