Skip to content

Commit 38897ab

Browse files
Added tests
1 parent 3db2062 commit 38897ab

File tree

3 files changed

+237
-3
lines changed

3 files changed

+237
-3
lines changed

NanoXLSX.Core/Colors/Color.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public enum ColorType
4949
/// <summary>
5050
/// Auto attribute - if true, color is automatically determined
5151
/// </summary>
52-
public bool? Auto { get; private set; }
52+
public bool Auto { get; private set; }
5353

5454
/// <summary>
5555
/// RGB/ARGB value when Type is Rgb

NanoXLSX.Core/Styles/Fill.cs

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
*/
77

88
using System.Collections.Generic;
9+
using System.ComponentModel;
910
using System.Text;
1011
using NanoXLSX.Colors;
12+
using NanoXLSX.Interfaces;
1113

1214
namespace NanoXLSX.Styles
1315
{
@@ -235,7 +237,7 @@ public Fill CopyFill()
235237
}
236238

237239
/// <summary>
238-
/// Sets the color and the depending on fill type, using a sRGB value (without alpha)
240+
/// Sets the color depending on fill type, using a sRGB value (without alpha)
239241
/// </summary>
240242
/// <param name="value">color value</param>
241243
/// <param name="fillType">fill type (fill or pattern)</param>
@@ -253,6 +255,36 @@ public void SetColor(string value, FillType fillType)
253255
}
254256
PatternFill = PatternValue.Solid;
255257
}
258+
259+
/// <summary>
260+
/// Sets the color depending on fill type, using a color object of the type <see cref="Color"/>
261+
/// </summary>
262+
/// <param name="value">color value (compound object)</param>
263+
/// <param name="fillType">fill type (fill or pattern)</param>
264+
public void SetColor(Color value, FillType fillType)
265+
{
266+
if (fillType == FillType.FillColor)
267+
{
268+
backgroundColor = DefaultColor;
269+
ForegroundColor = value;
270+
}
271+
else
272+
{
273+
BackgroundColor = value;
274+
foregroundColor = DefaultColor;
275+
}
276+
PatternFill = PatternValue.Solid;
277+
}
278+
279+
/// <summary>
280+
/// Sets the color depending on fill type, using a color object, deriving from <see cref="IColor"/>
281+
/// </summary>
282+
/// <param name="value">color value (component)</param>
283+
/// <param name="fillType">fill type (fill or pattern)</param>
284+
public void SetColor(IColor value, FillType fillType)
285+
{
286+
SetColor(GetColorByComponent(value), fillType);
287+
}
256288
#endregion
257289

258290
#region staticMethods
@@ -344,6 +376,34 @@ internal static PatternValue GetPatternEnum(string name)
344376
}
345377
}
346378

379+
/// <summary>
380+
/// Gets a Color object based on the passed component
381+
/// </summary>
382+
/// <param name="component">Color component</param>
383+
/// <returns>Color instance</returns>
384+
private static Color GetColorByComponent(IColor component)
385+
{
386+
if (component is SrgbColor)
387+
{
388+
return Color.CreateRgb((SrgbColor)component);
389+
}
390+
else if (component is IndexedColor)
391+
{
392+
return Color.CreateIndexed((IndexedColor)component);
393+
}
394+
else if (component is ThemeColor)
395+
{
396+
return Color.CreateTheme((ThemeColor)component);
397+
}
398+
else if (component is SystemColor)
399+
{
400+
return Color.CreateSystem((SystemColor)component);
401+
}
402+
else // AutoColor
403+
{
404+
return Color.CreateAuto();
405+
}
406+
}
347407
#endregion
348408

349409
}

NanoXlsx.Core.Test/Styles/FillTest.cs

Lines changed: 175 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using NanoXLSX.Colors;
33
using NanoXLSX.Exceptions;
44
using NanoXLSX.Styles;
5+
using NanoXLSX.Themes;
56
using NanoXLSX.Utils;
67
using Xunit;
78
using static NanoXLSX.Styles.Fill;
@@ -260,7 +261,7 @@ public void PatternFillTest(PatternValue value)
260261
Assert.Equal(value, fill.PatternFill);
261262
}
262263

263-
[Theory(DisplayName = "Test of the SetColor function")]
264+
[Theory(DisplayName = "Test of the SetColor function, when using a ARGB value")]
264265
[InlineData("FFAABBCC", FillType.FillColor, "FFAABBCC", "FF000000")]
265266
[InlineData("FF112233", FillType.PatternColor, "FF000000", "FF112233")]
266267
public void SetColorTest(string color, FillType fillType, string expectedForeground, string expectedBackground)
@@ -277,6 +278,179 @@ public void SetColorTest(string color, FillType fillType, string expectedForegro
277278
Assert.Equal(expectedBackground, fill.BackgroundColor);
278279
}
279280

281+
[Theory(DisplayName = "Test of the SetColor function, when using a sRGB color object")]
282+
[InlineData("FFAABBCC", FillType.FillColor, "FFAABBCC", "FF000000")]
283+
[InlineData("FF112233", FillType.PatternColor, "FF000000", "FF112233")]
284+
public void SetColorTest2(string colorValue, FillType fillType, string expectedForeground, string expectedBackground)
285+
{
286+
Fill fill = new Fill();
287+
SrgbColor color = new SrgbColor(colorValue);
288+
Assert.Equal(Fill.DefaultColor, fill.ForegroundColor);
289+
Assert.Equal(Fill.DefaultColor, fill.BackgroundColor);
290+
Assert.Equal(PatternValue.None, fill.PatternFill);
291+
fill.SetColor(color, fillType);
292+
Assert.Equal(Color.ColorType.Rgb, fill.ForegroundColor.Type);
293+
Assert.Equal(Color.ColorType.Rgb, fill.BackgroundColor.Type);
294+
Assert.Equal(PatternValue.Solid, fill.PatternFill);
295+
Assert.Equal(expectedForeground, fill.ForegroundColor);
296+
Assert.Equal(expectedBackground, fill.BackgroundColor);
297+
}
298+
299+
[Theory(DisplayName = "Test of the SetColor function, when using an indexed color object")]
300+
[InlineData(IndexedColor.Value.Black, FillType.FillColor, true, "FF000000")]
301+
[InlineData(IndexedColor.Value.Black, FillType.PatternColor, false, "FF000000")]
302+
[InlineData(IndexedColor.Value.Blue4, FillType.FillColor, true, "FF000000")]
303+
[InlineData(IndexedColor.Value.Indigo, FillType.PatternColor, false, "FF000000")]
304+
[InlineData(IndexedColor.Value.SystemBackground, FillType.FillColor, true, "FF000000")]
305+
[InlineData(IndexedColor.Value.SystemForeground, FillType.PatternColor, false, "FF000000")]
306+
public void SetColorTest3(IndexedColor.Value indexedValue, FillType fillType, bool expectedForegroundHasColor, string expectedOppositeColor)
307+
{
308+
Fill fill = new Fill();
309+
IndexedColor color = new IndexedColor(indexedValue);
310+
Assert.Equal(Fill.DefaultColor, fill.ForegroundColor);
311+
Assert.Equal(Fill.DefaultColor, fill.BackgroundColor);
312+
Assert.Equal(PatternValue.None, fill.PatternFill);
313+
fill.SetColor(color, fillType);
314+
if (expectedForegroundHasColor)
315+
{
316+
Assert.Equal(Color.ColorType.Indexed, fill.ForegroundColor.Type);
317+
Assert.Equal(Color.ColorType.Rgb, fill.BackgroundColor.Type);
318+
Assert.Equal(color.GetArgbValue(), fill.ForegroundColor.GetArgbValue());
319+
Assert.Equal(expectedOppositeColor, fill.BackgroundColor);
320+
}
321+
else
322+
{
323+
Assert.Equal(Color.ColorType.Rgb, fill.ForegroundColor.Type);
324+
Assert.Equal(Color.ColorType.Indexed, fill.BackgroundColor.Type);
325+
Assert.Equal(expectedOppositeColor, fill.ForegroundColor);
326+
Assert.Equal(color.GetArgbValue(), fill.BackgroundColor.GetArgbValue());
327+
}
328+
Assert.Equal(PatternValue.Solid, fill.PatternFill);
329+
}
330+
331+
[Theory(DisplayName = "Test of the SetColor function, when using a theme color object")]
332+
[InlineData(Theme.ColorSchemeElement.Accent1, FillType.FillColor, true, "FF000000")]
333+
[InlineData(Theme.ColorSchemeElement.Accent1, FillType.PatternColor, false, "FF000000")]
334+
[InlineData(Theme.ColorSchemeElement.Dark1, FillType.FillColor, true, "FF000000")]
335+
[InlineData(Theme.ColorSchemeElement.Light1, FillType.PatternColor, false, "FF000000")]
336+
[InlineData(Theme.ColorSchemeElement.Hyperlink, FillType.FillColor, true, "FF000000")]
337+
[InlineData(Theme.ColorSchemeElement.FollowedHyperlink, FillType.PatternColor, false, "FF000000")]
338+
public void SetColorTest4(Theme.ColorSchemeElement themeValue, FillType fillType, bool expectedForegroundHasColor, string expectedOppositeColor)
339+
{
340+
Fill fill = new Fill();
341+
ThemeColor color = new ThemeColor(themeValue);
342+
Assert.Equal(Fill.DefaultColor, fill.ForegroundColor);
343+
Assert.Equal(Fill.DefaultColor, fill.BackgroundColor);
344+
Assert.Equal(PatternValue.None, fill.PatternFill);
345+
fill.SetColor(color, fillType);
346+
if (expectedForegroundHasColor)
347+
{
348+
Assert.Equal(Color.ColorType.Theme, fill.ForegroundColor.Type);
349+
Assert.Equal(Color.ColorType.Rgb, fill.BackgroundColor.Type);
350+
Assert.Equal(color.StringValue, fill.ForegroundColor.ThemeColor.StringValue);
351+
Assert.Equal(expectedOppositeColor, fill.BackgroundColor);
352+
}
353+
else
354+
{
355+
Assert.Equal(Color.ColorType.Rgb, fill.ForegroundColor.Type);
356+
Assert.Equal(Color.ColorType.Theme, fill.BackgroundColor.Type);
357+
Assert.Equal(expectedOppositeColor, fill.ForegroundColor);
358+
Assert.Equal(color.StringValue, fill.BackgroundColor.ThemeColor.StringValue);
359+
}
360+
Assert.Equal(PatternValue.Solid, fill.PatternFill);
361+
}
362+
363+
[Theory(DisplayName = "Test of the SetColor function, when using a system color object")]
364+
[InlineData(SystemColor.Value.ActiveBorder, FillType.FillColor, true, "FF000000")]
365+
[InlineData(SystemColor.Value.Background, FillType.PatternColor, false, "FF000000")]
366+
[InlineData(SystemColor.Value.ButtonFace, FillType.FillColor, true, "FF000000")]
367+
[InlineData(SystemColor.Value.Menu, FillType.PatternColor, false, "FF000000")]
368+
[InlineData(SystemColor.Value.Window, FillType.FillColor, true, "FF000000")]
369+
[InlineData(SystemColor.Value.CaptionText, FillType.PatternColor, false, "FF000000")]
370+
public void SetColorTest5(SystemColor.Value systemValue, FillType fillType, bool expectedForegroundHasColor, string expectedOppositeColor)
371+
{
372+
Fill fill = new Fill();
373+
SystemColor color = new SystemColor(systemValue);
374+
Assert.Equal(Fill.DefaultColor, fill.ForegroundColor);
375+
Assert.Equal(Fill.DefaultColor, fill.BackgroundColor);
376+
Assert.Equal(PatternValue.None, fill.PatternFill);
377+
fill.SetColor(color, fillType);
378+
if (expectedForegroundHasColor)
379+
{
380+
Assert.Equal(Color.ColorType.System, fill.ForegroundColor.Type);
381+
Assert.Equal(Color.ColorType.Rgb, fill.BackgroundColor.Type);
382+
Assert.Equal(color.StringValue, fill.ForegroundColor.SystemColor.StringValue);
383+
Assert.Equal(expectedOppositeColor, fill.BackgroundColor);
384+
}
385+
else
386+
{
387+
Assert.Equal(Color.ColorType.Rgb, fill.ForegroundColor.Type);
388+
Assert.Equal(Color.ColorType.System, fill.BackgroundColor.Type);
389+
Assert.Equal(expectedOppositeColor, fill.ForegroundColor);
390+
Assert.Equal(color.StringValue, fill.BackgroundColor.SystemColor.StringValue);
391+
}
392+
Assert.Equal(PatternValue.Solid, fill.PatternFill);
393+
}
394+
395+
[Theory(DisplayName = "Test of the SetColor function, when using an auto color object")]
396+
[InlineData(FillType.FillColor, true, "FF000000")]
397+
[InlineData(FillType.PatternColor, false, "FF000000")]
398+
public void SetColorTest6(FillType fillType, bool expectedForegroundHasColor, string expectedOppositeColor)
399+
{
400+
Fill fill = new Fill();
401+
AutoColor color = new AutoColor();
402+
Assert.Equal(Fill.DefaultColor, fill.ForegroundColor);
403+
Assert.Equal(Fill.DefaultColor, fill.BackgroundColor);
404+
Assert.Equal(PatternValue.None, fill.PatternFill);
405+
fill.SetColor(color, fillType);
406+
if (expectedForegroundHasColor)
407+
{
408+
Assert.Equal(Color.ColorType.Auto, fill.ForegroundColor.Type);
409+
Assert.Equal(Color.ColorType.Rgb, fill.BackgroundColor.Type);
410+
Assert.True(fill.ForegroundColor.Auto);
411+
Assert.False(fill.BackgroundColor.Auto);
412+
Assert.Equal(expectedOppositeColor, fill.BackgroundColor);
413+
}
414+
else
415+
{
416+
Assert.Equal(Color.ColorType.Rgb, fill.ForegroundColor.Type);
417+
Assert.Equal(Color.ColorType.Auto, fill.BackgroundColor.Type);
418+
Assert.False(fill.ForegroundColor.Auto);
419+
Assert.True(fill.BackgroundColor.Auto);
420+
Assert.Equal(expectedOppositeColor, fill.ForegroundColor);
421+
}
422+
Assert.Equal(PatternValue.Solid, fill.PatternFill);
423+
}
424+
425+
[Theory(DisplayName = "Test of the SetColor function, when a compound color object was passed")]
426+
[InlineData(FillType.FillColor, true, "FF000000")]
427+
[InlineData(FillType.PatternColor, false, "FF000000")]
428+
public void SetColorTest7(FillType fillType, bool expectedForegroundHasColor, string expectedOppositeColor)
429+
{
430+
Fill fill = new Fill();
431+
Color color = Color.CreateRgb("FF112233");
432+
Assert.Equal(Fill.DefaultColor, fill.ForegroundColor);
433+
Assert.Equal(Fill.DefaultColor, fill.BackgroundColor);
434+
Assert.Equal(PatternValue.None, fill.PatternFill);
435+
fill.SetColor(color, fillType);
436+
if (expectedForegroundHasColor)
437+
{
438+
Assert.Equal(Color.ColorType.Rgb, fill.ForegroundColor.Type);
439+
Assert.Equal(Color.ColorType.Rgb, fill.BackgroundColor.Type);
440+
Assert.Equal(color.GetArgbValue(), fill.ForegroundColor.GetArgbValue());
441+
Assert.Equal(expectedOppositeColor, fill.BackgroundColor);
442+
}
443+
else
444+
{
445+
Assert.Equal(Color.ColorType.Rgb, fill.ForegroundColor.Type);
446+
Assert.Equal(Color.ColorType.Rgb, fill.BackgroundColor.Type);
447+
Assert.Equal(expectedOppositeColor, fill.ForegroundColor);
448+
Assert.Equal(color.GetArgbValue(), fill.BackgroundColor.GetArgbValue());
449+
}
450+
Assert.Equal(PatternValue.Solid, fill.PatternFill);
451+
}
452+
453+
280454
[Theory(DisplayName = "Test of the ValidateColor function")]
281455
[InlineData("", false, false, false)]
282456
[InlineData(null, false, false, false)]

0 commit comments

Comments
 (0)