A Go library for parsing CSS color values into color.Color.
go get github.com/KarpelesLab/csscolorpackage main
import (
"fmt"
"image/color"
"github.com/KarpelesLab/csscolor"
)
func main() {
// Parse various CSS color formats
red, _ := csscolor.Parse("#ff0000")
blue, _ := csscolor.Parse("blue")
semi, _ := csscolor.Parse("rgba(255, 128, 0, 0.5)")
hsl, _ := csscolor.Parse("hsl(180, 50%, 50%)")
fmt.Printf("Red: %v\n", red)
fmt.Printf("Blue: %v\n", blue)
fmt.Printf("Semi-transparent orange: %v\n", semi)
fmt.Printf("HSL teal: %v\n", hsl)
}-
#rgb- Short hex (e.g.,#f00) -
#rgba- Short hex with alpha (e.g.,#f008) -
#rrggbb- Full hex (e.g.,#ff0000) -
#rrggbbaa- Full hex with alpha (e.g.,#ff000080)
- All 147 CSS named colors (e.g.,
red,blue,rebeccapurple) -
transparent
-
rgb(255, 128, 0)- Legacy comma syntax -
rgb(255 128 0)- CSS4 space syntax -
rgb(100%, 50%, 0%)- Percentage values -
rgba(255, 128, 0, 0.5)- With alpha (0-1) -
rgb(255 128 0 / 0.5)- CSS4 slash syntax for alpha -
rgb(255 128 0 / 50%)- Percentage alpha
-
hsl(180, 50%, 50%)- Legacy comma syntax -
hsl(180 50% 50%)- CSS4 space syntax -
hsla(180, 50%, 50%, 0.5)- With alpha -
hsl(180 50% 50% / 0.5)- CSS4 slash syntax for alpha
MIT License - see LICENSE file.