NanoXLSX.Core 3.0.0-rc.4
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 © 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;
11using NanoXLSX.Utils;
12using static NanoXLSX.Colors.IndexedColor;
13
14namespace NanoXLSX.Colors
15{
19 public class IndexedColor : ITypedColor<Value>
20 {
173
177 public const Value DefaultIndexedColor = Value.SystemForeground;
181 public const string DefaultSystemForegroundColorArgb = "FF000000";
185 public const string DefaultSystemBackgroundColorArgb = "FFFFFFFF";
186
187
191 public Value ColorValue { get; set; }
192
197
202 {
204 }
205
210 public IndexedColor(Value color)
211 {
212 ColorValue = color;
213 }
214
220 public IndexedColor(int colorIndex)
221 {
222 if (colorIndex < 0 || colorIndex > 65)
223 {
224 throw new StyleException("Indexed color value must be between 0 and 65.");
225 }
226 ColorValue = (Value)colorIndex;
227 }
228
233 public string GetArgbValue()
234 {
235 return GetArgbValue(ColorValue);
236 }
237
243 {
244 return new SrgbColor(GetArgbValue());
245 }
246
247
253 public override bool Equals(object obj)
254 {
255 return obj is IndexedColor color &&
256 ColorValue == color.ColorValue;
257 }
258
263 public override int GetHashCode()
264 {
265 return 800285905 + ColorValue.GetHashCode();
266 }
267
273 public static string GetArgbValue(Value indexedValue)
274 {
275 switch (indexedValue)
276 {
277 // 0–7 (duplicates of 8–15)
278 case Value.Black0:
279 case Value.Black:
280 return "FF000000";
281
282 case Value.White1:
283 case Value.White:
284 return "FFFFFFFF";
285
286 case Value.Red2:
287 case Value.Red:
288 return "FFFF0000";
289
290 case Value.BrightGreen3:
291 case Value.BrightGreen:
292 return "FF00FF00";
293
294 case Value.Blue4:
295 case Value.Blue:
296 case Value.PureBlue:
297 return "FF0000FF";
298
299 case Value.Yellow5:
300 case Value.Yellow:
301 case Value.StrongYellow:
302 return "FFFFFF00";
303
304 case Value.Magenta6:
305 case Value.Magenta:
306 case Value.StrongMagenta:
307 return "FFFF00FF";
308
309 case Value.Cyan7:
310 case Value.Cyan:
311 case Value.StrongCyan:
312 return "FF00FFFF";
313
314 // Extended palette
315 case Value.DarkRed:
316 case Value.DarkMaroon:
317 return "FF800000";
318
319 case Value.DarkGreen:
320 return "FF008000";
321
322 case Value.DarkBlue:
323 case Value.Navy:
324 return "FF000080";
325
326 case Value.Olive:
327 return "FF808000";
328
329 case Value.Purple:
330 case Value.DarkViolet:
331 return "FF800080";
332
333 case Value.Teal:
334 case Value.DarkTeal:
335 return "FF008080";
336
337 case Value.LightGray:
338 return "FFC0C0C0";
339
340 case Value.Gray:
341 return "FF808080";
342
343 case Value.LightCornflowerBlue:
344 return "FF9999FF";
345
346 case Value.DarkRose:
347 case Value.DarkRoseDuplicate:
348 return "FF993366";
349
350 case Value.LightYellow:
351 return "FFFFFFCC";
352
353 case Value.LightCyan:
354 case Value.PaleCyan:
355 return "FFCCFFFF";
356
357 case Value.DarkPurple:
358 return "FF660066";
359
360 case Value.Salmon:
361 return "FFFF8080";
362
363 case Value.MediumBlue:
364 return "FF0066CC";
365
366 case Value.LightLavender:
367 return "FFCCCCFF";
368
369 case Value.SkyBlue:
370 return "FF00CCFF";
371
372 case Value.LightMint:
373 return "FFCCFFCC";
374
375 case Value.PastelYellow:
376 return "FFFFFF99";
377
378 case Value.LightSkyBlue:
379 return "FF99CCFF";
380
381 case Value.Rose:
382 return "FFFF99CC";
383
384 case Value.Lavender:
385 return "FFCC99FF";
386
387 case Value.Peach:
388 return "FFFFCC99";
389
390 case Value.RoyalBlue:
391 return "FF3366FF";
392
393 case Value.Turquoise:
394 return "FF33CCCC";
395
396 case Value.LightOlive:
397 return "FF99CC00";
398
399 case Value.Gold:
400 return "FFFFCC00";
401
402 case Value.Orange:
403 return "FFFF9900";
404
405 case Value.DarkOrange:
406 return "FFFF6600";
407
408 case Value.BlueGray:
409 return "FF666699";
410
411 case Value.MediumGray:
412 return "FF969696";
413
414 case Value.DarkSlateBlue:
415 return "FF003366";
416
417 case Value.SeaGreen:
418 return "FF339966";
419
420 case Value.VeryDarkGreen:
421 return "FF003300";
422
423 case Value.DarkOlive:
424 return "FF333300";
425
426 case Value.Brown:
427 return "FF993300";
428
429 case Value.Indigo:
430 return "FF333399";
431
432 case Value.VeryDarkGray:
433 return "FF333333";
434
435 case Value.SystemBackground:
436 // Excel default: white background
438
439 default:
440 // Excel default: black text
442 }
443 }
444
449 public static implicit operator IndexedColor(Value value)
450 {
451 return new IndexedColor(value);
452 }
453 }
454}
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:20
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.