using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; namespace MECF.Framework.Common.Utilities { public class ColorUtil { public static string Color_Defalult = "#c9c9c9"; public static string Color_Yellow = "#fcff6d"; /// /// 校验字符串是否为有效的16进制颜色代码。 /// 支持带#号或不带#号的格式,例如 #FFFFFF 或 FFFFFF。 /// /// 待校验的颜色代码 /// 如果是有效的16进制颜色代码,则返回true;否则返回false。 public static bool IsValidHexColor(string hexColor) { if (string.IsNullOrEmpty(hexColor)) return false; // 去除可能的#号 hexColor = hexColor.TrimStart('#'); // 检查长度是否为6(RGB)或8(ARGB) if (hexColor.Length != 6 && hexColor.Length != 8) return false; // 正则表达式匹配 Regex hexColorRegex = new Regex(@"^[0-9A-Fa-f]+$"); return hexColorRegex.IsMatch(hexColor); } } }