Improvement (#528): added validation for color arguments
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using QuestPDF.Elements;
|
||||
using QuestPDF.Helpers;
|
||||
using QuestPDF.Infrastructure;
|
||||
|
||||
namespace QuestPDF.Fluent
|
||||
@@ -57,6 +58,7 @@ namespace QuestPDF.Fluent
|
||||
|
||||
public static IContainer BorderColor(this IContainer element, string color)
|
||||
{
|
||||
ColorValidator.Validate(color);
|
||||
return element.Border(x => x.Color = color);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,8 @@ namespace QuestPDF.Fluent
|
||||
{
|
||||
public static IContainer DebugArea(this IContainer parent, string text, string color)
|
||||
{
|
||||
ColorValidator.Validate(color);
|
||||
|
||||
var container = new Container();
|
||||
|
||||
parent.Component(new DebugArea
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using QuestPDF.Drawing.Exceptions;
|
||||
using QuestPDF.Elements;
|
||||
using QuestPDF.Helpers;
|
||||
using QuestPDF.Infrastructure;
|
||||
|
||||
namespace QuestPDF.Fluent
|
||||
@@ -52,6 +53,8 @@ namespace QuestPDF.Fluent
|
||||
|
||||
public static IContainer Background(this IContainer element, string color)
|
||||
{
|
||||
ColorValidator.Validate(color);
|
||||
|
||||
return element.Element(new Background
|
||||
{
|
||||
Color = color
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using QuestPDF.Elements;
|
||||
using QuestPDF.Helpers;
|
||||
using QuestPDF.Infrastructure;
|
||||
|
||||
namespace QuestPDF.Fluent
|
||||
@@ -30,6 +31,7 @@ namespace QuestPDF.Fluent
|
||||
|
||||
public static void LineColor(this ILine descriptor, string value)
|
||||
{
|
||||
ColorValidator.Validate(value);
|
||||
(descriptor as Line).Color = value;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -110,6 +110,7 @@ namespace QuestPDF.Fluent
|
||||
|
||||
public void PageColor(string color)
|
||||
{
|
||||
ColorValidator.Validate(color);
|
||||
Page.BackgroundColor = color;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using QuestPDF.Helpers;
|
||||
using QuestPDF.Infrastructure;
|
||||
|
||||
namespace QuestPDF.Fluent
|
||||
@@ -28,12 +29,14 @@ namespace QuestPDF.Fluent
|
||||
|
||||
public static T FontColor<T>(this T descriptor, string value) where T : TextSpanDescriptor
|
||||
{
|
||||
ColorValidator.Validate(value);
|
||||
descriptor.MutateTextStyle(x => x.FontColor(value));
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
public static T BackgroundColor<T>(this T descriptor, string value) where T : TextSpanDescriptor
|
||||
{
|
||||
ColorValidator.Validate(value);
|
||||
descriptor.MutateTextStyle(x => x.BackgroundColor(value));
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using QuestPDF.Helpers;
|
||||
using QuestPDF.Infrastructure;
|
||||
|
||||
namespace QuestPDF.Fluent
|
||||
@@ -14,11 +15,13 @@ namespace QuestPDF.Fluent
|
||||
|
||||
public static TextStyle FontColor(this TextStyle style, string value)
|
||||
{
|
||||
ColorValidator.Validate(value);
|
||||
return style.Mutate(TextStyleProperty.Color, value);
|
||||
}
|
||||
|
||||
public static TextStyle BackgroundColor(this TextStyle style, string value)
|
||||
{
|
||||
ColorValidator.Validate(value);
|
||||
return style.Mutate(TextStyleProperty.BackgroundColor, value);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using SkiaSharp;
|
||||
|
||||
namespace QuestPDF.Helpers
|
||||
{
|
||||
internal static class ColorValidator
|
||||
{
|
||||
private static readonly ConcurrentDictionary<string, bool> Colors = new();
|
||||
|
||||
public static void Validate(string? color)
|
||||
{
|
||||
if (color == null)
|
||||
throw new ArgumentException("Color value cannot be null");
|
||||
|
||||
var isValid = Colors.GetOrAdd(color, IsColorValid);
|
||||
|
||||
if (isValid)
|
||||
return;
|
||||
|
||||
throw new ArgumentException(
|
||||
$"The provided value '{color}' is not a valid hex color. " +
|
||||
"The following formats are supported: #RGB, #ARGB, #RRGGBB, #AARRGGBB. " +
|
||||
"The hash sign is optional so the following formats are also valid: RGB, ARGB, RRGGBB, AARRGGBB. " +
|
||||
"For example #FF8800 is a solid orange color, while #20CF is a barely visible aqua color.");
|
||||
|
||||
static bool IsColorValid(string color)
|
||||
{
|
||||
try
|
||||
{
|
||||
SKColor.Parse(color);
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user