color

package
v12.120.0 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2024 License: Apache-2.0 Imports: 4 Imported by: 3

Documentation

Overview

Package color provides methods for working with colors

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Contrast

func Contrast(fg, bg Hex) float64

Contrast calculates contrast ratio of foreground and background colors

Example
c := Contrast(NewHex(0x222222), NewHex(0x40abf7))

fmt.Printf("ratio: %.2f:1\n", c)
Output:

ratio: 6.35:1

func HUE2RGB

func HUE2RGB(p, q, t float64) float64

HUE2RGB calculates HUE value for given RGB color

Example
hue := HUE2RGB(0.3, 0.12, 0.56)

fmt.Printf("Hue: %.4f\n", hue)
Output:

Hue: 0.1848

func Luminance

func Luminance(c RGB) float64

Luminance returns relative luminance for RGB color

Example
fmt.Printf("Lum: %.7f\n", Luminance(RGB{135, 85, 189}))
Output:

Lum: 0.1532202

func RGB2Term

func RGB2Term(c RGB) int

RGB2Term converts RGB color to terminal color code https://misc.flogisoft.com/bash/tip_colors_and_formatting#colors1

Example
c := RGB{255, 0, 0}

fmt.Printf("%s → \\e[38;5;%dm\n", c, RGB2Term(c))
Output:

RGB{R:255 G:0 B:0} → \e[38;5;196m

Types

type CMYK

type CMYK struct {
	C float64 // Cyan
	M float64 // Magenta
	Y float64 // Yellow
	K float64 // Key (black)
}

func RGB2CMYK

func RGB2CMYK(c RGB) CMYK

RGB2CMYK converts RGB color to CMYK

Example
fmt.Printf("%s\n", RGB2CMYK(RGB{127, 25, 75}))
Output:

CMYK{C:0% M:80% Y:41% K:50%}

func (CMYK) String

func (c CMYK) String() string

String returns string representation of CMYK color

func (CMYK) ToRGB

func (c CMYK) ToRGB() RGB

ToRGB converts CMYK color to RGB

Example
fmt.Printf("%s\n", CMYK{0, 0.8, 0.41, 0.5}.ToRGB())
Output:

RGB{R:127 G:25 B:75}

type HSL

type HSL struct {
	H float64 // Hue
	S float64 // Saturation
	L float64 // Value
	A float64 // Alpha
}

func RGB2HSL

func RGB2HSL(c RGB) HSL

RGB2HSL converts RGB color to HSL

Example
fmt.Printf("%s\n", RGB2HSL(RGB{127, 25, 75}))
Output:

HSL{H:331° S:67% L:30% A:0%}

func RGBA2HSL added in v12.90.0

func RGBA2HSL(c RGBA) HSL

RGBA2HSL converts RGBA color to HSL

func (HSL) String

func (c HSL) String() string

String returns string representation of HSL color

func (HSL) ToRGB

func (c HSL) ToRGB() RGB

ToRGB converts HSL color to RGB

Example
c := HSL{H: 331.0 / 360.0, S: 67.0 / 100.0, L: 30.0 / 100.0}.ToRGB()

fmt.Printf("%s\n", c)
Output:

RGB{R:127 G:25 B:74}

func (HSL) ToRGBA added in v12.90.0

func (c HSL) ToRGBA() RGBA

ToRGBA converts HSL color to RGBA

type HSV

type HSV struct {
	H float64 // Hue
	S float64 // Saturation
	V float64 // Lightness
	A float64 // Alpha
}

func RGB2HSV

func RGB2HSV(c RGB) HSV

RGB2HSV converts RGB color to HSV (HSB)

Example
fmt.Printf("%s\n", RGB2HSV(RGB{127, 25, 75}))
Output:

HSV{H:331° S:80% V:50% A:0%}

func RGBA2HSV added in v12.90.0

func RGBA2HSV(c RGBA) HSV

RGBA2HSV converts RGBA color to HSV (HSB)

func (HSV) String

func (c HSV) String() string

String returns string representation of HSV color

func (HSV) ToRGB

func (c HSV) ToRGB() RGB

ToRGB converts HSV color to RGB

Example
c := HSV{H: 331.0 / 360.0, S: 80.0 / 100.0, V: 50.0 / 100.0}.ToRGB()

fmt.Printf("%s\n", c)
Output:

RGB{R:127 G:25 B:74}

func (HSV) ToRGBA added in v12.90.0

func (c HSV) ToRGBA() RGBA

ToRGBA converts HSV color to RGBA

type Hex

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

func NewHex added in v12.90.0

func NewHex(c uint32) Hex

NewHex creates new hex color from uint32 number

func Parse

func Parse(c string) (Hex, error)

Parse parses color

Example
fmt.Println(Parse("#ff6347"))
fmt.Println(Parse("#B8F"))
fmt.Println(Parse("#FF3B21A6"))
fmt.Println(Parse("mintcream"))
Output:

Hex{#FF6347} <nil>
Hex{#BB88FF} <nil>
Hex{#FF3B21A6} <nil>
Hex{#F5FFFA} <nil>

func RGB2Hex

func RGB2Hex(c RGB) Hex

RGB2Hex converts RGB color to Hex

Example
fmt.Printf("%s\n", RGB2Hex(RGB{127, 25, 75}).ToWeb(true, false))
Output:

#7F194B

func RGBA2Hex

func RGBA2Hex(c RGBA) Hex

RGBA2Hex converts RGBA color to Hex

Example
c := RGBA2Hex(RGBA{127, 25, 75, 204})

fmt.Printf("%s\n", c.ToWeb(true, false))
Output:

#7F194BCC

func (Hex) IsRGBA

func (c Hex) IsRGBA() bool

IsRGBA returns true if color contains info about alpha channel

Example
c1 := NewHex(0x7F194B)
c2 := NewHex(0x7F194B5F)

fmt.Printf("%s → %t\n", c1.ToWeb(true, false), c1.IsRGBA())
fmt.Printf("%s → %t\n", c2.ToWeb(true, false), c2.IsRGBA())
Output:

#7F194B → false
#7F194B5F → true

func (Hex) String

func (c Hex) String() string

String returns string representation of hex color

func (Hex) ToRGB

func (c Hex) ToRGB() RGB

ToRGB converts hex color to RGB

Example
fmt.Printf("%s\n", NewHex(0x7F194B).ToRGB())
Output:

RGB{R:127 G:25 B:75}

func (Hex) ToRGBA

func (c Hex) ToRGBA() RGBA

ToRGB converts hex color to RGBA

Example
fmt.Printf("%s\n", NewHex(0x7F194B5F).ToRGBA())
Output:

RGBA{R:127 G:25 B:75 A:0.37}

func (Hex) ToWeb

func (c Hex) ToWeb(useCaps, allowShorthand bool) string

ToWeb converts hex color to notation used in web (#RGB/#RGBA/#RRGGBB/#RRGGBBAA)

Example
fmt.Printf("%s\n", NewHex(0xFFAA44CC).ToWeb(true, false))
fmt.Printf("%s\n", NewHex(0xFFAA44CC).ToWeb(false, true))
Output:

#FFAA44CC
#fa4c

type RGB

type RGB struct {
	R uint8 // Red
	G uint8 // Green
	B uint8 // Blue
}

func CMYK2RGB

func CMYK2RGB(c CMYK) RGB

CMYK2RGB converts CMYK color to RGB

Example
fmt.Printf("%s\n", CMYK2RGB(CMYK{0, 0.8, 0.41, 0.5}))
Output:

RGB{R:127 G:25 B:75}

func HSL2RGB

func HSL2RGB(c HSL) RGB

HSL2RGB converts HSL color to RGB

Example
c := HSL2RGB(HSL{H: 331.0 / 360.0, S: 67.0 / 100.0, L: 30.0 / 100.0})

fmt.Printf("%s\n", c)
Output:

RGB{R:127 G:25 B:74}

func HSV2RGB

func HSV2RGB(c HSV) RGB

HSV2RGB converts HSV (HSB) color to RGB

Example
c := HSV2RGB(HSV{H: 331.0 / 360.0, S: 80.0 / 100.0, V: 50.0 / 100.0})

fmt.Printf("%s\n", c)
Output:

RGB{R:127 G:25 B:74}

func Hex2RGB

func Hex2RGB(h Hex) RGB

Hex2RGB converts Hex color to RGB

Example
fmt.Printf("%s\n", Hex2RGB(NewHex(0x7F194B)))
Output:

RGB{R:127 G:25 B:75}

func Term2RGB added in v12.92.0

func Term2RGB(c uint8) RGB

Term2RGB converts terminal color code (0-255) to RGB color https://misc.flogisoft.com/bash/tip_colors_and_formatting#colors1

Example
c := uint8(162)

fmt.Printf("%d → %s\n", c, Term2RGB(c))
Output:

162 → RGB{R:215 G:0 B:135}

func (RGB) String

func (c RGB) String() string

String returns string representation of RGB color

func (RGB) ToCMYK

func (c RGB) ToCMYK() CMYK

ToCMYK converts RGB color to CMYK

Example
fmt.Printf("%s\n", RGB{127, 25, 75}.ToCMYK())
Output:

CMYK{C:0% M:80% Y:41% K:50%}

func (RGB) ToHSL

func (c RGB) ToHSL() HSL

ToHSL converts RGB color to HSL

Example
fmt.Printf("%s\n", RGB{127, 25, 75}.ToHSL())
Output:

HSL{H:331° S:67% L:30% A:0%}

func (RGB) ToHSV

func (c RGB) ToHSV() HSV

ToHSV converts RGB color to HSV

Example
fmt.Printf("%s\n", RGB{127, 25, 75}.ToHSV())
Output:

HSV{H:331° S:80% V:50% A:0%}

func (RGB) ToHex

func (c RGB) ToHex() Hex

ToHex converts RGB color to hex

Example
fmt.Printf("%s\n", RGB{127, 25, 75}.ToHex().ToWeb(true, false))
Output:

#7F194B

func (RGB) ToTerm

func (c RGB) ToTerm() int

ToTerm converts RGB color to terminal color code

Example
c := RGB{255, 0, 0}

fmt.Printf("%s → \\e[38;5;%dm\n", c, c.ToTerm())
Output:

RGB{R:255 G:0 B:0} → \e[38;5;196m

type RGBA

type RGBA struct {
	R uint8 // Red
	G uint8 // Green
	B uint8 // Blue
	A uint8 // Alpha
}

func HSL2RGBA added in v12.90.0

func HSL2RGBA(c HSL) RGBA

HSL2RGBA converts HSL color to RGBA

func HSV2RGBA added in v12.90.0

func HSV2RGBA(c HSV) RGBA

HSV2RGBA converts HSV (HSB) color to RGBA

func Hex2RGBA

func Hex2RGBA(h Hex) RGBA

Hex2RGBA converts Hex color to RGBA

Example
fmt.Printf("%s\n", Hex2RGBA(NewHex(0x7F194BCC)))
Output:

RGBA{R:127 G:25 B:75 A:0.80}

func (RGBA) String

func (c RGBA) String() string

String returns string representation of RGBA color

func (RGBA) ToHSL added in v12.90.0

func (c RGBA) ToHSL() HSL

ToHSL converts RGBA color to HSL

func (RGBA) ToHSV added in v12.90.0

func (c RGBA) ToHSV() HSV

ToHSV converts RGBA color to HSV

func (RGBA) ToHex

func (c RGBA) ToHex() Hex

ToHex converts RGBA color to hex

Example
c := RGBA{127, 25, 75, 204}.ToHex()

fmt.Printf("%s\n", c.ToWeb(true, false))
Output:

#7F194BCC

func (RGBA) WithAlpha added in v12.90.0

func (c RGBA) WithAlpha(alpha float64) RGBA

WithAlpha returns color with given alpha value

Jump to

Keyboard shortcuts

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