Help us shape the future of Go! Take the 2026 Go Developer Survey

csscolorparser

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 17, 2026 License: MIT Imports: 6 Imported by: 44

README

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

Documentation

Overview

Package csscolorparser provides function for parsing CSS color string as defined in the W3C's CSS color module level 4.

Example
package main

import (
	"fmt"

	"github.com/mazznoer/csscolorparser"
)

func main() {
	c, err := csscolorparser.Parse("rgb(100% 0% 0% / 50%)")

	if err != nil {
		panic(err)
	}

	fmt.Printf("R:%.3f, G:%.3f, B:%.3f, A:%.3f\n", c.R, c.G, c.B, c.A)
	fmt.Println(c.RGBA255())
	fmt.Println(c.HexString())
	fmt.Println(c.RGBString())
}
Output:
R:1.000, G:0.000, B:0.000, A:0.500
255 0 0 128
#ff000080
rgb(255 0 0 / 50%)

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidHex      = errors.New("invalid hex")
	ErrInvalidRgb      = errors.New("invalid rgb")
	ErrInvalidHsl      = errors.New("invalid hsl")
	ErrInvalidHwb      = errors.New("invalid hwb")
	ErrInvalidHsv      = errors.New("invalid hsv")
	ErrInvalidLab      = errors.New("invalid lab")
	ErrInvalidLch      = errors.New("invalid lch")
	ErrInvalidOklab    = errors.New("invalid oklab")
	ErrInvalidOklch    = errors.New("invalid oklch")
	ErrInvalidColor    = errors.New("invalid color")
	ErrInvalidFunction = errors.New("invalid function")
	ErrInvalidUnknown  = errors.New("invalid unknown")
)
View Source
var NamedColors = map[string][3]uint8{}/* 148 elements not displayed */

Functions

func ParseColors added in v0.2.0

func ParseColors(s string) iter.Seq2[Color, error]

ParseColors returns an iterator that lazily parses a comma-separated list of CSS colors.

Commas inside parentheses are not treated as separators, so functional notations such as "rgb(255, 0, 0)" or "hsl(120, 100%, 50%)" stay intact. Empty entries (e.g. from a trailing comma) are skipped.

Each iteration yields the parsed color and an error. If a color fails to parse, the error includes the offending token and iteration continues, letting the caller decide whether to stop (break) or skip.

Example:

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

Types

type Color

type Color struct {
	R, G, B, A float64
}

R, G, B, A values in the range 0..1

func FromHsl added in v0.1.4

func FromHsl(h, s, l, a float64) Color

FromHsl creates a Color from HSL colors.

Arguments:

  • h: Hue angle [0..360]
  • s: Saturation [0..1]
  • l: Lightness [0..1]
  • a: Alpha [0..1]

func FromHsv added in v0.1.4

func FromHsv(h, s, v, a float64) Color

FromHsv creates a Color from HSV colors.

Arguments:

  • h: Hue angle [0..360]
  • s: Saturation [0..1]
  • v: Value [0..1]
  • a: Alpha [0..1]

func FromHwb added in v0.1.4

func FromHwb(h, w, b, a float64) Color

FromHwb creates a Color from HWB colors.

Arguments:

  • h: Hue angle [0..360]
  • w: Whiteness [0..1]
  • b: Blackness [0..1]
  • a: Alpha [0..1]

func FromLab added in v0.1.7

func FromLab(l, a, b, alpha float64) Color

func FromLch added in v0.1.7

func FromLch(l, c, h, alpha float64) Color

func FromLinearRGB added in v0.1.4

func FromLinearRGB(r, g, b, a float64) Color

FromLinearRGB creates a Color from linear-light RGB colors.

Arguments:

  • r: Red value [0..1]
  • g: Green value [0..1]
  • b: Blue value [0..1]
  • a: Alpha value [0..1]

func FromOklab added in v0.1.4

func FromOklab(l, a, b, alpha float64) Color

FromOklab creates a Color from Oklab colors.

Arguments:

  • l: Perceived lightness
  • a: How green/red the color is
  • b: How blue/yellow the color is
  • alpha: Alpha [0..1]

func FromOklch added in v0.1.4

func FromOklch(l, c, h, alpha float64) Color

FromOklch creates a Color from OKLCh colors.

Arguments:

  • l: Perceived lightness
  • c: Chroma
  • h: Hue angle in radians
  • alpha: Alpha [0..1]

func FromXyzD50 added in v0.2.0

func FromXyzD50(x, y, z, alpha float64) Color

func FromXyzD65 added in v0.2.0

func FromXyzD65(x, y, z, alpha float64) Color

func Parse

func Parse(s string) (Color, error)

Parse parses CSS color string and returns, if successful, a Color.

func (Color) Clamp added in v0.1.5

func (c Color) Clamp() Color

Clamp restricts R, G, B, A values to the range 0..1.

func (Color) HexString

func (c Color) HexString() string

HexString returns CSS hexadecimal string.

func (Color) Hsl added in v0.2.0

func (c Color) Hsl() [4]float64

func (Color) Hwb added in v0.2.0

func (c Color) Hwb() [4]float64

func (Color) Lab added in v0.2.0

func (c Color) Lab() [4]float64

func (Color) Lch added in v0.2.0

func (col Color) Lch() [4]float64

func (Color) LinearRgb added in v0.2.0

func (c Color) LinearRgb() [4]float64

func (Color) MarshalText added in v0.1.3

func (c Color) MarshalText() ([]byte, error)

Implement the Go TextMarshaler interface

func (Color) Name added in v0.1.3

func (c Color) Name() (string, bool)

Name returns name of this color if its available.

Example
package main

import (
	"fmt"

	"github.com/mazznoer/csscolorparser"
)

func main() {
	c, err := csscolorparser.Parse("#ffd700")

	if err != nil {
		panic(err)
	}

	fmt.Println(c.RGBString())
	name, _ := c.Name()
	fmt.Println(name)
}
Output:
rgb(255 215 0)
gold

func (Color) Oklab added in v0.2.0

func (col Color) Oklab() [4]float64

func (Color) Oklch added in v0.2.0

func (col Color) Oklch() [4]float64

func (Color) RGBA

func (c Color) RGBA() (r, g, b, a uint32)

Implement the Go color.Color interface.

func (Color) RGBA255

func (c Color) RGBA255() (r, g, b, a uint8)

RGBA255 returns R, G, B, A values in the range 0..255

func (Color) RGBString

func (c Color) RGBString() string

RGBString returns CSS RGB string.

func (*Color) UnmarshalText added in v0.1.3

func (c *Color) UnmarshalText(text []byte) error

Implement the Go TextUnmarshaler interface

func (Color) XyzD50 added in v0.2.0

func (c Color) XyzD50() [4]float64

func (Color) XyzD65 added in v0.2.0

func (c Color) XyzD65() [4]float64

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL