clr

package module
v0.0.0-...-cbe678c Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2025 License: AGPL-3.0 Imports: 6 Imported by: 1

README

CLR logo

Go Reference Go Report Card

Efficient 16-bit and 32-bit color representation types and conversion between hexadecimal, RGB, RGBA and CSS named-colors.

16-bit colors

C16 has four 4-bit channels (red, green, blue and alpha transparency) to represent 3 and 4-digit hexadecimal colors.

  • #f60
  • #f60f

All CSS named-colors, RGB and RGBA colors can be converted to C16 with some degree of accuracy loss for non-web-safe colors.

32-bit colors

C32 has four 8-bit channels (red, green, blue and alpha transparency) to represent CSS named-colors, RGB, RGBA or 6 and 8-digit hexadecimal colors.

  • darkviolet
  • rgba(255,128,0)
  • rgba(255,128,0,255)
  • #ff6600
  • #ff6600ff

All 16-bit colors can be converted to C32 without any color accuracy loss.

Example

package main

import (
	"fmt"
	"github.com/speedyhoon/clr"
)

func main() {
	a := clr.BlanchedAlmond
	b, err := clr.FromHex6("#d2b48c")
	fmt.Println(a, b, b.RGBA(), err)
}

Outputs:

#ffebcd tan rgb(210,180,140,255) #cfb <nil>

Documentation

Overview

Package clr provides efficient 16-bit and 32-bit color representation types and conversion between hexadecimal, RGB, RGBA and CSS named-colors. Along with W3C named-color hex value constants.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalid     = errors.New("invalid value: expected a color name or 3, 4, 6, or 8 hexadecimal digits")
	ErrExceedRange = errors.New("expected an integer between 0 and 255")

	Hex3 = regexp.MustCompile(`^#?[[:xdigit:]]{3}$`)
	Hex4 = regexp.MustCompile(`^#?[[:xdigit:]]{4}$`)
	Hex6 = regexp.MustCompile(`^#?[[:xdigit:]]{6}$`)
	Hex8 = regexp.MustCompile(`^#?[[:xdigit:]]{8}$`)
	RGB  = regexp.MustCompile(`^rgb\(\d{1,3},\d{1,3},\d{1,3}\)$`)
	RGBA = regexp.MustCompile(`^rgba\(\d{1,3},\d{1,3},\d{1,3},\d{1,3}\)$`)
)

Functions

func IsValidHex3

func IsValidHex3(c string) bool

func IsValidHex4

func IsValidHex4(c string) bool

func IsValidHex6

func IsValidHex6(c string) bool

func IsValidHex8

func IsValidHex8(c string) bool

func IsValidRGB

func IsValidRGB(c string) bool

func IsValidRGBA

func IsValidRGBA(c string) bool

func TrimRGBPrefix

func TrimRGBPrefix(color string) string

Types

type C16

type C16 uint16

C16 represents a 16-bit color with 4 channels (red, green, blue and alpha) using 4-bits each (0-15 or 0-F).

func FromHex3

func FromHex3(c string) (C16, error)

FromHex3 converts a 3-digit hexadecimal string into a 16-bit color.

FromHex3("#F60")

func FromHex4

func FromHex4(color string) (C16, error)

FromHex4 converts a 4-digit hexadecimal string into a 16-bit color.

FromHex4("#F60F")

func New16

func New16(R, G, B, A uint8) C16

New16 returns a new 16-bit color where R, G, B, and A are converted from 0-255 range to 0-F hexadecimal. Use New16Hex for 4-bit hexadecimal integers ranging from 0 to F.

New16Hex(255, 102, 0, 255)

func New16Hex

func New16Hex(R, G, B, A uint8) C16

New16Hex returns a new 16-bit color where R, G, B, and A are expected to be a hexadecimal ranging from 0 to F. Use New16 for 8-bit integers ranging from 0 to 255.

New16Hex(0xF, 0x6, 0x0, 0xF)

func New16Str

func New16Str(color string) (_ C16, err error)

New16Str parses string color and if valid returns a 16-bit color. Accepts any 3, 4, 6 or 8 digit hexadecimal, CSS named-color, RGB or RGBA color string.

FromHex4("rgba(255,102,0,255)")

func Round

func Round(channel C32) C16

Round converts a single C32 channel into a single C16 channel. The returned C16 channel is rounded to the closest hexadecimal integer for optimal color accuracy.

func (C16) Hex3

func (c C16) Hex3() string

Hex3 returns a 3-digit hexadecimal color string without the alpha channel like `#f00`.

func (C16) Hex4

func (c C16) Hex4() string

Hex4 returns a 4-digit hexadecimal color string with the alpha channel like `#f00f`.

func (C16) Hex6

func (c C16) Hex6() string

Hex6 returns a 6-digit hexadecimal color string with the alpha channel like `#ff0000`.

func (C16) Hex8

func (c C16) Hex8() string

Hex8 returns an 8-digit hexadecimal color string with the alpha channel like `#ff0000ff`.

func (C16) Lossy

func (c C16) Lossy() string

Lossy returns the shortest possible string representation of color ignoring the alpha channel. Returns:

"red" otherwise,
a 3-digit hexadecimal color string (like `#f60`).

func (C16) RGB

func (c C16) RGB() string

RGB returns an RGB color string without the alpha channel like `rgb(255,0,0)`.

func (C16) RGBA

func (c C16) RGBA() string

RGBA returns an RGBA color string with the alpha channel like `rgba(255,0,0,255)`.

func (C16) String

func (c C16) String() string

String returns the shortest possible string representation of color without loosing any color accuracy. Returns:

"red" or
a 4-digit hexadecimal color string when the alpha channel is <= 254 (like `#f608`) otherwise,
a 3-digit hexadecimal color string when the alpha channel equals 255 (like `#f60`).

func (C16) To32

func (c C16) To32() C32

To32 converts a 16-bit color to a 32-bit color.

type C32

type C32 uint32

C32 represents a 32-bit color with four channels (red, green, blue and alpha) using 8-bits each (0-255 or 0-FF).

const (
	AliceBlue            C32 = 0xf0f8ffff
	AntiqueWhite         C32 = 0xfaebd7ff
	Aqua                 C32 = 0x00ffffff
	Aquamarine           C32 = 0x7fffd4ff
	Azure                C32 = 0xf0ffffff
	Beige                C32 = 0xf5f5dcff
	Bisque               C32 = 0xffe4c4ff
	Black                C32 = 0x000000ff
	BlanchedAlmond       C32 = 0xffebcdff
	Blue                 C32 = 0x0000ffff
	BlueViolet           C32 = 0x8a2be2ff
	Brown                C32 = 0xa52a2aff
	BurlyWood            C32 = 0xdeb887ff
	CadetBlue            C32 = 0x5f9ea0ff
	Chartreuse           C32 = 0x7fff00ff
	Chocolate            C32 = 0xd2691eff
	Coral                C32 = 0xff7f50ff
	CornflowerBlue       C32 = 0x6495edff
	Cornsilk             C32 = 0xfff8dcff
	Crimson              C32 = 0xdc143cff
	DarkBlue             C32 = 0x00008bff
	DarkCyan             C32 = 0x008b8bff
	DarkGoldenrod        C32 = 0xb8860bff
	DarkGray             C32 = 0xa9a9a9ff
	DarkGreen            C32 = 0x006400ff
	DarkKhaki            C32 = 0xbdb76bff
	DarkMagenta          C32 = 0x8b008bff
	DarkOliveGreen       C32 = 0x556b2fff
	DarkOrange           C32 = 0xff8c00ff
	DarkOrchid           C32 = 0x9932ccff
	DarkRed              C32 = 0x8b0000ff
	DarkSalmon           C32 = 0xe9967aff
	DarkSeaGreen         C32 = 0x8fbc8fff
	DarkSlateBlue        C32 = 0x483d8bff
	DarkSlateGray        C32 = 0x2f4f4fff
	DarkTurquoise        C32 = 0x00ced1ff
	DarkViolet           C32 = 0x9400d3ff
	DeepPink             C32 = 0xff1493ff
	DeepSkyBlue          C32 = 0x00bfffff
	DimGray              C32 = 0x696969ff
	DodgerBlue           C32 = 0x1e90ffff
	Firebrick            C32 = 0xb22222ff
	FloralWhite          C32 = 0xfffaf0ff
	ForestGreen          C32 = 0x228b22ff
	Fuchsia              C32 = 0xff00ffff
	Gainsboro            C32 = 0xdcdcdcff
	GhostWhite           C32 = 0xf8f8ffff
	Gold                 C32 = 0xffd700ff
	Goldenrod            C32 = 0xdaa520ff
	Gray                 C32 = 0x808080ff
	Green                C32 = 0x008000ff
	GreenYellow          C32 = 0xadff2fff
	Honeydew             C32 = 0xf0fff0ff
	HotPink              C32 = 0xff69b4ff
	IndianRed            C32 = 0xcd5c5cff
	Indigo               C32 = 0x4b0082ff
	Ivory                C32 = 0xfffff0ff
	Khaki                C32 = 0xf0e68cff
	Lavender             C32 = 0xe6e6faff
	LavenderBlush        C32 = 0xfff0f5ff
	LawnGreen            C32 = 0x7cfc00ff
	LemonChiffon         C32 = 0xfffacdff
	LightBlue            C32 = 0xadd8e6ff
	LightCoral           C32 = 0xf08080ff
	LightCyan            C32 = 0xe0ffffff
	LightGoldenrodYellow C32 = 0xfafad2ff
	LightGray            C32 = 0xd3d3d3ff
	LightGreen           C32 = 0x90ee90ff
	LightPink            C32 = 0xffb6c1ff
	LightSalmon          C32 = 0xffa07aff
	LightSeaGreen        C32 = 0x20b2aaff
	LightSkyBlue         C32 = 0x87cefaff
	LightSlateGray       C32 = 0x778899ff
	LightSteelBlue       C32 = 0xb0c4deff
	LightYellow          C32 = 0xffffe0ff
	Lime                 C32 = 0x00ff00ff
	LimeGreen            C32 = 0x32cd32ff
	Linen                C32 = 0xfaf0e6ff
	Maroon               C32 = 0x800000ff
	MediumAquamarine     C32 = 0x66cdaaff
	MediumBlue           C32 = 0x0000cdff
	MediumOrchid         C32 = 0xba55d3ff
	MediumPurple         C32 = 0x9370dbff
	MediumSeaGreen       C32 = 0x3cb371ff
	MediumSlateBlue      C32 = 0x7b68eeff
	MediumSpringGreen    C32 = 0x00fa9aff
	MediumTurquoise      C32 = 0x48d1ccff
	MediumVioletRed      C32 = 0xc71585ff
	MidnightBlue         C32 = 0x191970ff
	MintCream            C32 = 0xf5fffaff
	MistyRose            C32 = 0xffe4e1ff
	Moccasin             C32 = 0xffe4b5ff
	NavajoWhite          C32 = 0xffdeadff
	Navy                 C32 = 0x000080ff
	OldLace              C32 = 0xfdf5e6ff
	Olive                C32 = 0x808000ff
	OliveDrab            C32 = 0x6b8e23ff
	Orange               C32 = 0xffa500ff
	OrangeRed            C32 = 0xff4500ff
	Orchid               C32 = 0xda70d6ff
	PaleGoldenrod        C32 = 0xeee8aaff
	PaleGreen            C32 = 0x98fb98ff
	PaleTurquoise        C32 = 0xafeeeeff
	PaleVioletRed        C32 = 0xdb7093ff
	PapayaWhip           C32 = 0xffefd5ff
	PeachPuff            C32 = 0xffdab9ff
	Peru                 C32 = 0xcd853fff
	Pink                 C32 = 0xffc0cbff
	Plum                 C32 = 0xdda0ddff
	PowderBlue           C32 = 0xb0e0e6ff
	Purple               C32 = 0x800080ff
	RebeccaPurple        C32 = 0x663399ff
	Red                  C32 = 0xff0000ff
	RosyBrown            C32 = 0xbc8f8fff
	RoyalBlue            C32 = 0x4169e1ff
	SaddleBrown          C32 = 0x8b4513ff
	Salmon               C32 = 0xfa8072ff
	SandyBrown           C32 = 0xf4a460ff
	SeaGreen             C32 = 0x2e8b57ff
	Seashell             C32 = 0xfff5eeff
	Sienna               C32 = 0xa0522dff
	Silver               C32 = 0xc0c0c0ff
	SkyBlue              C32 = 0x87ceebff
	SlateBlue            C32 = 0x6a5acdff
	SlateGray            C32 = 0x708090ff
	Snow                 C32 = 0xfffafaff
	SpringGreen          C32 = 0x00ff7fff
	SteelBlue            C32 = 0x4682b4ff
	Tan                  C32 = 0xd2b48cff
	Teal                 C32 = 0x008080ff
	Thistle              C32 = 0xd8bfd8ff
	Tomato               C32 = 0xff6347ff
	Turquoise            C32 = 0x40e0d0ff
	Violet               C32 = 0xee82eeff
	Wheat                C32 = 0xf5deb3ff
	White                C32 = 0xffffffff
	WhiteSmoke           C32 = 0xf5f5f5ff
	Yellow               C32 = 0xffff00ff
	YellowGreen          C32 = 0x9acd32ff

	Cyan    = Aqua
	Magenta = Fuchsia
)

func FromHex6

func FromHex6(c string) (C32, error)

FromHex6 converts a 6-digit hexadecimal string into a 32-bit color.

FromHex6("#FF6600")

func FromHex8

func FromHex8(color string) (C32, error)

FromHex8 converts an 8-digit hexadecimal string into a 32-bit color.

FromHex8("#FF6600FF")

func FromName

func FromName(color string) C32

FromName converts a color-name string to its 32-bit color representation. Any color with an undefined name returns zero (0).

func FromRGB

func FromRGB(color string) (h C32, err error)

FromRGB converts 3 (RGB) or 4 (RGBA) comma separated channels ranging from 0 to 255 in base 10 into C32. Expected color string formats are:

rgb(0,0,0)
rgba(0, 0, 0, 0)
0,0,0
0,0,0,0

func FromRGBA

func FromRGBA(color string) (h C32, err error)

func New32

func New32(R, G, B, A uint8) C32

func New32Str

func New32Str(color string) (_ C32, err error)

New32Str parses string color and if valid returns a 32-bit color.

func (C32) Hex3

func (c C32) Hex3() string

Hex3 returns a lossy 3-digit hexadecimal color string without the alpha channel like `#f00`.

func (C32) Hex4

func (c C32) Hex4() string

Hex4 returns a lossy 4-digit hexadecimal color string with the alpha channel like `#f00f`.

func (C32) Hex6

func (c C32) Hex6() string

Hex6 returns a 6-digit hexadecimal color string with the alpha channel like `#ff0000`.

func (C32) Hex8

func (c C32) Hex8() string

Hex8 returns an 8-digit hexadecimal color string with the alpha channel like `#ff0000ff`.

func (C32) Is24bit

func (c C32) Is24bit() bool

Is24bit checks if a color's red, green and blue channels can be represented as a 16-bit color without any color accuracy loss.

func (C32) RGB

func (c C32) RGB() string

func (C32) RGBA

func (c C32) RGBA() string

func (C32) String

func (c C32) String() string

String returns the shortest possible string representation of color without loosing any color accuracy.

func (C32) To16

func (c C32) To16() C16

To16 is a lossy conversion to a 16-bit color.

func (C32) ToName

func (c C32) ToName() string

ToName converts a 32-bit color to its string name if it exactly matches. Any color with an undefined name returns an empty string.

Jump to

Keyboard shortcuts

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