Browse Source

Improvement (#528): added validation for color arguments

MarcinZiabek 2 years ago
parent
commit
9809faccb6

+ 2 - 0
Source/QuestPDF/Fluent/BorderExtensions.cs

@@ -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);
         }
     }

+ 2 - 0
Source/QuestPDF/Fluent/DebugExtensions.cs

@@ -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

+ 3 - 0
Source/QuestPDF/Fluent/ElementExtensions.cs

@@ -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

+ 2 - 0
Source/QuestPDF/Fluent/LineExtensions.cs

@@ -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;
         }
     }

+ 1 - 0
Source/QuestPDF/Fluent/PageExtensions.cs

@@ -110,6 +110,7 @@ namespace QuestPDF.Fluent
         
         public void PageColor(string color)
         {
+            ColorValidator.Validate(color);
             Page.BackgroundColor = color;
         }
         

+ 3 - 0
Source/QuestPDF/Fluent/TextSpanDescriptorExtensions.cs

@@ -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;
         }

+ 3 - 0
Source/QuestPDF/Fluent/TextStyleExtensions.cs

@@ -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);
         }
         

+ 41 - 0
Source/QuestPDF/Helpers/ColorValidator.cs

@@ -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;
+                }
+            }
+        }
+    }
+}