Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions internal/color/validate.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package color

// IsHexColor reports whether value is a safe three- or six-digit hex color.
// IsHexColor reports whether value is a safe 3-, 4-, 6-, or 8-digit hex color.
// Eight-digit form is #RRGGBBAA (used by templates such as aether.zed.json).
func IsHexColor(value string) bool {
if len(value) != 4 && len(value) != 7 {
switch len(value) {
case 4, 5, 7, 9:
default:
return false
}
if value[0] != '#' {
Expand Down
21 changes: 21 additions & 0 deletions internal/color/validate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package color

import "testing"

func TestIsHexColor(t *testing.T) {
cases := map[string]bool{
"#fff": true,
"#ffffff": true,
"#ffffffff": true,
"#FFFF": true,
"#ggg": false,
"#fffffff": false,
"ffffff": false,
"": false,
}
for in, want := range cases {
if got := IsHexColor(in); got != want {
t.Fatalf("IsHexColor(%q)=%v want %v", in, got, want)
}
}
}