...
1# Golang CSS Color Parser Library
2
3[](https://pkg.go.dev/github.com/mazznoer/csscolorparser)
4[](https://travis-ci.org/mazznoer/csscolorparser)
5[](https://github.com/mazznoer/csscolorparser/actions)
6[](https://goreportcard.com/report/github.com/mazznoer/csscolorparser)
7[](https://codecov.io/gh/mazznoer/csscolorparser)
8[](https://github.com/mazznoer/csscolorparser)
9
10[Go](https://www.golang.org/) library for parsing CSS color string as defined in the W3C's [CSS Color Module Level 4](https://www.w3.org/TR/css-color-4/).
11
12## Supported Color Format
13
14* [Named colors](https://www.w3.org/TR/css-color-4/#named-colors)
15* RGB hexadecimal (with and without `#` prefix)
16 + Short format `#rgb`
17 + Short format with alpha `#rgba`
18 + Long format `#rrggbb`
19 + Long format with alpha `#rrggbbaa`
20* `rgb()` and `rgba()`
21* `hsl()` and `hsla()`
22* `hwb()`
23* `hwba()`, `hsv()`, `hsva()` - not in CSS standard.
24
25Not yet supported: `lab()`, `lch()`.
26
27### Example Color Format
28
29```
30transparent
31lime
32#0f0
33#0f0f
34#00ff00
35#00ff00ff
36rgb(0,255,0)
37rgb(0% 100% 0%)
38rgb(0 255 0 / 100%)
39rgba(0,255,0,1)
40hsl(120,100%,50%)
41hsl(120deg 100% 50%)
42hsl(-240 100% 50%)
43hsl(-240deg 100% 50%)
44hsl(0.3333turn 100% 50%)
45hsl(133.333grad 100% 50%)
46hsl(2.0944rad 100% 50%)
47hsla(120,100%,50%,100%)
48hwb(120 0% 0%)
49hwb(480deg 0% 0% / 100%)
50hsv(120,100%,100%)
51hsv(120deg 100% 100% / 100%)
52```
53
54## Usage Examples
55
56```go
57import "github.com/mazznoer/csscolorparser"
58```
59
60```go
61c, err := csscolorparser.Parse("gold")
62
63if err != nil {
64 panic(err)
65}
66
67fmt.Printf("R:%.3f, G:%.3f, B:%.3f, A:%.3f", c.R, c.G, c.B, c.A) // R:1.000, G:0.843, B:0.000, A:1.000
68fmt.Println(c.RGBA255()) // 255 215 0 255
69fmt.Println(c.HexString()) // #ffd700
70fmt.Println(c.RGBString()) // rgb(255,215,0)
71```
72
73## Try It Online
74
75* [Playground 1](https://play.golang.org/p/8KMIc1TLQB0)
76* [Playground 2](https://play.golang.org/p/7kb62KSARwa)
77
78## Similar Projects
79
80* [csscolorparser](https://github.com/mazznoer/csscolorparser-rs) (Rust)
81* [csscolorparser](https://github.com/deanm/css-color-parser-js) (Javascript)
82
View as plain text