colors

package module
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2026 License: MIT Imports: 7 Imported by: 7

README

colors

colors is a Go package to parse color strings. Provides a type to use easily with cobra / pflag or other command line packages.

Using | Example | About

Unit Tests Go Reference Discord Discussion

Using

Install in the usual Go fashion:

$ go get -u github.com/kenshaw/colors@latest

Example

package colors_test

import (
	"fmt"
	"log"

	"github.com/kenshaw/colors"
)

func Example() {
	for _, s := range []string{
		"red",
		"rgba(192, 192, 192, 255)",
		"navajo white",
		"#ffeeaa",
		"rgb(106,90,205)",
		"rgb(0,255,0)",
		"hex(f,e,a)",
		"#fff",
		"rgba(26,33,80,22)",
		"#cdcdcd80",
		"#cdcdcdff",
		"hex(0,0,0,0)",
	} {
		c, err := colors.Parse(s)
		if err != nil {
			log.Fatal(err)
		}
		fmt.Println(c)
	}
	// Output:
	// red
	// silver
	// navajowhite
	// #fea
	// slateblue
	// lime
	// #0f0e0a
	// white
	// #1a215016
	// #cdcdcd80
	// #cdcdcd
	// transparent
}

About

Built to support the fv, iv, and usql applications.

Documentation

Overview

Package colors supports parsing various colors, from named colors to standard web / CSS style definitions.

See Parse for more information on supported representation formats.

Example
package main

import (
	"fmt"
	"log"

	"github.com/kenshaw/colors"
)

func main() {
	for _, s := range []string{
		"red",
		"rgba(192, 192, 192, 255)",
		"navajo white",
		"#ffeeaa",
		"rgb(106,90,205)",
		"rgb(0,255,0)",
		"hex(f,e,a)",
		"#fff",
		"rgba(26,33,80,22)",
		"#cdcdcd80",
		"#cdcdcdff",
		"hex(0,0,0,0)",
	} {
		c, err := colors.Parse(s)
		if err != nil {
			log.Fatal(err)
		}
		fmt.Println(c)
	}
}
Output:
red
silver
navajowhite
#fea
slateblue
lime
#0f0e0a
white
#1a215016
#cdcdcd80
#cdcdcd
transparent

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Is added in v0.1.3

func Is(a, b color.Color) bool

Is returns true when a, b are equivalent.

func Map

func Map() map[NamedColor]Color

Map returns a map of all named colors.

func MapString

func MapString() map[string]Color

MapString returns a map of all named colors.

func Register

func Register(n NamedColor, clr color.Color)

Register registers a named color.

func RegisterName

func RegisterName(s string, clr color.Color)

RegisterName registers a named color.

Types

type Color

type Color struct {
	R, G, B, A uint8
	NamedColor NamedColor
}

Color is a color. Effectively the same as color.NRGBA, but with a NamedColor.

func FromColor

func FromColor(clr color.Color) Color

FromColor converts a standard color.Color to a color.

func FromHex

func FromHex(s string) (Color, bool)

FromHex converts a hex string to a color.

func FromName

func FromName(s string) (Color, bool)

FromName converts a name to a color.

func FromRGB

func FromRGB(s string) (Color, bool)

FromRGB converts a rgb string to a color.

func FromRGBA

func FromRGBA(s string) (Color, bool)

FromRGBA converts a rgba string to a color.

func FromWeb

func FromWeb(s string) (c Color, ok bool)

FromWeb converts a web string to a color.

Cribbed from https://stackoverflow.com/questions/54197913/parse-hex-string-to-image-color

func New

func New(r, g, b, a uint8) Color

New creates a new color, and looks up the named color.

func Parse

func Parse(s string) (Color, error)

Parse parses a color. Case-insensitive and ignores whitespace.

Parses formatting like the following:

Name - a CSS color name (ex: Misty_Rose, red, ...)
#abc - a 3

For example, a copy of the named color [MistyRose] will be returned, when passed a string like any the following:

"Misty_Rose"
"mistyrose"
"MISTY ROSE"
"rgb(255, 228, 225)"
"rgba(255, 228, 225, 255)"
"hex(ff, e4, e1)"
"hex(ff, e4, e1, ff)"
"#ffe4e1"
"#ffe4e1ff"

func ToColor

func ToColor(r, g, b, a uint8, name string) Color

ToColor creates a color.

func (Color) AsHex

func (c Color) AsHex() string

AsHex returns the color formatted as a web, ex: hex(aa,bb,cc,dd).

func (Color) AsRGB

func (c Color) AsRGB() string

AsRGB returns the color formatted as a rgb string, ex: rgb(255,255,255).

func (Color) AsRGBA

func (c Color) AsRGBA() string

AsRGBA returns the color formatted as a rgba string, ex: rgba(255,255,255,255).

func (Color) AsText

func (c Color) AsText() string

AsText returns a string representation of the color.

func (Color) AsWeb

func (c Color) AsWeb() string

AsWeb returns the color formatted as a web string, ex: #ffffffff.

func (Color) AsWebShort

func (c Color) AsWebShort() string

AsWebShort returns the shortest possible formatted web string, ex: #fff.

func (Color) CMYK added in v0.2.3

func (c Color) CMYK() color.CMYK

CMYK returns the color as a color.CMYK.

func (Color) Dark

func (c Color) Dark() bool

Dark returs whether or not the color is a "dark" color or not.

func (Color) Format

func (c Color) Format(f fmt.State, verb rune)

Format satisfies the fmt.Formatter interface.

func (Color) Is

func (c Color) Is(clr color.Color) bool

Is returns true when the colors are equivalent.

func (Color) Light

func (c Color) Light() bool

Light returns whether the color is a "light" color or not.

func (*Color) MarshalText

func (c *Color) MarshalText() ([]byte, error)

MarshalText satisfies the encoding.TextMarshaler interface.

func (Color) NRGBA added in v0.2.3

func (c Color) NRGBA() color.NRGBA

NRGBA returns the color as a color.NRGBA.

func (Color) NYCbCrA added in v0.2.3

func (c Color) NYCbCrA() color.NYCbCrA

NYcbCrA returns the color as a color.NYcbCrA.

func (Color) Name

func (c Color) Name() string

Name returns the color's name.

func (*Color) Pflag

func (c *Color) Pflag() Pflag

Pflag returns a Pflag wrapping the color, that can be used with various command-line packages, such as cobra, and satisfies the pflag.Value interface.

func (Color) RGBA

func (c Color) RGBA() (r, g, b, a uint32)

RGBA satisfies the color.Color interface.

func (Color) Type added in v0.2.1

func (Color) Type() string

Type returns the type.

func (*Color) UnmarshalText

func (c *Color) UnmarshalText(text []byte) error

UnmarshalText satisfies the encoding.TextUnmarshaler interface.

func (Color) YCbCr added in v0.2.3

func (c Color) YCbCr() color.YCbCr

YCbCr returns the color as a color.YCbCr.

type Error

type Error string

Error is a error.

const (
	// ErrInvalidColor is invalid color error.
	ErrInvalidColor Error = "invalid color"
)

Errors.

func (Error) Error

func (err Error) Error() string

Error satisfies the [error] interface.

type NamedColor

type NamedColor string

NamedColor is a named color.

const (
	Aliceblue            NamedColor = "aliceblue"
	Antiquewhite         NamedColor = "antiquewhite"
	Aqua                 NamedColor = "aqua"
	Aquamarine           NamedColor = "aquamarine"
	Azure                NamedColor = "azure"
	Beige                NamedColor = "beige"
	Bisque               NamedColor = "bisque"
	Black                NamedColor = "black"
	Blanchedalmond       NamedColor = "blanchedalmond"
	Blue                 NamedColor = "blue"
	Blueviolet           NamedColor = "blueviolet"
	Brown                NamedColor = "brown"
	Burlywood            NamedColor = "burlywood"
	Cadetblue            NamedColor = "cadetblue"
	Chartreuse           NamedColor = "chartreuse"
	Chocolate            NamedColor = "chocolate"
	Coral                NamedColor = "coral"
	Cornflowerblue       NamedColor = "cornflowerblue"
	Cornsilk             NamedColor = "cornsilk"
	Crimson              NamedColor = "crimson"
	Cyan                 NamedColor = "cyan"
	Darkblue             NamedColor = "darkblue"
	Darkcyan             NamedColor = "darkcyan"
	Darkgoldenrod        NamedColor = "darkgoldenrod"
	Darkgray             NamedColor = "darkgray"
	Darkgreen            NamedColor = "darkgreen"
	Darkgrey             NamedColor = "darkgrey"
	Darkkhaki            NamedColor = "darkkhaki"
	Darkmagenta          NamedColor = "darkmagenta"
	Darkolivegreen       NamedColor = "darkolivegreen"
	Darkorange           NamedColor = "darkorange"
	Darkorchid           NamedColor = "darkorchid"
	Darkred              NamedColor = "darkred"
	Darksalmon           NamedColor = "darksalmon"
	Darkseagreen         NamedColor = "darkseagreen"
	Darkslateblue        NamedColor = "darkslateblue"
	Darkslategray        NamedColor = "darkslategray"
	Darkslategrey        NamedColor = "darkslategrey"
	Darkturquoise        NamedColor = "darkturquoise"
	Darkviolet           NamedColor = "darkviolet"
	Deeppink             NamedColor = "deeppink"
	Deepskyblue          NamedColor = "deepskyblue"
	Dimgray              NamedColor = "dimgray"
	Dimgrey              NamedColor = "dimgrey"
	Dodgerblue           NamedColor = "dodgerblue"
	Firebrick            NamedColor = "firebrick"
	Floralwhite          NamedColor = "floralwhite"
	Forestgreen          NamedColor = "forestgreen"
	Fuchsia              NamedColor = "fuchsia"
	Gainsboro            NamedColor = "gainsboro"
	Ghostwhite           NamedColor = "ghostwhite"
	Gold                 NamedColor = "gold"
	Goldenrod            NamedColor = "goldenrod"
	Gray                 NamedColor = "gray"
	Green                NamedColor = "green"
	Greenyellow          NamedColor = "greenyellow"
	Grey                 NamedColor = "grey"
	Honeydew             NamedColor = "honeydew"
	Hotpink              NamedColor = "hotpink"
	Indianred            NamedColor = "indianred"
	Indigo               NamedColor = "indigo"
	Ivory                NamedColor = "ivory"
	Khaki                NamedColor = "khaki"
	Lavender             NamedColor = "lavender"
	Lavenderblush        NamedColor = "lavenderblush"
	Lawngreen            NamedColor = "lawngreen"
	Lemonchiffon         NamedColor = "lemonchiffon"
	Lightblue            NamedColor = "lightblue"
	Lightcoral           NamedColor = "lightcoral"
	Lightcyan            NamedColor = "lightcyan"
	Lightgoldenrodyellow NamedColor = "lightgoldenrodyellow"
	Lightgray            NamedColor = "lightgray"
	Lightgreen           NamedColor = "lightgreen"
	Lightgrey            NamedColor = "lightgrey"
	Lightpink            NamedColor = "lightpink"
	Lightsalmon          NamedColor = "lightsalmon"
	Lightseagreen        NamedColor = "lightseagreen"
	Lightskyblue         NamedColor = "lightskyblue"
	Lightslategray       NamedColor = "lightslategray"
	Lightslategrey       NamedColor = "lightslategrey"
	Lightsteelblue       NamedColor = "lightsteelblue"
	Lightyellow          NamedColor = "lightyellow"
	Lime                 NamedColor = "lime"
	Limegreen            NamedColor = "limegreen"
	Linen                NamedColor = "linen"
	Magenta              NamedColor = "magenta"
	Maroon               NamedColor = "maroon"
	Mediumaquamarine     NamedColor = "mediumaquamarine"
	Mediumblue           NamedColor = "mediumblue"
	Mediumorchid         NamedColor = "mediumorchid"
	Mediumpurple         NamedColor = "mediumpurple"
	Mediumseagreen       NamedColor = "mediumseagreen"
	Mediumslateblue      NamedColor = "mediumslateblue"
	Mediumspringgreen    NamedColor = "mediumspringgreen"
	Mediumturquoise      NamedColor = "mediumturquoise"
	Mediumvioletred      NamedColor = "mediumvioletred"
	Midnightblue         NamedColor = "midnightblue"
	Mintcream            NamedColor = "mintcream"
	Mistyrose            NamedColor = "mistyrose"
	Moccasin             NamedColor = "moccasin"
	Navajowhite          NamedColor = "navajowhite"
	Navy                 NamedColor = "navy"
	Oldlace              NamedColor = "oldlace"
	Olive                NamedColor = "olive"
	Olivedrab            NamedColor = "olivedrab"
	Orange               NamedColor = "orange"
	Orangered            NamedColor = "orangered"
	Orchid               NamedColor = "orchid"
	Palegoldenrod        NamedColor = "palegoldenrod"
	Palegreen            NamedColor = "palegreen"
	Paleturquoise        NamedColor = "paleturquoise"
	Palevioletred        NamedColor = "palevioletred"
	Papayawhip           NamedColor = "papayawhip"
	Peachpuff            NamedColor = "peachpuff"
	Peru                 NamedColor = "peru"
	Pink                 NamedColor = "pink"
	Plum                 NamedColor = "plum"
	Powderblue           NamedColor = "powderblue"
	Purple               NamedColor = "purple"
	Red                  NamedColor = "red"
	Rosybrown            NamedColor = "rosybrown"
	Royalblue            NamedColor = "royalblue"
	Saddlebrown          NamedColor = "saddlebrown"
	Salmon               NamedColor = "salmon"
	Sandybrown           NamedColor = "sandybrown"
	Seagreen             NamedColor = "seagreen"
	Seashell             NamedColor = "seashell"
	Sienna               NamedColor = "sienna"
	Silver               NamedColor = "silver"
	Skyblue              NamedColor = "skyblue"
	Slateblue            NamedColor = "slateblue"
	Slategray            NamedColor = "slategray"
	Slategrey            NamedColor = "slategrey"
	Snow                 NamedColor = "snow"
	Springgreen          NamedColor = "springgreen"
	Steelblue            NamedColor = "steelblue"
	Tan                  NamedColor = "tan"
	Teal                 NamedColor = "teal"
	Thistle              NamedColor = "thistle"
	Tomato               NamedColor = "tomato"
	Transparent          NamedColor = "transparent"
	Turquoise            NamedColor = "turquoise"
	Violet               NamedColor = "violet"
	Wheat                NamedColor = "wheat"
	White                NamedColor = "white"
	Whitesmoke           NamedColor = "whitesmoke"
	Yellow               NamedColor = "yellow"
	Yellowgreen          NamedColor = "yellowgreen"
)

Named colors.

func (NamedColor) CMYK added in v0.2.3

func (c NamedColor) CMYK() color.CMYK

CMYK returns the color as a color.CMYK.

func (NamedColor) Color

func (c NamedColor) Color() Color

Color returns a Color for the named color.

func (NamedColor) Format

func (c NamedColor) Format(f fmt.State, verb rune)

Format satisfies the fmt.Formatter interface.

func (NamedColor) NRGBA added in v0.2.2

func (c NamedColor) NRGBA() color.NRGBA

NRGBA returns the color.NRGBA for the named color.

func (NamedColor) NYCbCrA added in v0.2.3

func (c NamedColor) NYCbCrA() color.NYCbCrA

NYcbCrA returns the color as a color.NYcbCrA.

func (NamedColor) RGBA

func (c NamedColor) RGBA() (r, g, b, a uint32)

RGBA satisfies the color.Color interface.

func (NamedColor) YCbCr added in v0.2.3

func (c NamedColor) YCbCr() color.YCbCr

YCbCr returns the color as a color.YCbCr.

type Pflag

type Pflag struct {
	// contains filtered or unexported fields
}

Pflag wraps a color, for use with command-line packages, such as cobra. Satisfies the pflag.Value interface.

func (Pflag) Set

func (f Pflag) Set(s string) error

Set satisfies the pflag.Value interface.

func (Pflag) String

func (f Pflag) String() string

String satisfies the pflag.Value interface.

func (Pflag) Type

func (Pflag) Type() string

Type satisfies the pflag.Value interface.

Directories

Path Synopsis
Package strcase provides methods to convert CamelCase to and from snake_case.
Package strcase provides methods to convert CamelCase to and from snake_case.

Jump to

Keyboard shortcuts

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