ColorUtil.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7. namespace MECF.Framework.Common.Utilities
  8. {
  9. public class ColorUtil
  10. {
  11. public static string Color_Defalult = "#c9c9c9";
  12. public static string Color_Yellow = "#fcff6d";
  13. /// <summary>
  14. /// 校验字符串是否为有效的16进制颜色代码。
  15. /// 支持带#号或不带#号的格式,例如 #FFFFFF 或 FFFFFF。
  16. /// </summary>
  17. /// <param name="hexColor">待校验的颜色代码</param>
  18. /// <returns>如果是有效的16进制颜色代码,则返回true;否则返回false。</returns>
  19. public static bool IsValidHexColor(string hexColor)
  20. {
  21. if (string.IsNullOrEmpty(hexColor)) return false;
  22. // 去除可能的#号
  23. hexColor = hexColor.TrimStart('#');
  24. // 检查长度是否为6(RGB)或8(ARGB)
  25. if (hexColor.Length != 6 && hexColor.Length != 8) return false;
  26. // 正则表达式匹配
  27. Regex hexColorRegex = new Regex(@"^[0-9A-Fa-f]+$");
  28. return hexColorRegex.IsMatch(hexColor);
  29. }
  30. }
  31. }