Skip to content

Commit 8fdacd2

Browse files
Changed color handling
1 parent 19964b4 commit 8fdacd2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+2090
-201
lines changed

Changelog.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
11
# Change Log
22

3+
## v3.0.0-rc.5
4+
5+
---
6+
Release Date: **22.12.2025**
7+
8+
- Added explicit operator for Address to convert from string to Address (string can be cast to address explicitly)
9+
- Added implicit operator for Range to convert from string to Range (string can be cast to range implicitly)
10+
- Introduced new class Color to handle colors in the Fill class. This class contains several color representations (implementations of the IColor interface):
11+
- IndexedColor (representing indexed colors form index 0 to 65)
12+
- SrgbColor (RGB or ARGB value)
13+
- ThemeColor (Color, defined by theme elements)
14+
- SystemColor (Color, defined by system colors)
15+
- AutoColor (automatic color, no value / dummy class)
16+
- Re-implementation of the Fill class to use the new Color class for color representation of the foreground and background colors (indexed property removed)
17+
- Added implicit operator for Fill to create a fill color by a string (RGB or ARGB value)
18+
- Added implicit operator for Fill to create a fill color by an integer (indexed color value)
19+
- Changed internal handling of text cell values
20+
- Added static methods `bool Comparators.IsZero(double)` and `bool Comparators.IsZero(float)
21+
- Added `double ParserUtils.ParseDouble(string)` to parse doubles with invariant culture
22+
- Added `void Validators.ValidateGenericColor(string, bool = false)` to check generic color strings (RGB or ARGB)
23+
- Added test cases
24+
25+
Note: Implicit address conversion from string to Address was not implemented, to avoid potential problems when comparing addresses (invalid strings could raise exceptions instead of returning false in the equals method). This does not apply to the Range struct
26+
327
## v3.0.0-rc.3 + v3.0.0-rc.4
428

529
---

MigrationGuide.md

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ The following guide lists all necessary changes, as well as changed behaviors of
2222
- The method `Workbook.AddStyle(Style)` was completely removed, after marked as obsolete in version 2.x. Styles should be added directly to cells or ranges.
2323
- The method `Workbook.AddStyleComponent(Style, AbstractStyle)` was completely removed, after marked as obsolete in version 2.x. Styles should be modified directly on cells, e.g. `workbook.CurrentWorksheet.Cells["A1"].CellStyle.CurrentFont.Bold = true;` or `workbook.CurrentWorksheet.Cells["A1"].CellStyle.Append(fontStyle)`.
2424
- The methods `Workbook.RemoveStyle(Style)`, `Workbook.RemoveStyle(Style, bool)`, `Workbook.RemoveStyle(string)` and `Workbook.RemoveStyle(string, bool)` were completely removed, after marked as obsolete in version 2.x. Styles should be removed directly from cells (e.g. `workbook.CurrentWorksheet.Cells["A1"].RemoveStyle()`.
25-
25+
- The method `Workbook.GetMruColors()` returns now a `IReadOnlyList<Color>` (was string). The new Color class (namespace `NanoXLSX.Colors`) can represent an RGB, ARGB, system, theme or indexed color. See the Color class documentation for more details.
2626
---
2727

2828
### Worksheet
@@ -230,12 +230,14 @@ ApplicationDefined, ANSI, Default, Symbols, Mac, ShiftJIS, Hangul, Johab, GBK, B
230230

231231
#### Fill
232232

233+
Fill was completely overhauled in version 3.0.0, to support more flexible color definitions, like (A)RGB, indexed colors, theme or system colors, as well as tint.
234+
233235
- The public constant values of the `Fill` class were renamed, according to the following overview:
234236

235237
| Old Constant | New Constant | Remarks |
236238
|--------------------------|------------------------|----------|
237-
| `DEFAULT_COLOR` | `DefaultColor` | |
238-
| `DEFAULT_INDEXED_COLOR` | `DefaultIndexedColor` | |
239+
| `DEFAULT_COLOR` | `DefaultColor` | The type was changed from `string` to an instance of `SrgbColor` with `FF000000` as ARGB value |
240+
| `DEFAULT_INDEXED_COLOR` | `DefaultIndexedColor` | The type was changed from `int` to an instance of `IndexedColor` with the `64` as index value |
239241
| `DEFAULT_PATTERN_FILL` | `DefaultPatternFill` | |
240242

241243
- The enum values of `Fill.FillType` were renamed, according to the following overview:
@@ -258,6 +260,23 @@ ApplicationDefined, ANSI, Default, Symbols, Mac, ShiftJIS, Hangul, Johab, GBK, B
258260
| `PatternValue.none` | `PatternValue.None` | |
259261

260262
- The static method `Fill.ValidateColr(string,bool, bool)` was moved to the utils class `Validators.ValidateColr(string,bool, bool)` in namespace `NanoXLSX.Utils`. The class has to be changed in the code, but the method signature remains unchanged.
263+
- The property `Fill.BackgroundColor` was changed from type `string` to the new `Color` class (namespace `NanoXLSX.Colors`). The new Color class can represent an RGB, ARGB, system, theme or indexed color. See the Color class documentation for more details.
264+
- The property `Fill.ForegroundColor` was changed from type `string` to the new `Color` class (namespace `NanoXLSX.Colors`). The new Color class can represent an RGB, ARGB, system, theme or indexed color. See the Color class documentation for more details.
265+
- The property `IndexedColor` was removed and is now part of the new `Color` class, applied to `BackgroundColor` and `ForegroundColor`. In the `Color` class, there is a property `IndexedColor` that holds an object of the type `IndexedColor` (namespace `NanoXLSX.Themes`). The original `int` value has to be replaced by one of the available enum values in the class `IndexedColor`. Usage example:
266+
267+
```csharp
268+
Fill fill = new Fill();
269+
fill.ForegroundColor = Color.CreateIndexed(IndexedColor.Value.Yellow);
270+
271+
// Different examples with other color types:
272+
Fill fill2 = new Fill();
273+
fill2.BackgroundColor = Color.CreateRgb("FF00FF00"); // ARGB
274+
275+
Fill fill3 = new Fill();
276+
fill3.ForegroundColor = Color.CreateTheme(Theme.ColorSchemeElement.Accent1, 0.5f); // Theme color with tint
277+
278+
```
279+
261280

262281
#### CellXf
263282

NanoXLSX.Core/Address.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,15 @@ public int CompareTo(Address other)
222222
return left.CompareTo(right) >= 0;
223223
}
224224

225+
/// <summary>
226+
/// Explicit conversion from string to Address
227+
/// </summary>
228+
/// <param name="address">Address expression</param>
229+
public static explicit operator Address(string address)
230+
{
231+
return new Address(address);
232+
}
233+
225234
/// <summary>
226235
/// Creates a (dereferenced, if applicable) deep copy of this address
227236
/// </summary>

NanoXLSX.Core/Colors/AutoColor.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
8+
using NanoXLSX.Interfaces;
9+
10+
namespace NanoXLSX.Colors
11+
{
12+
/// <summary>
13+
/// Class representing an automatic color.
14+
/// </summary>
15+
/// \remark <remarks>This class does not carry any value. It is only for the purpose of identification.</remarks>
16+
public class AutoColor : IColor
17+
{
18+
/// <summary>
19+
/// Static instance of the AutoColor class to avoid multiple instances (instances does not deviate)
20+
/// </summary>
21+
public static readonly AutoColor Instance = new AutoColor();
22+
23+
/// <summary>
24+
/// The string value of an auto color is always null
25+
/// </summary>
26+
public string StringValue => null;
27+
}
28+
}

0 commit comments

Comments
 (0)