csscolorparser

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2022 License: MIT Imports: 4 Imported by: 22

README

Golang CSS Color Parser Library

PkgGoDev Build Status Build Status go report codecov Lines of Code

Go library for parsing CSS color string as defined in the W3C's CSS Color Module Level 4.

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()
  • hwba(), hsv(), hsva() - not in CSS standard.

Not yet supported: lab(), lch().

Example Color Format
transparent
lime
#0f0
#0f0f
#00ff00
#00ff00ff
rgb(0,255,0)
rgb(0% 100% 0%)
rgb(0 255 0 / 100%)
rgba(0,255,0,1)
hsl(120,100%,50%)
hsl(120deg 100% 50%)
hsl(-240 100% 50%)
hsl(-240deg 100% 50%)
hsl(0.3333turn 100% 50%)
hsl(133.333grad 100% 50%)
hsl(2.0944rad 100% 50%)
hsla(120,100%,50%,100%)
hwb(120 0% 0%)
hwb(480deg 0% 0% / 100%)
hsv(120,100%,100%)
hsv(120deg 100% 100% / 100%)

Usage Examples

import "github.com/mazznoer/csscolorparser"
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)

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 (NamedColor)
package main

import (
	"fmt"

	"github.com/mazznoer/csscolorparser"
)

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

	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.843, B:0.000, A:1.000
255 215 0 255
#ffd700
rgb(255,215,0)
Example (RgbColor)
package main

import (
	"fmt"

	"github.com/mazznoer/csscolorparser"
)

func main() {
	c, err := csscolorparser.Parse("rgba(100%, 0%, 0%, 0.5)")

	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
rgba(255,0,0,0.5)

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Color

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

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

func Parse

func Parse(s string) (Color, error)

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

func (Color) HexString

func (c Color) HexString() string

HexString returns CSS hexadecimal string.

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.

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

Jump to

Keyboard shortcuts

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