Skip to content

About

Go (Golang) CSS color parser library

Topics

Resources

Stars

31 stars

Watchers

3 watching

Forks

Repository files navigation

Go Language CSS Color Parser Library

PkgGoDev Build Status go report

Go library for parsing CSS color string as defined in the W3C's CSS Color Module Level 4. It supports parsing for absolute and relative color formats.

Supported Color Format

  • Named colors
  • RGB hexadecimal (with and without # prefix)
    • Short format #rgb
    • Short format with alpha #rgba
    • Long format #rrggbb
    • Long format with alpha #rrggbbaa
  • rgb() and rgba()
  • hsl() and hsla()
  • hwb()
  • lab()
  • lch()
  • oklab()
  • oklch()
  • color(srgb ...)
  • color(srgb-linear ...)
  • color(xyz ...)
  • color(xyz-d50 ...)
  • color(xyz-d65 ...)
  • hwba(), hsv(), hsva() - not in CSS standard.

Usage Examples

import "github.com/mazznoer/csscolorparser"

Parse Single Color

c, err := csscolorparser.Parse("gold")

if err != nil {
    panic(err)
}

fmt.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
fmt.Println(c.RGBA255())   // 255 215 0 255
fmt.Println(c.HexString()) // #ffd700
fmt.Println(c.RGBString()) // rgb(255 215 0)

Parse Multiple Colors

for c, err := range csscolorparser.ParseColors("red, #00ff00, rgb(0, 0, 255)") {
    if err != nil {
        fmt.Println(err)
        continue
    }
    fmt.Println(c.HexString())
}

Try It Online

Similar Projects

About

Go (Golang) CSS color parser library

Topics

Resources

Stars

31 stars

Watchers

3 watching

Forks

Releases

Sponsor this project

Contributors

Languages