color

package
v12.41.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2022 License: Apache-2.0 Imports: 4 Imported by: 0

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(0x222222, 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 convert 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
}

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%}

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{331.0 / 360.0, 67.0 / 100.0, 30.0 / 100.0}.ToRGB()

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

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

type HSV

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

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%}

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{331.0 / 360.0, 80.0 / 100.0, 50.0 / 100.0}.ToRGB()

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

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

type Hex

type Hex uint32 // Hex color 0x00000000 - 0xFFFFFFFF

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))
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))
Output:

#7F194BCC

func (Hex) IsRGBA

func (c Hex) IsRGBA() bool

IsRGBA returns true if color contains info about alpha channel

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

fmt.Printf("%s → %t\n", c1.ToWeb(true), c1.IsRGBA())
fmt.Printf("%s → %t\n", c2.ToWeb(true), 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", Hex(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", Hex(0x7F194B5F).ToRGBA())
Output:

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

func (Hex) ToWeb

func (c Hex) ToWeb(caps bool) string

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

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

#7F194B
#7f194b

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{331.0 / 360.0, 67.0 / 100.0, 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{331.0 / 360.0, 80.0 / 100.0, 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(0x7F194B))
Output:

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

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%}

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%}

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))
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 Hex2RGBA

func Hex2RGBA(h Hex) RGBA

Hex2RGBA converts Hex color to RGBA

Example
fmt.Printf("%s\n", Hex2RGBA(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) 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))
Output:

#7F194BCC

Jump to

Keyboard shortcuts

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