update gradient color stop validity check

This commit is contained in:
melsonic 2025-04-18 21:22:07 +05:30
parent 9f66199777
commit 909eeb00af
No known key found for this signature in database
GPG key ID: DFA426742F621CD7
2 changed files with 16 additions and 11 deletions

View file

@ -512,7 +512,19 @@ var NamedColors = []string{
var ColorHexRegex = regexp.MustCompile(`^#(([0-9a-fA-F]{2}){3}|([0-9a-fA-F]){3})$`)
func ValidColor(color string) bool {
if !go2.Contains(NamedColors, strings.ToLower(color)) && !ColorHexRegex.MatchString(color) && !IsGradient(color) {
if IsGradient(color) {
gradient, err := ParseGradient(color)
for _, colorStop := range gradient.ColorStops {
_, err = csscolorparser.Parse(colorStop.Color)
if err != nil {
break
}
}
return err == nil
}
if !go2.Contains(NamedColors, strings.ToLower(color)) && !ColorHexRegex.MatchString(color) {
return false
}

View file

@ -9,8 +9,6 @@ import (
"regexp"
"strconv"
"strings"
"github.com/mazznoer/csscolorparser"
)
type Gradient struct {
@ -243,15 +241,10 @@ func UniqueGradientID(cssGradient string) string {
return "grad-" + hash
}
var GradientRegex = regexp.MustCompile(`^(linear|radial)-gradient\((.+)\)$`)
func IsGradient(color string) bool {
gradient, err := ParseGradient(color)
for _, colorStop := range gradient.ColorStops {
_, err = csscolorparser.Parse(colorStop.Color)
if err != nil {
break
}
}
return err == nil
return GradientRegex.MatchString(color)
}
var URLGradientID = regexp.MustCompile(`^url\('#grad-[a-f0-9]{40}'\)$`)