color

package
v0.0.0-...-d3199ed Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2020 License: Apache-2.0 Imports: 9 Imported by: 0

README

A library for colors in Go

Rearranged from this package.

Also, contains tcell colors.

Documentation

Overview

The colorful package provides all kinds of functions for working with colors.

Index

Constants

View Source
const (
	Black = valid + iota
	Maroon
	Green
	Olive
	Navy
	Purple
	Teal
	Silver
	Gray
	Red
	Lime
	Yellow
	Blue
	Fuchsia
	Aqua
	White
	Noname16
	Noname17
	Noname18
	Noname19
	Noname20
	Noname21
	Noname22
	Noname23
	Noname24
	Noname25
	Noname26
	Noname27
	Noname28
	Noname29
	Noname30
	Noname31
	Noname32
	Noname33
	Noname34
	Noname35
	Noname36
	Noname37
	Noname38
	Noname39
	Noname40
	Noname41
	Noname42
	Noname43
	Noname44
	Noname45
	Noname46
	Noname47
	Noname48
	Noname49
	Noname50
	Noname51
	Noname52
	Noname53
	Noname54
	Noname55
	Noname56
	Noname57
	Noname58
	Noname59
	Noname60
	Noname61
	Noname62
	Noname63
	Noname64
	Noname65
	Noname66
	Noname67
	Noname68
	Noname69
	Noname70
	Noname71
	Noname72
	Noname73
	Noname74
	Noname75
	Noname76
	Noname77
	Noname78
	Noname79
	Noname80
	Noname81
	Noname82
	Noname83
	Noname84
	Noname85
	Noname86
	Noname87
	Noname88
	Noname89
	Noname90
	Noname91
	Noname92
	Noname93
	Noname94
	Noname95
	Noname96
	Noname97
	Noname98
	Noname99
	Noname100
	Noname101
	Noname102
	Noname103
	Noname104
	Noname105
	Noname106
	Noname107
	Noname108
	Noname109
	Noname110
	Noname111
	Noname112
	Noname113
	Noname114
	Noname115
	Noname116
	Noname117
	Noname118
	Noname119
	Noname120
	Noname121
	Noname122
	Noname123
	Noname124
	Noname125
	Noname126
	Noname127
	Noname128
	Noname129
	Noname130
	Noname131
	Noname132
	Noname133
	Noname134
	Noname135
	Noname136
	Noname137
	Noname138
	Noname139
	Noname140
	Noname141
	Noname142
	Noname143
	Noname144
	Noname145
	Noname146
	Noname147
	Noname148
	Noname149
	Noname150
	Noname151
	Noname152
	Noname153
	Noname154
	Noname155
	Noname156
	Noname157
	Noname158
	Noname159
	Noname160
	Noname161
	Noname162
	Noname163
	Noname164
	Noname165
	Noname166
	Noname167
	Noname168
	Noname169
	Noname170
	Noname171
	Noname172
	Noname173
	Noname174
	Noname175
	Noname176
	Noname177
	Noname178
	Noname179
	Noname180
	Noname181
	Noname182
	Noname183
	Noname184
	Noname185
	Noname186
	Noname187
	Noname188
	Noname189
	Noname190
	Noname191
	Noname192
	Noname193
	Noname194
	Noname195
	Noname196
	Noname197
	Noname198
	Noname199
	Noname200
	Noname201
	Noname202
	Noname203
	Noname204
	Noname205
	Noname206
	Noname207
	Noname208
	Noname209
	Noname210
	Noname211
	Noname212
	Noname213
	Noname214
	Noname215
	Noname216
	Noname217
	Noname218
	Noname219
	Noname220
	Noname221
	Noname222
	Noname223
	Noname224
	Noname225
	Noname226
	Noname227
	Noname228
	Noname229
	Noname230
	Noname231
	Noname232
	Noname233
	Noname234
	Noname235
	Noname236
	Noname237
	Noname238
	Noname239
	Noname240
	Noname241
	Noname242
	Noname243
	Noname244
	Noname245
	Noname246
	Noname247
	Noname248
	Noname249
	Noname250
	Noname251
	Noname252
	Noname253
	Noname254
	Noname255
	AliceBlue
	AntiqueWhite
	AquaMarine
	Azure
	Beige
	Bisque
	BlanchedAlmond
	BlueViolet
	Brown
	BurlyWood
	CadetBlue
	Chartreuse
	Chocolate
	Coral
	CornflowerBlue
	CornSilk
	Crimson
	DarkBlue
	DarkCyan
	DarkGoldenrod
	DarkGray
	DarkGreen
	DarkKhaki
	DarkMagenta
	DarkOliveGreen
	DarkOrange
	DarkOrchid
	DarkRed
	DarkSalmon
	DarkSeaGreen
	DarkSlateBlue
	DarkSlateGray
	DarkTurquoise
	DarkViolet
	DeepPink
	DeepSkyBlue
	DimGray
	DodgerBlue
	FireBrick
	FloralWhite
	ForestGreen
	GainsBoro
	GhostWhite
	Gold
	Goldenrod
	GreenYellow
	Honeydew
	HotPink
	IndianRed
	Indigo
	Ivory
	Khaki
	Lavender
	LavenderBlush
	LawnGreen
	LemonChiffon
	LightBlue
	LightCoral
	LightCyan
	LightGoldenrodYellow
	LightGray
	LightGreen
	LightPink
	LightSalmon
	LightSeaGreen
	LightSkyBlue
	LightSlateGray
	LightSteelBlue
	LightYellow
	LimeGreen
	Linen
	MediumAquamarine
	MediumBlue
	MediumOrchid
	MediumPurple
	MediumSeaGreen
	MediumSlateBlue
	MediumSpringGreen
	MediumTurquoise
	MediumVioletRed
	MidnightBlue
	MintCream
	MistyRose
	Moccasin
	NavajoWhite
	OldLace
	OliveDrab
	Orange
	OrangeRed
	Orchid
	PaleGoldenrod
	PaleGreen
	PaleTurquoise
	PaleVioletRed
	PapayaWhip
	PeachPuff
	Peru
	Pink
	Plum
	PowderBlue
	RebeccaPurple
	RosyBrown
	RoyalBlue
	SaddleBrown
	Salmon
	SandyBrown
	SeaGreen
	Seashell
	Sienna
	SkyBlue
	SlateBlue
	SlateGray
	Snow
	SpringGreen
	SteelBlue
	Tan
	Thistle
	Tomato
	Turquoise
	Violet
	Wheat
	WhiteSmoke
	YellowGreen
)

Note that the order of these options is important -- it follows the definitions used by ECMA and XTerm. Hence any further named colors must begin at a value not less than 256.

View Source
const Delta = 1.0 / 255.0

This is the tolerance used when comparing colors using AlmostEqualRGB.

View Source
const (
	// Reset is used to indicate that the color should use the vanilla terminal colors. (Basically go back to the defaults.)
	Reset = Special | iota
)

Special colors.

View Source
const (
	ValidConst = valid
)

Variables

This section is empty.

Functions

func ANSIWriter

func ANSIWriter(writer io.Writer) io.Writer

ANSIWriter returns an io.Writer which translates any ANSI escape codes written to it into color tags. Other escape codes don't have an effect and are simply removed. The translated text is written to the provided writer.

func AlmostEqualRGB

func AlmostEqualRGB(c1, c2 RGB) bool

Check for equality between colors within the tolerance Delta (1/255).

func DistanceCIE76

func DistanceCIE76(c1, c2 RGB) float64

That's actually the same, but I don't want to break code.

func DistanceCIE94

func DistanceCIE94(c1, c2 RGB) float64

Uses the CIE94 formula to calculate color distance. More accurate than DistanceLab, but also more work.

func DistanceCIEDE2000

func DistanceCIEDE2000(c1, c2 RGB) float64

DistanceCIEDE2000 uses the Delta E 2000 formula to calculate color distance. It is more expensive but more accurate than both DistanceLab and DistanceCIE94.

func DistanceCIEDE2000klch

func DistanceCIEDE2000klch(c1, c2 RGB, kl, kc, kh float64) float64

DistanceCIEDE2000klch uses the Delta E 2000 formula with custom values for the weighting factors kL, kC, and kH.

func DistanceLab

func DistanceLab(c1, c2 RGB) float64

DistanceLab is a good measure of visual similarity between two colors! A result of 0 would mean identical colors, while a result of 1 or higher means the colors differ a lot.

func DistanceLuv

func DistanceLuv(c1, c2 RGB) float64

DistanceLuv is a good measure of visual similarity between two colors! A result of 0 would mean identical colors, while a result of 1 or higher means the colors differ a lot.

func DistanceRGB

func DistanceRGB(c1, c2 RGB) float64

DistanceRGB computes the distance between two colors in RGB space. This is not a good measure! Rather do it in Lab space.

func ExpandToInt32

func ExpandToInt32(r *RGB) (int32, int32, int32)

func FromLab

func FromLab(t float64) float64

func HCLToLab

func HCLToLab(h, c, l float64) (L, a, b float64)

func HCLWhiteRef

func HCLWhiteRef(c RGB, wref [3]float64) (h, co, l float64)

Converts the given color to HCL space, taking into account a given reference white. (i.e. the monitor's white) H values are in [0..360], C and L values are in [0..1]

func Hex

func Hex(c Color) int32

NewRGBFromHex returns the color's hexadecimal RGB 24-bit value with each component consisting of a single byte, ala R << 16 | G << 8 | B. If the color is unknown or unset, -1 is returned.

func IsRGB

func IsRGB(c Color) bool

IsRGB is true if the color is an RGB specific value.

func LabToHCL

func LabToHCL(L, a, b float64) (h, c, l float64)

func LabToXYZ

func LabToXYZ(l, a, b float64) (x, y, z float64)

func LabToXYZWhiteRef

func LabToXYZWhiteRef(l, a, b float64, wref [3]float64) (x, y, z float64)

func LabWhiteRef

func LabWhiteRef(c RGB, wref [3]float64) (l, a, b float64)

Converts the given color to CIE L*a*b* space, taking into account a given reference white. (i.e. the monitor's white)

func Light

func Light(c Color) float64

Light returns the amount of light in a color.

func LinearRGBToXYZ

func LinearRGBToXYZ(r, g, b float64) (x, y, z float64)

func LuvToXYZ

func LuvToXYZ(l, u, v float64) (x, y, z float64)

func LuvToXYZWhiteRef

func LuvToXYZWhiteRef(l, u, v float64, wref [3]float64) (x, y, z float64)

func Name

func Name(c Color) string

Name - should have been Stringer implementation, but it's not needed for now

func RGB255

func RGB255(c RGB) (r, g, b uint8)

Might come in handy sometimes to reduce boilerplate code.

func ToFastLinearRGB

func ToFastLinearRGB(c RGB) (r, g, b float64)

NewRGBFromFastLinearRGB is much faster than and almost as accurate as NewRGBFromLinearRGB. they only produce good results for valid colors r,g,b in [0,1].

func ToHCL

func ToHCL(c RGB) (h, co, l float64)

Converts the given color to HCL space using D65 as reference white. H values are in [0..360], C and L values are in [0..1] although C can overshoot 1.0

func ToHSL

func ToHSL(c RGB) (h, s, l float64)

NewRGBFromHSL returns the Hue [0..360], Saturation [0..1], and Luminance (lightness) [0..1] of the color.

func ToHSV

func ToHSV(c RGB) (h, s, v float64)

NewRGBFromHSV returns the Hue [0..360], Saturation and Value [0..1] of the color.

func ToLab

func ToLab(c RGB) (l, a, b float64)

Converts the given color to CIE L*a*b* space using D65 as reference white.

func ToLinearRGB

func ToLinearRGB(c RGB) (r, g, b float64)

NewRGBFromLinearRGB converts the color into the linear RGB space (see http://www.sjbrown.co.uk/2004/05/14/gamma-correct-rendering/).

func ToLuv

func ToLuv(c RGB) (l, u, v float64)

Converts the given color to CIE L*u*v* space using D65 as reference white. L* is in [0..1] and both u* and v* are in about [-1..1]

func ToLuvWhiteRef

func ToLuvWhiteRef(c RGB, wref [3]float64) (l, u, v float64)

Converts the given color to CIE L*u*v* space, taking into account a given reference white. (i.e. the monitor's white) L* is in [0..1] and both u* and v* are in about [-1..1]

func ToRGB

func ToRGB(c Color) (int, int, int)

ToRGB returns the red, green, and blue components of the color, with each component represented as a value 0-255. In the event that the color cannot be broken up (not set usually), -1 is returned for each value.

func ToXYY

func ToXYY(c RGB) (x, y, Y float64)

Converts the given color to CIE xyY space using D65 as reference white. (Note that the reference white is only used for black input.) x, y and Y are in [0..1]

func ToXYYWhiteRef

func ToXYYWhiteRef(c RGB, wref [3]float64) (x, y, Y float64)

Converts the given color to CIE xyY space, taking into account a given reference white. (i.e. the monitor's white) (Note that the reference white is only used for black input.) x, y and Y are in [0..1]

func ToXYZ

func ToXYZ(c RGB) (x, y, z float64)

func TranslateANSI

func TranslateANSI(text string) string

TranslateANSI replaces ANSI escape sequences found in the provided string with color tags and returns the resulting string.

func Valid

func Valid(c Color) bool

Valid indicates the color is a valid value (has been set).

func XYYToXYZ

func XYYToXYZ(x, y, Y float64) (X, Yout, Z float64)

func XYZToLab

func XYZToLab(x, y, z float64) (l, a, b float64)

func XYZToLabWhiteRef

func XYZToLabWhiteRef(x, y, z float64, wref [3]float64) (l, a, b float64)

func XYZToLinearRGB

func XYZToLinearRGB(x, y, z float64) (r, g, b float64)

XYZToLinearRGB converts from CIE XYZ-space to Linear RGB space.

func XYZToLuv

func XYZToLuv(x, y, z float64) (l, a, b float64)

func XYZToLuvWhiteRef

func XYZToLuvWhiteRef(x, y, z float64, wref [3]float64) (l, u, v float64)

func XYZToXYY

func XYZToXYY(X, Y, Z float64) (x, y, Yout float64)

Well, the name is bad, since it's xyY but Golang needs me to start with a capital letter to make the method public.

func XYZToXYYWhiteRef

func XYZToXYYWhiteRef(X, Y, Z float64, wref [3]float64) (x, y, Yout float64)

Types

type Color

type Color uint64

Color represents a color. The low numeric values are the same as used by ECMA-48, and beyond that XTerm. A 24-bit RGB value may be used by adding in the IsRGB flag. For Color names we use the W3C approved color names.

We use a 64-bit integer to allow future expansion if we want to add an 8-bit alpha, while still leaving us some room for extra options.

Note that on various terminals colors may be approximated however, or not supported at all. If no suitable representation for a color is known, the library will simply not set any color, deferring to whatever default attributes the terminal uses.

const (
	Default Color = 0 // Default is used to leave the Color unchanged from whatever system or terminal default may exist. It's also the zero value.

	Special Color = 1 << 34 // Special is a flag used to indicate that the values have special meaning, and live outside of the color space(s).
)

func FindColor

func FindColor(c Color, palette []Color) Color

FindColor attempts to find a given color, or the best match possible for it, from the palette given. This is an expensive operation, so results should be cached by the caller.

func NewColor

func NewColor(name string) Color

NewColor creates a Color from a color name (W3C name). A hex value may be supplied as a string in the format "#ffffff".

func NewHexColor

func NewHexColor(v int32) Color

NewHexColor returns a color using the given 24-bit RGB value.

func NewRGBAColor

func NewRGBAColor(r, g, b, a uint32) Color

NewRGBAColor makes a color from imageColor ("image/color")

func NewRGBColor

func NewRGBColor(r, g, b int32) Color

NewRGBColor returns a new color with the given red, green, and blue values. Each value must be represented in the range 0-255.

func PaletteColor

func PaletteColor(index int) Color

PaletteColor creates a color based on the palette index.

func TrueColor

func TrueColor(c Color) Color

TrueColor returns the true color (RGB) version of the provided color. This is useful for ensuring color accuracy when using named colors. This will override terminal theme colors.

type RGB

type RGB struct {
	R float64
	G float64
	B float64
}

A color is stored internally using sRGB (standard RGB) values in the range 0-1

func FromXYY

func FromXYY(x, y, Y float64) RGB

Generates a color by using data given in CIE xyY space. x, y and Y are in [0..1]

func FromXYZ

func FromXYZ(x, y, z float64) RGB

func GenLabWhiteRef

func GenLabWhiteRef(l, a, b float64, wref [3]float64) RGB

Generates a color by using data given in CIE L*a*b* space, taking into account a given reference white. (i.e. the monitor's white)

func Lab

func Lab(l, a, b float64) RGB

Generates a color by using data given in CIE L*a*b* space using D65 as reference white. WARNING: many combinations of `l`, `a`, and `b` values do not have corresponding

valid RGB values, check the FAQ in the README if you're unsure.

func NewRGB

func NewRGB(col color.Color) (RGB, bool)

Constructs a color.RGB from something implementing color.Color

func NewRGBBlendLuv

func NewRGBBlendLuv(c1, c2 RGB, t float64) RGB

NewRGBBlendLuv blends two colors in the CIE-L*u*v* color-space, which should result in a smoother blend. t == 0 results in c1, t == 1 results in c2

func NewRGBFromBlendHCL

func NewRGBFromBlendHCL(col1, col2 RGB, t float64) RGB

NewRGBFromBlendHCL blends two colors in the CIE-L*C*h° color-space, which should result in a smoother blend. t == 0 results in c1, t == 1 results in c2

func NewRGBFromBlendHSV

func NewRGBFromBlendHSV(c1, c2 RGB, t float64) RGB

Use for NewRGBFromBlendLab, NewRGBBlendLuv or NewRGBFromBlendHCL.

func NewRGBFromBlendLab

func NewRGBFromBlendLab(c1, c2 RGB, t float64) RGB

NewRGBFromBlendLab blends two colors in the L*a*b* color-space, which should result in a smoother blend. t == 0 results in c1, t == 1 results in c2

func NewRGBFromBlendRGB

func NewRGBFromBlendRGB(c1, c2 RGB, t float64) RGB

Use NewRGBFromBlendLab, NewRGBBlendLuv or NewRGBFromBlendHCL.

func NewRGBFromClamped

func NewRGBFromClamped(c RGB) RGB

Returns Clamps the color into valid range, clamping each value to [0..1] If the color is valid already, this is a no-op.

func NewRGBFromFastLinearRGB

func NewRGBFromFastLinearRGB(r, g, b float64) RGB

NewRGBFromFastLinearRGB is much faster than and almost as accurate as NewRGBFromLinearRGB. BUT it is important to NOTE that they only produce good results for valid inputs r,g,b in [0,1].

func NewRGBFromHCL

func NewRGBFromHCL(h, c, l float64) RGB

Generates a color by using data given in HCL space using D65 as reference white. H values are in [0..360], C and L values are in [0..1] WARNING: many combinations of `l`, `a`, and `b` values do not have corresponding

valid RGB values, check the FAQ in the README if you're unsure.

func NewRGBFromHCLWhite

func NewRGBFromHCLWhite(h, c, l float64, wref [3]float64) RGB

Generates a color by using data given in HCL space, taking into account a given reference white. (i.e. the monitor's white) H values are in [0..360], C and L values are in [0..1]

func NewRGBFromHSL

func NewRGBFromHSL(h, s, l float64) RGB

NewRGBFromHSL creates a new RGB given a Hue in [0..360], a Saturation [0..1], and a Luminance (lightness) in [0..1]

func NewRGBFromHSV

func NewRGBFromHSV(H, S, V float64) RGB

NewRGBFromHSV creates a new RGB given a Hue in [0..360], a Saturation and a Value in [0..1]

func NewRGBFromHex

func NewRGBFromHex(color string) (RGB, error)

NewRGBFromHex parses a "html" hex color-string, either in the 3 "#f0c" or 6 "#ff1034" digits form.

func NewRGBFromLinearRGB

func NewRGBFromLinearRGB(r, g, b float64) RGB

NewRGBFromLinearRGB creates an sRGB color out of the given linear RGB color (see http://www.sjbrown.co.uk/2004/05/14/gamma-correct-rendering/).

func NewRGBFromLuv

func NewRGBFromLuv(l, u, v float64) RGB

Generates a color by using data given in CIE L*u*v* space using D65 as reference white. L* is in [0..1] and both u* and v* are in about [-1..1] WARNING: many combinations of `l`, `a`, and `b` values do not have corresponding

valid RGB values, check the FAQ in the README if you're unsure.

func NewRGBFromLuvWhiteRef

func NewRGBFromLuvWhiteRef(l, u, v float64, wref [3]float64) RGB

Generates a color by using data given in CIE L*u*v* space, taking into account a given reference white. (i.e. the monitor's white) L* is in [0..1] and both u* and v* are in about [-1..1]

func (*RGB) IsValid

func (r *RGB) IsValid() bool

Checks whether the color exists in RGB space, i.e. all values are in [0..1]

func (RGB) RGBA

func (r RGB) RGBA() (red, g, b, alpha uint32)

Implement the Go color.Color interface.

func (*RGB) Scan

func (r *RGB) Scan(value interface{}) error

func (RGB) String

func (r RGB) String() string

Name returns the hex "html" representation of the color, as in #ff0080.

func (*RGB) Value

func (r *RGB) Value() (driver.Value, error)

Jump to

Keyboard shortcuts

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