Skip to content

Commit a126b6b

Browse files
committed
fix(color): return a stable name for colors with aliases
1 parent f177de4 commit a126b6b

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

‎color/color.go‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,11 +1056,18 @@ func (c Color) String() string {
10561056
// if passed true as an argument it will falls back to
10571057
// the CSS hex string if no W3C name found '#ABCDEF'
10581058
func (c Color) Name(css ...bool) string {
1059+
// Some colors have more than one name (e.g. "gray" and "grey"), and map
1060+
// iteration order is random, so pick the alphabetically first name to
1061+
// keep the result stable.
1062+
found := ""
10591063
for name, hex := range Names {
1060-
if c == hex {
1061-
return name
1064+
if c == hex && (found == "" || name < found) {
1065+
found = name
10621066
}
10631067
}
1068+
if found != "" {
1069+
return found
1070+
}
10641071
if len(css) > 0 && css[0] {
10651072
return c.CSS()
10661073
}

0 commit comments

Comments
 (0)