1
0
mirror of https://github.com/6543/go-hexcolor synced 2024-12-18 15:54:17 +00:00

resolve CSS3 named colors

This commit is contained in:
6543 2020-09-11 01:07:43 +02:00
parent d071c61804
commit fe0e8e86d6
Signed by: 6543
GPG Key ID: A1CA74D27FD13271
3 changed files with 326 additions and 3 deletions

View File

@ -18,6 +18,7 @@ func NewHexColor(hc string) (*HexColor, error) {
c := &HexColor{original: hc}
hc = strings.TrimLeft(strings.ToLower(hc), "#")

// normalize hex color
if _, err := strconv.ParseUint(hc, 16, 24); err == nil {
if len(hc) == 6 {
c.hex = hc
@ -29,7 +30,12 @@ func NewHexColor(hc string) (*HexColor, error) {
}
}

// handle named colors
// resolve named color to hex color
val, exist := CSS3ColorMap[strings.TrimSpace(hc)]
if exist {
c.hex = string(val)
return c, nil
}

return nil, fmt.Errorf("Malformed color: %s", hc)
}

View File

@ -15,7 +15,14 @@ func TestNewHexColor(t *testing.T) {
{"#dd22cc", "dd22cc", false},
{"#adf", "aaddff", false},
{"15d", "1155dd", false},
{"a5i", "", true},
{"15di", "1155dd", true},
{"2e8b57", "2e8b57", false},
{"2e8b572e8b57", "", true},

{"Red", "ff0000", false},
{"blue", "0000ff", false},
{"reed", "", true},
{"", "", true},
}

for _, tc := range cases {

312
named.go
View File

@ -1 +1,311 @@
package go_hexcolor
package hexcolor

type ANSIColor string

// CSS3 named colors
// ref: https://drafts.csswg.org/css-color-3/
type CSS3Color string

const (
Aliceblue CSS3Color = "f0f8ff"
Antiquewhite CSS3Color = "faebd7"
Aqua CSS3Color = "00ffff"
Aquamarine CSS3Color = "7fffd4"
Azure CSS3Color = "f0ffff"
Beige CSS3Color = "f5f5dc"
Bisque CSS3Color = "ffe4c4"
Black CSS3Color = "000000"
Blanchedalmond CSS3Color = "ffebcd"
Blue CSS3Color = "0000ff"
Blueviolet CSS3Color = "8a2be2"
Brown CSS3Color = "a52a2a"
Burlywood CSS3Color = "deb887"
Cadetblue CSS3Color = "5f9ea0"
Chartreuse CSS3Color = "7fff00"
Chocolate CSS3Color = "d2691e"
Coral CSS3Color = "ff7f50"
Cornflowerblue CSS3Color = "6495ed"
Cornsilk CSS3Color = "fff8dc"
Crimson CSS3Color = "dc143c"
Cyan CSS3Color = "00ffff"
Darkblue CSS3Color = "00008b"
Darkcyan CSS3Color = "008b8b"
Darkgoldenrod CSS3Color = "b8860b"
Darkgray CSS3Color = "a9a9a9"
Darkgreen CSS3Color = "006400"
Darkgrey CSS3Color = "a9a9a9"
Darkkhaki CSS3Color = "bdb76b"
Darkmagenta CSS3Color = "8b008b"
Darkolivegreen CSS3Color = "556b2f"
Darkorange CSS3Color = "ff8c00"
Darkorchid CSS3Color = "9932cc"
Darkred CSS3Color = "8b0000"
Darksalmon CSS3Color = "e9967a"
Darkseagreen CSS3Color = "8fbc8f"
Darkslateblue CSS3Color = "483d8b"
Darkslategray CSS3Color = "2f4f4f"
Darkslategrey CSS3Color = "2f4f4f"
Darkturquoise CSS3Color = "00ced1"
Darkviolet CSS3Color = "9400d3"
Deeppink CSS3Color = "ff1493"
Deepskyblue CSS3Color = "00bfff"
Dimgray CSS3Color = "696969"
Dimgrey CSS3Color = "696969"
Dodgerblue CSS3Color = "1e90ff"
Firebrick CSS3Color = "b22222"
Floralwhite CSS3Color = "fffaf0"
Forestgreen CSS3Color = "228b22"
Fuchsia CSS3Color = "ff00ff"
Gainsboro CSS3Color = "dcdcdc"
Ghostwhite CSS3Color = "f8f8ff"
Gold CSS3Color = "ffd700"
Goldenrod CSS3Color = "daa520"
Gray CSS3Color = "808080"
Green CSS3Color = "008000"
Greenyellow CSS3Color = "adff2f"
Grey CSS3Color = "808080"
Honeydew CSS3Color = "f0fff0"
Hotpink CSS3Color = "ff69b4"
Indianred CSS3Color = "cd5c5c"
Indigo CSS3Color = "4b0082"
Ivory CSS3Color = "fffff0"
Khaki CSS3Color = "f0e68c"
Lavender CSS3Color = "e6e6fa"
Lavenderblush CSS3Color = "fff0f5"
Lawngreen CSS3Color = "7cfc00"
Lemonchiffon CSS3Color = "fffacd"
Lightblue CSS3Color = "add8e6"
Lightcoral CSS3Color = "f08080"
Lightcyan CSS3Color = "e0ffff"
Lightgoldenrodyellow CSS3Color = "fafad2"
Lightgray CSS3Color = "d3d3d3"
Lightgreen CSS3Color = "90ee90"
Lightgrey CSS3Color = "d3d3d3"
Lightpink CSS3Color = "ffb6c1"
Lightsalmon CSS3Color = "ffa07a"
Lightseagreen CSS3Color = "20b2aa"
Lightskyblue CSS3Color = "87cefa"
Lightslategray CSS3Color = "778899"
Lightslategrey CSS3Color = "778899"
Lightsteelblue CSS3Color = "b0c4de"
Lightyellow CSS3Color = "ffffe0"
Lime CSS3Color = "00ff00"
Limegreen CSS3Color = "32cd32"
Linen CSS3Color = "faf0e6"
Magenta CSS3Color = "ff00ff"
Maroon CSS3Color = "800000"
Mediumaquamarine CSS3Color = "66cdaa"
Mediumblue CSS3Color = "0000cd"
Mediumorchid CSS3Color = "ba55d3"
Mediumpurple CSS3Color = "9370db"
Mediumseagreen CSS3Color = "3cb371"
Mediumslateblue CSS3Color = "7b68ee"
Mediumspringgreen CSS3Color = "00fa9a"
Mediumturquoise CSS3Color = "48d1cc"
Mediumvioletred CSS3Color = "c71585"
Midnightblue CSS3Color = "191970"
Mintcream CSS3Color = "f5fffa"
Mistyrose CSS3Color = "ffe4e1"
Moccasin CSS3Color = "ffe4b5"
Navajowhite CSS3Color = "ffdead"
Navy CSS3Color = "000080"
Oldlace CSS3Color = "fdf5e6"
Olive CSS3Color = "808000"
Olivedrab CSS3Color = "6b8e23"
Orange CSS3Color = "ffa500"
Orangered CSS3Color = "ff4500"
Orchid CSS3Color = "da70d6"
Palegoldenrod CSS3Color = "eee8aa"
Palegreen CSS3Color = "98fb98"
Paleturquoise CSS3Color = "afeeee"
Palevioletred CSS3Color = "db7093"
Papayawhip CSS3Color = "ffefd5"
Peachpuff CSS3Color = "ffdab9"
Peru CSS3Color = "cd853f"
Pink CSS3Color = "ffc0cb"
Plum CSS3Color = "dda0dd"
Powderblue CSS3Color = "b0e0e6"
Purple CSS3Color = "800080"
Red CSS3Color = "ff0000"
Rosybrown CSS3Color = "bc8f8f"
Royalblue CSS3Color = "4169e1"
Saddlebrown CSS3Color = "8b4513"
Salmon CSS3Color = "fa8072"
Sandybrown CSS3Color = "f4a460"
Seagreen CSS3Color = "2e8b57"
Seashell CSS3Color = "fff5ee"
Sienna CSS3Color = "a0522d"
Silver CSS3Color = "c0c0c0"
Skyblue CSS3Color = "87ceeb"
Slateblue CSS3Color = "6a5acd"
Slategray CSS3Color = "708090"
Slategrey CSS3Color = "708090"
Snow CSS3Color = "fffafa"
Springgreen CSS3Color = "00ff7f"
Steelblue CSS3Color = "4682b4"
Tan CSS3Color = "d2b48c"
Teal CSS3Color = "008080"
Thistle CSS3Color = "d8bfd8"
Tomato CSS3Color = "ff6347"
Turquoise CSS3Color = "40e0d0"
Violet CSS3Color = "ee82ee"
Wheat CSS3Color = "f5deb3"
White CSS3Color = "ffffff"
Whitesmoke CSS3Color = "f5f5f5"
Yellow CSS3Color = "ffff00"
Yellowgreen CSS3Color = "9acd32"
)

var CSS3ColorMap map[string]CSS3Color

func init() {
CSS3ColorMap = make(map[string]CSS3Color)

CSS3ColorMap["aliceblue"] = Aliceblue
CSS3ColorMap["antiquewhite"] = Antiquewhite
CSS3ColorMap["aqua"] = Aqua
CSS3ColorMap["aquamarine"] = Aquamarine
CSS3ColorMap["azure"] = Azure
CSS3ColorMap["beige"] = Beige
CSS3ColorMap["bisque"] = Bisque
CSS3ColorMap["black"] = Black
CSS3ColorMap["blanchedalmond"] = Blanchedalmond
CSS3ColorMap["blue"] = Blue
CSS3ColorMap["blueviolet"] = Blueviolet
CSS3ColorMap["brown"] = Brown
CSS3ColorMap["burlywood"] = Burlywood
CSS3ColorMap["cadetblue"] = Cadetblue
CSS3ColorMap["chartreuse"] = Chartreuse
CSS3ColorMap["chocolate"] = Chocolate
CSS3ColorMap["coral"] = Coral
CSS3ColorMap["cornflowerblue"] = Cornflowerblue
CSS3ColorMap["cornsilk"] = Cornsilk
CSS3ColorMap["crimson"] = Crimson
CSS3ColorMap["cyan"] = Cyan
CSS3ColorMap["darkblue"] = Darkblue
CSS3ColorMap["darkcyan"] = Darkcyan
CSS3ColorMap["darkgoldenrod"] = Darkgoldenrod
CSS3ColorMap["darkgray"] = Darkgray
CSS3ColorMap["darkgreen"] = Darkgreen
CSS3ColorMap["darkgrey"] = Darkgrey
CSS3ColorMap["darkkhaki"] = Darkkhaki
CSS3ColorMap["darkmagenta"] = Darkmagenta
CSS3ColorMap["darkolivegreen"] = Darkolivegreen
CSS3ColorMap["darkorange"] = Darkorange
CSS3ColorMap["darkorchid"] = Darkorchid
CSS3ColorMap["darkred"] = Darkred
CSS3ColorMap["darksalmon"] = Darksalmon
CSS3ColorMap["darkseagreen"] = Darkseagreen
CSS3ColorMap["darkslateblue"] = Darkslateblue
CSS3ColorMap["darkslategray"] = Darkslategray
CSS3ColorMap["darkslategrey"] = Darkslategrey
CSS3ColorMap["darkturquoise"] = Darkturquoise
CSS3ColorMap["darkviolet"] = Darkviolet
CSS3ColorMap["deeppink"] = Deeppink
CSS3ColorMap["deepskyblue"] = Deepskyblue
CSS3ColorMap["dimgray"] = Dimgray
CSS3ColorMap["dimgrey"] = Dimgrey
CSS3ColorMap["dodgerblue"] = Dodgerblue
CSS3ColorMap["firebrick"] = Firebrick
CSS3ColorMap["floralwhite"] = Floralwhite
CSS3ColorMap["forestgreen"] = Forestgreen
CSS3ColorMap["fuchsia"] = Fuchsia
CSS3ColorMap["gainsboro"] = Gainsboro
CSS3ColorMap["ghostwhite"] = Ghostwhite
CSS3ColorMap["gold"] = Gold
CSS3ColorMap["goldenrod"] = Goldenrod
CSS3ColorMap["gray"] = Gray
CSS3ColorMap["green"] = Green
CSS3ColorMap["greenyellow"] = Greenyellow
CSS3ColorMap["grey"] = Grey
CSS3ColorMap["honeydew"] = Honeydew
CSS3ColorMap["hotpink"] = Hotpink
CSS3ColorMap["indianred"] = Indianred
CSS3ColorMap["indigo"] = Indigo
CSS3ColorMap["ivory"] = Ivory
CSS3ColorMap["khaki"] = Khaki
CSS3ColorMap["lavender"] = Lavender
CSS3ColorMap["lavenderblush"] = Lavenderblush
CSS3ColorMap["lawngreen"] = Lawngreen
CSS3ColorMap["lemonchiffon"] = Lemonchiffon
CSS3ColorMap["lightblue"] = Lightblue
CSS3ColorMap["lightcoral"] = Lightcoral
CSS3ColorMap["lightcyan"] = Lightcyan
CSS3ColorMap["lightgoldenrodyellow"] = Lightgoldenrodyellow
CSS3ColorMap["lightgray"] = Lightgray
CSS3ColorMap["lightgreen"] = Lightgreen
CSS3ColorMap["lightgrey"] = Lightgrey
CSS3ColorMap["lightpink"] = Lightpink
CSS3ColorMap["lightsalmon"] = Lightsalmon
CSS3ColorMap["lightseagreen"] = Lightseagreen
CSS3ColorMap["lightskyblue"] = Lightskyblue
CSS3ColorMap["lightslategray"] = Lightslategray
CSS3ColorMap["lightslategrey"] = Lightslategrey
CSS3ColorMap["lightsteelblue"] = Lightsteelblue
CSS3ColorMap["lightyellow"] = Lightyellow
CSS3ColorMap["lime"] = Lime
CSS3ColorMap["limegreen"] = Limegreen
CSS3ColorMap["linen"] = Linen
CSS3ColorMap["magenta"] = Magenta
CSS3ColorMap["maroon"] = Maroon
CSS3ColorMap["mediumaquamarine"] = Mediumaquamarine
CSS3ColorMap["mediumblue"] = Mediumblue
CSS3ColorMap["mediumorchid"] = Mediumorchid
CSS3ColorMap["mediumpurple"] = Mediumpurple
CSS3ColorMap["mediumseagreen"] = Mediumseagreen
CSS3ColorMap["mediumslateblue"] = Mediumslateblue
CSS3ColorMap["mediumspringgreen"] = Mediumspringgreen
CSS3ColorMap["mediumturquoise"] = Mediumturquoise
CSS3ColorMap["mediumvioletred"] = Mediumvioletred
CSS3ColorMap["midnightblue"] = Midnightblue
CSS3ColorMap["mintcream"] = Mintcream
CSS3ColorMap["mistyrose"] = Mistyrose
CSS3ColorMap["moccasin"] = Moccasin
CSS3ColorMap["navajowhite"] = Navajowhite
CSS3ColorMap["navy"] = Navy
CSS3ColorMap["oldlace"] = Oldlace
CSS3ColorMap["olive"] = Olive
CSS3ColorMap["olivedrab"] = Olivedrab
CSS3ColorMap["orange"] = Orange
CSS3ColorMap["orangered"] = Orangered
CSS3ColorMap["orchid"] = Orchid
CSS3ColorMap["palegoldenrod"] = Palegoldenrod
CSS3ColorMap["palegreen"] = Palegreen
CSS3ColorMap["paleturquoise"] = Paleturquoise
CSS3ColorMap["palevioletred"] = Palevioletred
CSS3ColorMap["papayawhip"] = Papayawhip
CSS3ColorMap["peachpuff"] = Peachpuff
CSS3ColorMap["peru"] = Peru
CSS3ColorMap["pink"] = Pink
CSS3ColorMap["plum"] = Plum
CSS3ColorMap["powderblue"] = Powderblue
CSS3ColorMap["purple"] = Purple
CSS3ColorMap["red"] = Red
CSS3ColorMap["rosybrown"] = Rosybrown
CSS3ColorMap["royalblue"] = Royalblue
CSS3ColorMap["saddlebrown"] = Saddlebrown
CSS3ColorMap["salmon"] = Salmon
CSS3ColorMap["sandybrown"] = Sandybrown
CSS3ColorMap["seagreen"] = Seagreen
CSS3ColorMap["seashell"] = Seashell
CSS3ColorMap["sienna"] = Sienna
CSS3ColorMap["silver"] = Silver
CSS3ColorMap["skyblue"] = Skyblue
CSS3ColorMap["slateblue"] = Slateblue
CSS3ColorMap["slategray"] = Slategray
CSS3ColorMap["slategrey"] = Slategrey
CSS3ColorMap["snow"] = Snow
CSS3ColorMap["springgreen"] = Springgreen
CSS3ColorMap["steelblue"] = Steelblue
CSS3ColorMap["tan"] = Tan
CSS3ColorMap["teal"] = Teal
CSS3ColorMap["thistle"] = Thistle
CSS3ColorMap["tomato"] = Tomato
CSS3ColorMap["turquoise"] = Turquoise
CSS3ColorMap["violet"] = Violet
CSS3ColorMap["wheat"] = Wheat
CSS3ColorMap["white"] = White
CSS3ColorMap["whitesmoke"] = Whitesmoke
CSS3ColorMap["yellow"] = Yellow
CSS3ColorMap["yellowgreen"] = Yellowgreen
}