ColorValidator.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. using System;
  2. using System.Collections.Concurrent;
  3. using QuestPDF.Skia;
  4. namespace QuestPDF.Helpers
  5. {
  6. internal static class ColorValidator
  7. {
  8. static ColorValidator()
  9. {
  10. NativeDependencyCompatibilityChecker.Test();
  11. }
  12. private static readonly ConcurrentDictionary<string, bool> ColorsValidityCache = new();
  13. public static void Validate(string? color)
  14. {
  15. if (color == null)
  16. throw new ArgumentException("Color value cannot be null");
  17. var isValid = ColorsValidityCache.GetOrAdd(color, x => SkColor.TryParse(x, out var _));
  18. if (isValid)
  19. return;
  20. throw new ArgumentException(
  21. $"The provided value '{color}' is not a valid hex color. " +
  22. "The following formats are supported: #RGB, #ARGB, #RRGGBB, #AARRGGBB. " +
  23. "The hash sign is optional so the following formats are also valid: RGB, ARGB, RRGGBB, AARRGGBB. " +
  24. "For example #FF8800 is a solid orange color, while #20CF is a barely visible aqua color.");
  25. }
  26. }
  27. }