{"version":3,"file":"color-rules.mjs","names":[],"sources":["../../../../../../../../@warlock.js/seal/src/rules/color/color-rules.ts"],"sourcesContent":["import { invalidRule, VALID_RULE } from \"../../helpers\";\r\nimport type { SchemaRule } from \"../../types\";\r\n\r\n/**\r\n * Color validation regex patterns\r\n */\r\nconst colorValidationRegex = {\r\n  hex: /^#([0-9a-f]{3}){1,2}$/i,\r\n  rgb: /^rgb\\((\\d{1,3}),\\s*(\\d{1,3}),\\s*(\\d{1,3})\\)$/,\r\n  rgba: /^rgba\\((\\d{1,3}),\\s*(\\d{1,3}),\\s*(\\d{1,3}),\\s*(0?\\.\\d+|1)\\)$/,\r\n  hsl: /^hsl\\((\\d{1,3}),\\s*(\\d{1,3})%?,\\s*(\\d{1,3})%?\\)$/,\r\n};\r\n\r\n/**\r\n * Generic color rule - validates any color format\r\n */\r\nexport const colorRule: SchemaRule = {\r\n  name: \"color\",\r\n  defaultErrorMessage: \"The :input must be a valid color\",\r\n  async validate(value: any, context) {\r\n    const isValidColor =\r\n      colorValidationRegex.hex.test(value) ||\r\n      colorValidationRegex.rgb.test(value) ||\r\n      colorValidationRegex.rgba.test(value) ||\r\n      colorValidationRegex.hsl.test(value);\r\n\r\n    if (isValidColor) {\r\n      return VALID_RULE;\r\n    }\r\n\r\n    return invalidRule(this, context);\r\n  },\r\n};\r\n\r\n/**\r\n * Hex color rule\r\n */\r\nexport const hexColorRule: SchemaRule = {\r\n  name: \"hexColor\",\r\n  defaultErrorMessage: \"The :input must be a valid hex color\",\r\n  async validate(value: any, context) {\r\n    if (colorValidationRegex.hex.test(value)) {\r\n      return VALID_RULE;\r\n    }\r\n    return invalidRule(this, context);\r\n  },\r\n};\r\n\r\n/**\r\n * RGB color rule\r\n */\r\nexport const rgbColorRule: SchemaRule = {\r\n  name: \"rgbColor\",\r\n  defaultErrorMessage: \"The :input must be a valid RGB color\",\r\n  async validate(value: any, context) {\r\n    if (colorValidationRegex.rgb.test(value)) {\r\n      return VALID_RULE;\r\n    }\r\n    return invalidRule(this, context);\r\n  },\r\n};\r\n\r\n/**\r\n * RGBA color rule\r\n */\r\nexport const rgbaColorRule: SchemaRule = {\r\n  name: \"rgbaColor\",\r\n  defaultErrorMessage: \"The :input must be a valid RGBA color\",\r\n  async validate(value: any, context) {\r\n    if (colorValidationRegex.rgba.test(value)) {\r\n      return VALID_RULE;\r\n    }\r\n    return invalidRule(this, context);\r\n  },\r\n};\r\n\r\n/**\r\n * HSL color rule\r\n */\r\nexport const hslColorRule: SchemaRule = {\r\n  name: \"hslColor\",\r\n  defaultErrorMessage: \"The :input must be a valid HSL color\",\r\n  async validate(value: any, context) {\r\n    if (colorValidationRegex.hsl.test(value)) {\r\n      return VALID_RULE;\r\n    }\r\n    return invalidRule(this, context);\r\n  },\r\n};\r\n\r\n/**\r\n * Light color rule\r\n */\r\nexport const lightColorRule: SchemaRule = {\r\n  name: \"lightColor\",\r\n  defaultErrorMessage: \"The :input must be a light color\",\r\n  async validate(value: any, context) {\r\n    if (!colorValidationRegex.hex.test(value)) {\r\n      return invalidRule(this, context);\r\n    }\r\n\r\n    const hex = value.replace(\"#\", \"\");\r\n    const r = parseInt(hex.substring(0, 2), 16);\r\n    const g = parseInt(hex.substring(2, 4), 16);\r\n    const b = parseInt(hex.substring(4, 6), 16);\r\n    const brightness = (r * 299 + g * 587 + b * 114) / 1000;\r\n\r\n    if (brightness > 155) {\r\n      return VALID_RULE;\r\n    }\r\n\r\n    return invalidRule(this, context);\r\n  },\r\n};\r\n\r\n/**\r\n * Dark color rule\r\n */\r\nexport const darkColorRule: SchemaRule = {\r\n  name: \"darkColor\",\r\n  defaultErrorMessage: \"The :input must be a dark color\",\r\n  async validate(value: any, context) {\r\n    if (!colorValidationRegex.hex.test(value)) {\r\n      return invalidRule(this, context);\r\n    }\r\n\r\n    const hex = value.replace(\"#\", \"\");\r\n    const r = parseInt(hex.substring(0, 2), 16);\r\n    const g = parseInt(hex.substring(2, 4), 16);\r\n    const b = parseInt(hex.substring(4, 6), 16);\r\n    const brightness = (r * 299 + g * 587 + b * 114) / 1000;\r\n\r\n    if (brightness <= 155) {\r\n      return VALID_RULE;\r\n    }\r\n\r\n    return invalidRule(this, context);\r\n  },\r\n};\r\n"],"mappings":";;;;;;;AAMA,MAAM,uBAAuB;CAC3B,KAAK;CACL,KAAK;CACL,MAAM;CACN,KAAK;AACP;;;;AAKA,MAAa,YAAwB;CACnC,MAAM;CACN,qBAAqB;CACrB,MAAM,SAAS,OAAY,SAAS;EAOlC,IALE,qBAAqB,IAAI,KAAK,KAAK,KACnC,qBAAqB,IAAI,KAAK,KAAK,KACnC,qBAAqB,KAAK,KAAK,KAAK,KACpC,qBAAqB,IAAI,KAAK,KAAK,GAGnC,OAAO;EAGT,OAAO,YAAY,MAAM,OAAO;CAClC;AACF;;;;AAKA,MAAa,eAA2B;CACtC,MAAM;CACN,qBAAqB;CACrB,MAAM,SAAS,OAAY,SAAS;EAClC,IAAI,qBAAqB,IAAI,KAAK,KAAK,GACrC,OAAO;EAET,OAAO,YAAY,MAAM,OAAO;CAClC;AACF;;;;AAKA,MAAa,eAA2B;CACtC,MAAM;CACN,qBAAqB;CACrB,MAAM,SAAS,OAAY,SAAS;EAClC,IAAI,qBAAqB,IAAI,KAAK,KAAK,GACrC,OAAO;EAET,OAAO,YAAY,MAAM,OAAO;CAClC;AACF;;;;AAKA,MAAa,gBAA4B;CACvC,MAAM;CACN,qBAAqB;CACrB,MAAM,SAAS,OAAY,SAAS;EAClC,IAAI,qBAAqB,KAAK,KAAK,KAAK,GACtC,OAAO;EAET,OAAO,YAAY,MAAM,OAAO;CAClC;AACF;;;;AAKA,MAAa,eAA2B;CACtC,MAAM;CACN,qBAAqB;CACrB,MAAM,SAAS,OAAY,SAAS;EAClC,IAAI,qBAAqB,IAAI,KAAK,KAAK,GACrC,OAAO;EAET,OAAO,YAAY,MAAM,OAAO;CAClC;AACF;;;;AAKA,MAAa,iBAA6B;CACxC,MAAM;CACN,qBAAqB;CACrB,MAAM,SAAS,OAAY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