csscolor

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2025 License: MIT Imports: 6 Imported by: 1

README

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.

Documentation

Overview

Package csscolor parses CSS color values into Go color.Color types.

Supported formats:

  • Hex: #rgb, #rgba, #rrggbb, #rrggbbaa
  • Named colors: red, blue, transparent, etc.
  • RGB: rgb(255, 128, 0), rgb(100%, 50%, 0%), rgb(255 128 0)
  • RGBA: rgba(255, 128, 0, 0.5), rgba(255 128 0 / 0.5)
  • HSL: hsl(180, 50%, 50%), hsl(180 50% 50%)
  • HSLA: hsla(180, 50%, 50%, 0.5), hsla(180 50% 50% / 0.5)

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidColor = errors.New("invalid color")

ErrInvalidColor is returned when a color string cannot be parsed.

Functions

func Parse

func Parse(col string) (color.Color, error)

Parse parses a CSS color string and returns a color.Color. It supports hex notation (#rgb, #rgba, #rrggbb, #rrggbbaa), named colors, rgb(), rgba(), hsl(), and hsla() functions.

Types

This section is empty.

Jump to

Keyboard shortcuts

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