Skip to content

Commit

Permalink
Update doc
Browse files Browse the repository at this point in the history
  • Loading branch information
mazznoer committed Jun 13, 2021
1 parent fadfbf9 commit d57eb58
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 21 deletions.
63 changes: 44 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
# CSS Color Parser
# Golang CSS Color Parser Library

[![PkgGoDev](https://pkg.go.dev/badge/github.com/mazznoer/csscolorparser)](https://pkg.go.dev/github.com/mazznoer/csscolorparser)
[![Build Status](https://travis-ci.org/mazznoer/csscolorparser.svg?branch=master)](https://travis-ci.org/mazznoer/csscolorparser)
[![Build Status](https://github.com/mazznoer/csscolorparser/workflows/Go/badge.svg)](https://github.com/mazznoer/csscolorparser/actions)
[![go report](https://goreportcard.com/badge/github.com/mazznoer/csscolorparser)](https://goreportcard.com/report/github.com/mazznoer/csscolorparser)
[![codecov](https://codecov.io/gh/mazznoer/csscolorparser/branch/master/graph/badge.svg)](https://codecov.io/gh/mazznoer/csscolorparser)
[![Lines of Code](https://tokei.rs/b1/github/mazznoer/csscolorparser?category=code)](https://github.com/mazznoer/csscolorparser)

Go (Golang) CSS color parser.
[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/).

It support W3C's CSS color module level 4.
## Supported Color Format

```go
import "github.com/mazznoer/csscolorparser"
```
* [Named colors](https://www.w3.org/TR/css-color-4/#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.

## Usage
Not yet supported: `lab()`, `lch()`.

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

if err != nil {
panic(err)
}
```

## Supported Format

It support named colors, hexadecimal (`#rgb`, `#rgba`, `#rrggbb`, `#rrggbbaa`), `rgb()`, `rgba()`, `hsl()`, `hsla()`, `hwb()`, and `hsv()`.
### Example Color Format

```
transparent
lime
#0f0
#0f0f
Expand All @@ -52,6 +51,32 @@ hsv(120,100%,100%)
hsv(120deg 100% 100% / 100%)
```

## Usage Examples

```go
import "github.com/mazznoer/csscolorparser"
```

```go
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

[Go Playground](https://play.golang.org/p/7kb62KSARwa)
* [Playground 1](https://play.golang.org/p/8KMIc1TLQB0)
* [Playground 2](https://play.golang.org/p/7kb62KSARwa)

## Similar Projects

* [csscolorparser](https://github.com/mazznoer/csscolorparser-rs) (Rust)
* [csscolorparser](https://github.com/deanm/css-color-parser-js) (Javascript)

10 changes: 8 additions & 2 deletions colorparser.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package csscolorparser provides function for parsing CSS color string as defined in the W3C's CSS color module level 4.
package csscolorparser

import (
Expand All @@ -9,10 +10,12 @@ import (

// Inspired by https://github.com/deanm/css-color-parser-js

// R, G, B, A values in the range 0..1
type Color struct {
R, G, B, A float64
}

// Implement the Go color.Color interface.
func (c Color) RGBA() (r, g, b, a uint32) {
r = uint32(math.Round(c.R * 65535))
g = uint32(math.Round(c.G * 65535))
Expand All @@ -21,6 +24,7 @@ func (c Color) RGBA() (r, g, b, a uint32) {
return
}

// RGBA255 returns R, G, B, A values in the range 0..255
func (c Color) RGBA255() (r, g, b, a uint8) {
r = uint8(math.Round(c.R * 255))
g = uint8(math.Round(c.G * 255))
Expand All @@ -29,6 +33,7 @@ func (c Color) RGBA255() (r, g, b, a uint8) {
return
}

// HexString returns CSS hexadecimal string.
func (c Color) HexString() string {
r, g, b, a := c.RGBA255()
if a < 255 {
Expand All @@ -37,12 +42,13 @@ func (c Color) HexString() string {
return fmt.Sprintf("#%02x%02x%02x", r, g, b)
}

// RGBString returns CSS RGB string.
func (c Color) RGBString() string {
r, g, b, _ := c.RGBA255()
if c.A < 1 {
return fmt.Sprintf("rgba(%v,%v,%v,%v)", r, g, b, c.A)
return fmt.Sprintf("rgba(%d,%d,%d,%v)", r, g, b, c.A)
}
return fmt.Sprintf("rgb(%v,%v,%v)", r, g, b)
return fmt.Sprintf("rgb(%d,%d,%d)", r, g, b)
}

var black = Color{0, 0, 0, 1}
Expand Down
43 changes: 43 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package csscolorparser_test

import (
"fmt"

"github.com/mazznoer/csscolorparser"
)

func Example_namedColor() {
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)
}

func Example_rgbColor() {
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)
}

0 comments on commit d57eb58

Please sign in to comment.