Skip to content

KarpelesLab/csscolor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

csscolor

Go Test Go Reference

A Go library for parsing CSS color values into color.Color.

Installation

go get github.com/KarpelesLab/csscolor

Usage

package 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)
}

Supported Formats

Hex Colors

  • #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)

Named Colors

  • All 147 CSS named colors (e.g., red, blue, rebeccapurple)
  • transparent

RGB/RGBA Functions

  • 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/HSLA Functions

  • 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

License

MIT License - see LICENSE file.

About

Simple parser for CSS colors

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •