Documentation
¶
Overview ¶
Package color implements common color math functions and provides color manipulation capabilities.
Example ¶
package main
import (
"fmt"
"github.com/shelepuginivan/color"
)
func main() {
// Parse color from CSS functions.
scarlet := color.Must(color.Parse("hsl(8deg, 100%, 50%)"))
// Generate shades, tints, and tones.
for _, tone := range color.Tones(scarlet, 5) {
// Color wheel functions.
c1, c2 := color.SplitComplementary(tone)
fmt.Println(tone.Hex(), c1.Hex(), c2.Hex())
}
mint := color.NewRGB(209, 227, 217)
// Mix colors in different colorspaces.
peach := color.MixLab(scarlet, mint)
// Convert colors to different colorspaces.
fmt.Println(peach.XYZWithWhitepoint(color.D55))
}
Output:
Index ¶
- Variables
- func Analogous(c Color) (Color, Color)
- func Contrast(c1, c2 Color) float64
- func Luminance(color Color) float64
- func SplitComplementary(c Color) (Color, Color)
- func Tetradic(c Color) (Color, Color, Color)
- func Triadic(c Color) (Color, Color)
- type CMYK
- func (c CMYK) CMYK() *CMYK
- func (c *CMYK) Edit(editfn func(c *CMYK)) *CMYK
- func (c CMYK) HSL() *HSL
- func (c CMYK) HSV() *HSV
- func (c CMYK) Hex() string
- func (c CMYK) Lab() *Lab
- func (c CMYK) Lch() *Lch
- func (c CMYK) Oklab() *Oklab
- func (c CMYK) Oklch() *Oklch
- func (c CMYK) RGB() *RGB
- func (c CMYK) String() string
- func (c CMYK) XYZ() *XYZ
- type Color
- func Complementary(c Color) Color
- func ContrastBlackWhite(c1 Color) Color
- func Must(c Color, err error) Color
- func Parse(s string) (Color, error)
- func ParseFunc(fnstring string) (Color, error)
- func ParseHex(hex string) (Color, error)
- func ParseNamed(name string) (Color, error)
- func Shades(color Color, n int) []Color
- func Tints(color Color, n int) []Color
- func Tones(color Color, n int) []Color
- type HSL
- func (c HSL) CMYK() *CMYK
- func (c *HSL) Edit(editfn func(c *HSL)) *HSL
- func (c HSL) HSL() *HSL
- func (c HSL) HSV() *HSV
- func (c HSL) Hex() string
- func (c HSL) Lab() *Lab
- func (c HSL) Lch() *Lch
- func (c HSL) Oklab() *Oklab
- func (c HSL) Oklch() *Oklch
- func (c HSL) RGB() *RGB
- func (c HSL) String() string
- func (c HSL) XYZ() *XYZ
- type HSV
- func (c HSV) CMYK() *CMYK
- func (c *HSV) Edit(editfn func(c *HSV)) *HSV
- func (c HSV) HSL() *HSL
- func (c HSV) HSV() *HSV
- func (c HSV) Hex() string
- func (c HSV) Lab() *Lab
- func (c HSV) Lch() *Lch
- func (c HSV) Oklab() *Oklab
- func (c HSV) Oklch() *Oklch
- func (c HSV) RGB() *RGB
- func (c HSV) String() string
- func (c HSV) XYZ() *XYZ
- type Lab
- func (c Lab) CMYK() *CMYK
- func (c *Lab) Edit(editfn func(c *Lab)) *Lab
- func (c Lab) HSL() *HSL
- func (c Lab) HSV() *HSV
- func (c Lab) Hex() string
- func (c Lab) Lab() *Lab
- func (c Lab) Lch() *Lch
- func (c Lab) Oklab() *Oklab
- func (c Lab) Oklch() *Oklch
- func (c Lab) RGB() *RGB
- func (c Lab) String() string
- func (c Lab) XYZ() *XYZ
- func (c Lab) XYZWithWhitepoint(white *XYZ) *XYZ
- type Lch
- func (c Lch) CMYK() *CMYK
- func (c *Lch) Edit(editfn func(c *Lch)) *Lch
- func (c Lch) HSL() *HSL
- func (c Lch) HSV() *HSV
- func (c Lch) Hex() string
- func (c Lch) Lab() *Lab
- func (c Lch) Lch() *Lch
- func (c Lch) Oklab() *Oklab
- func (c Lch) Oklch() *Oklch
- func (c Lch) RGB() *RGB
- func (c Lch) String() string
- func (c Lch) XYZ() *XYZ
- type Oklab
- func (c Oklab) CMYK() *CMYK
- func (c *Oklab) Edit(editfn func(*Oklab)) *Oklab
- func (c Oklab) HSL() *HSL
- func (c Oklab) HSV() *HSV
- func (c Oklab) Hex() string
- func (c Oklab) Lab() *Lab
- func (c Oklab) Lch() *Lch
- func (c Oklab) Oklab() *Oklab
- func (c Oklab) Oklch() *Oklch
- func (c Oklab) RGB() *RGB
- func (c Oklab) String() string
- func (c Oklab) XYZ() *XYZ
- type Oklch
- func (c Oklch) CMYK() *CMYK
- func (c *Oklch) Edit(editfn func(*Oklch)) *Oklch
- func (c Oklch) HSL() *HSL
- func (c Oklch) HSV() *HSV
- func (c Oklch) Hex() string
- func (c Oklch) Lab() *Lab
- func (c Oklch) Lch() *Lch
- func (c Oklch) Oklab() *Oklab
- func (c Oklch) Oklch() *Oklch
- func (c Oklch) RGB() *RGB
- func (c Oklch) String() string
- func (c Oklch) XYZ() *XYZ
- type RGB
- func (c RGB) CMYK() *CMYK
- func (c *RGB) Edit(editfn func(c *RGB)) *RGB
- func (c RGB) HSL() *HSL
- func (c RGB) HSV() *HSV
- func (c RGB) Hex() string
- func (c RGB) Lab() *Lab
- func (c RGB) Lch() *Lch
- func (c RGB) Oklab() *Oklab
- func (c RGB) Oklch() *Oklch
- func (c RGB) RGB() *RGB
- func (c RGB) String() string
- func (c RGB) XYZ() *XYZ
- type XYZ
- func (c XYZ) CMYK() *CMYK
- func (c *XYZ) Edit(editfn func(c *XYZ)) *XYZ
- func (c XYZ) HSL() *HSL
- func (c XYZ) HSV() *HSV
- func (c XYZ) Hex() string
- func (c XYZ) Lab() *Lab
- func (c XYZ) LabWithWhitepoint(white *XYZ) *Lab
- func (c XYZ) Lch() *Lch
- func (c XYZ) Oklab() *Oklab
- func (c XYZ) Oklch() *Oklch
- func (c XYZ) RGB() *RGB
- func (c XYZ) String() string
- func (c XYZ) XYZ() *XYZ
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // CIE standard illuminant A. Simulates typical, domestic, // tungsten-filament lighting with correlated color temperature of 2856 K. A = &XYZ{1.0985, 1.0000, 0.3558} // CIE standard illuminant C. Simulates average or north sky daylight with // correlated color temperature of 6774 K. Deprecated by CIE. C = &XYZ{0.9807, 1.0000, 1.1822} // Equal-energy radiator. Useful as a theoretical reference. E = &XYZ{1.0000, 1.0000, 1.0000} // CIE standard illuminant D50. Simulates warm daylight at sunrise or // sunset with correlated color temperature of 5003 K. Also known as // horizon light. D50 = &XYZ{0.9642, 1.0000, 0.8251} // CIE standard illuminant D55. Simulates mid-morning or mid-afternoon // daylight with correlated color temperature of 5500 K. D55 = &XYZ{0.9568, 1.0000, 0.9214} // CIE standard illuminant D65. Simulates noon daylight with correlated // color temperature of 6504 K. D65 = &XYZ{0.9505, 1.0000, 1.0888} // Profile Connection Space (PCS) illuminant used in ICC profiles. ICC = &XYZ{0.9642, 1.000, 0.8249} )
Predefined whitepoints.
Functions ¶
func Analogous ¶
Analogous returns two colors adjacent to the given one.
Example ¶
package main
import (
"fmt"
"github.com/shelepuginivan/color"
)
func main() {
orange := color.Must(color.ParseHex("#ff8000"))
// Red and yellow are adjacent to orange.
red, yellow := color.Analogous(orange)
fmt.Println(red.Hex())
fmt.Println(yellow.Hex())
}
Output: #ff0000 #ffff00
func Luminance ¶
Luminance calculates relative luminance of the color, normalized to 0 for darkest black and 1 for lightest white.
Relative luminance is calculates as per WCAG 2.2.
func SplitComplementary ¶
SplitComplementary returns two colors adjacent to the complementary color of the given one.
Example ¶
package main
import (
"fmt"
"github.com/shelepuginivan/color"
)
func main() {
cyan := color.Must(color.ParseHex("#00ffff"))
// Crimson and orange are split complementary colors of cyan.
crimson, orange := color.SplitComplementary(cyan)
fmt.Println(crimson.Hex())
fmt.Println(orange.Hex())
}
Output: #ff0080 #ff8000
func Tetradic ¶
Tetradic returns 3 of the remaining colors from the tetradic colorscheme for the given color.
Example ¶
package main
import (
"fmt"
"github.com/shelepuginivan/color"
)
func main() {
magenta := color.Must(color.ParseHex("ff00ff"))
// Orange, green and blue are the colors in the tetradic colorscheme with magenta.
orange, green, blue := color.Tetradic(magenta)
fmt.Println(orange.Hex())
fmt.Println(green.Hex())
fmt.Println(blue.Hex())
}
Output: #ff8000 #00ff00 #007fff
func Triadic ¶
Triadic returns 2 of the remaining colors from the triadic colorscheme for the given color.
Example ¶
package main
import (
"fmt"
"github.com/shelepuginivan/color"
)
func main() {
red := color.Must(color.ParseHex("#ff0000"))
// Green and blue are the colors in the triadic colorscheme with red.
green, blue := color.Triadic(red)
fmt.Println(green.Hex())
fmt.Println(blue.Hex())
}
Output: #00ff00 #0000ff
Types ¶
type CMYK ¶
type CMYK struct {
C int // Cyan (in percents).
M int // Magenta (in percents).
Y int // Yellow (in percents).
K int // Black key (in percents).
}
CMYK represents a color in CMYK color space.
func MixCMYK ¶
MixCMYK calculates the average color in CMYK colorspace from an arbitrary number of colors.
Example ¶
package main
import (
"fmt"
"github.com/shelepuginivan/color"
)
func main() {
var (
cyan = color.NewCMYK(100, 0, 0, 40)
magenta = color.NewCMYK(0, 100, 0, 20)
yellow = color.NewCMYK(0, 0, 100, 60)
)
grey := color.MixCMYK(cyan, magenta, yellow)
fmt.Printf("cmyk(%d%%, %d%%, %d%%, %d%%)", grey.C, grey.M, grey.Y, grey.K)
}
Output: cmyk(33%, 33%, 33%, 40%)
func (CMYK) CMYK ¶
CMYK returns the color unchanged. This method is required to implement the Color interface.
func (*CMYK) Edit ¶
Edit allows in-place modification of the CMYK color instance using the provided editing function.
The returned value is a pointer to the same instance of CMYK, so it should not be used to assign values to other variables. It is intended for method chaining.
Example ¶
package main
import (
"fmt"
"github.com/shelepuginivan/color"
)
func main() {
orange := color.NewCMYK(0, 47, 90, 0) // rgb(255, 134, 26)
// Make it darker and print RGB.
fmt.Println(orange.Edit(func(c *color.CMYK) {
c.K = 50
}).RGB())
}
Output: rgb(128, 68, 13)
func (CMYK) HSL ¶
HSL returns HSL representation of color (hue, saturation, lightness).
Example ¶
package main
import (
"fmt"
"github.com/shelepuginivan/color"
)
func main() {
skyblue := color.NewCMYK(43, 12, 0, 8)
fmt.Println(skyblue.HSL())
}
Output: hsl(197, 72%, 72%)
func (CMYK) Hex ¶
Hex returns hexadecimal representation of color.
Example ¶
package main
import (
"fmt"
"github.com/shelepuginivan/color"
)
func main() {
coral := color.NewCMYK(0, 50, 70, 0)
fmt.Println(coral.Hex())
}
Output: #ff804d
func (CMYK) Lab ¶
Lab returns Lab representation of color (lightness, red-green, yellow-blue).
D65 is used as a reference white. Use XYZ.LabWithWhitepoint to specify a different whitepoint.
func (CMYK) Lch ¶
Lch returns Lch representation of color (lightness, chroma, hue).
D65 is used as a reference white. Use XYZ.LabWithWhitepoint to specify a different whitepoint.
func (CMYK) Oklab ¶ added in v0.2.0
Oklab returns Oklab representation of color (lightness, red-green, yellow-blue).
func (CMYK) Oklch ¶ added in v0.2.0
Oklch returns Oklch representation of color (lightness, chroma, hue).
type Color ¶
type Color interface {
CMYK() *CMYK
Hex() string
HSL() *HSL
HSV() *HSV
Lab() *Lab
Lch() *Lch
Oklab() *Oklab
Oklch() *Oklch
RGB() *RGB
XYZ() *XYZ
String() string
}
Color is a common interface for all package structures. It is used for methods that accept colors in any color space.
func Complementary ¶
Complementary returns a complementary color for the given one. Complementary colors are two colors that are directly across from one another on the color wheel.
Example ¶
package main
import (
"fmt"
"github.com/shelepuginivan/color"
)
func main() {
blue := color.Must(color.ParseHex("#0000ff"))
// Yellow is the complementary color for blue.
yellow := color.Complementary(blue)
fmt.Println(yellow.Hex())
}
Output: #ffff00
func ContrastBlackWhite ¶
ContrastBlackWhite returns black or white depending on luminance of the given color.
func Must ¶
Must is a helper that wraps a call to a function returning (Color, error) and panics if the error is not nil. It is intended for use in variable initializations such as
c := color.Must(color.ParseHex("#ffffff"))
func Parse ¶
Parse is a generic color parsing function.
The following order is used for parsing:
- CSS named colors using color.ParseNamed
- Hexadecimal notation using color.ParseHex
- Color functions using color.ParseFunc
See color.ParseNamed, color.ParseHex, and color.ParseFunc for more information about color notations supported by Parse.
func ParseFunc ¶
ParseFunc parses the color function and returns a color.
A color function is a string starting with the function name (e.g. rgb), containing its parameters in parentheses. Some examples are:
- rgb(255, 255, 255)
- hsl(0.8turn 80% 30%)
- xyz(0.9642, 1.0000, 0.8251)
Arguments of the function may or may not be comma-separated.
The following units are supported for the arguments:
- Percents (%)
- Radians (rad)
- Turns (turn)
- Degrees (deg)
Additionally, none is interpreted as zero:
color.ParseFunc("lch(29.2345% 44.2 none)") // &color.Lch{0.29345, 44.2, 0}
Note that ParseFunc does not implement CSS color functions completely. For example, there is no support for relative values; i.e. the following call:
color.ParseFunc("hsl(from green h s l / 0.5)")
results in error.
func ParseHex ¶
ParseHex returns a color by parsing hexadecimal color string. The string may start with hash character (`#`) and may be either short or long hexadecimal color. Hence, all of the following strings are valid arguments:
- fff
- #fff
- ffffff
- #ffffff
func ParseNamed ¶
ParseNamed returns a CSS named color by name.
func Shades ¶
Shades returns n shades of the color. New colors are created by reducing lightness of the original color, making them darker.
The first element of the resulting slice is the color itself. The last is black. Special case is n < 2: a slice with a single element being the color itself is returned.
func Tints ¶
Tints returns n tints shades of the color. New colors are created by increasing lightness of the original color, making them lighter.
The first element of the resulting slice is the color itself. The last is white. Special case is n < 2: a slice with a single element being the color itself is returned.
func Tones ¶
Tones returns n tones of the color. New colors are created by reducing saturation of the original color and changing lightness towards 50% (in HSL), making them more grayish.
The first element of the resulting slice is the color itself. The last is gray. Special case is n < 2: a slice with a single element being the color itself is returned.
type HSL ¶
type HSL struct {
H int // Hue (in degrees).
S int // Saturation (in percents).
L int // Lightness (in percents).
}
HSL represents color in HSL colorspace.
func MixHSL ¶
MixHSL calculates the average color in HSL colorspace from an arbitrary number of colors.
func (*HSL) Edit ¶
Edit allows in-place modification of the HSL color instance using the provided editing function.
The returned value is a pointer to the same instance of HSL, so it should not be used to assign values to other variables. It is intended for method chaining.
Example ¶
package main
import (
"fmt"
"github.com/shelepuginivan/color"
)
func main() {
cyan := color.NewHSL(180, 100, 50) // rgb(0, 255, 255)
// Decrease saturation and print as RGB.
fmt.Println(cyan.Edit(func(c *color.HSL) {
c.S = 30
}).RGB())
}
Output: rgb(89, 166, 166)
func (HSL) HSL ¶
HSL returns the color unchanged. This method is required to implement the Color interface.
func (HSL) HSV ¶
HSV returns HSV representation of color (hue, saturation, value).
Example ¶
package main
import (
"fmt"
"github.com/shelepuginivan/color"
)
func main() {
c := color.NewHSL(30, 20, 60)
hsv := c.HSV()
fmt.Printf("hsv(%d, %d%%, %d%%)\n", hsv.H, hsv.S, hsv.V)
}
Output: hsv(30, 24%, 68%)
func (HSL) Lab ¶
Lab returns Lab representation of color (lightness, red-green, yellow-blue).
D65 is used as a reference white. Use XYZ.LabWithWhitepoint to specify a different whitepoint.
func (HSL) Lch ¶
Lch returns Lch representation of color (lightness, chroma, hue).
D65 is used as a reference white. Use XYZ.LabWithWhitepoint to specify a different whitepoint.
func (HSL) Oklab ¶ added in v0.2.0
Oklab returns Oklab representation of color (lightness, red-green, yellow-blue).
func (HSL) Oklch ¶ added in v0.2.0
Oklch returns Oklch representation of color (lightness, chroma, hue).
type HSV ¶
type HSV struct {
H int // Hue (in degrees).
S int // Saturation (in percents).
V int // Value (in percents).
}
HSV represents color in HSV colorspace.
func (*HSV) Edit ¶
Edit allows in-place modification of the HSV color instance using the provided editing function.
The returned value is a pointer to the same instance of HSV, so it should not be used to assign values to other variables. It is intended for method chaining.
Example ¶
package main
import (
"fmt"
"github.com/shelepuginivan/color"
)
func main() {
green := color.NewHSV(72, 100, 40) // rgb(78, 102, 0)
// Increase value and print as RGB.
fmt.Println(green.Edit(func(c *color.HSV) {
c.V = 100
}).RGB())
}
Output: rgb(204, 255, 0)
func (HSV) HSV ¶
HSV returns the color unchanged. This method is required to implement the Color interface.
func (HSV) Lab ¶
Lab returns Lab representation of color (lightness, red-green, yellow-blue).
D65 is used as a reference white. Use XYZ.LabWithWhitepoint to specify a different whitepoint.
func (HSV) Lch ¶
Lch returns Lch representation of color (lightness, chroma, hue).
D65 is used as a reference white. Use XYZ.LabWithWhitepoint to specify a different whitepoint.
func (HSV) Oklab ¶ added in v0.2.0
Oklab returns Oklab representation of color (lightness, red-green, yellow-blue).
func (HSV) Oklch ¶ added in v0.2.0
Oklch returns Oklch representation of color (lightness, chroma, hue).
type Lab ¶
type Lab struct {
L float64 // L represents lightness of the color.
A float64 // A represents the green-red component of the color.
B float64 // B represents the yellow-blue component of the color.
}
Lab represents a color in Lab colorspace.
func MixLab ¶
MixLab calculates the average color in Lab colorspace from an arbitrary number of colors.
func (*Lab) Edit ¶
Edit allows in-place modification of the Lab color instance using the provided editing function.
The returned value is a pointer to the same instance of Lab, so it should not be used to assign values to other variables. It is intended for method chaining.
func (Lab) Lab ¶
Lab returns the color unchanged. This method is required to implement the Color interface.
func (Lab) Oklab ¶ added in v0.2.0
Oklab returns Oklab representation of color (lightness, red-green, yellow-blue).
func (Lab) Oklch ¶ added in v0.2.0
Oklch returns Oklch representation of color (lightness, chroma, hue).
func (Lab) XYZ ¶
XYZ returns XYZ representation of color. D65 is used as whitepoint, use Lab.XYZWithWhitepoint to specify a different whitepoint.
type Lch ¶
type Lch struct {
L float64 // L represents lightness of the color.
C float64 // C represents relative saturation (chroma).
H int // H represents angle of the hue in the CIELAB color wheel (in degrees).
}
Lch represents a color in Lch colorspace.
func (*Lch) Edit ¶
Edit allows in-place modification of the Lch color instance using the provided editing function.
The returned value is a pointer to the same instance of Lch, so it should not be used to assign values to other variables. It is intended for method chaining.
func (Lch) Lch ¶
Lch returns the color unchanged. This method is required to implement the Color interface.
func (Lch) Oklab ¶ added in v0.2.0
Oklab returns Oklab representation of color (lightness, red-green, yellow-blue).
func (Lch) Oklch ¶ added in v0.2.0
Oklch returns Oklch representation of color (lightness, chroma, hue).
type Oklab ¶ added in v0.2.0
type Oklab struct {
L float64 // L represents lightness of the color.
A float64 // A represents the green-red component of the color.
B float64 // B represents the yellow-blue component of the color.
}
Oklab represents a color in Oklab colorspace.
func (Oklab) CMYK ¶ added in v0.2.0
CMYK returns CMYK representation of color (cyan, magenta, yellow, key).
func (*Oklab) Edit ¶ added in v0.2.0
Edit allows in-place modification of the Oklab color instance using the provided editing function.
The returned value is a pointer to the same instance of Oklab, so it should not be used to assign values to other variables. It is intended for method chaining.
func (Oklab) HSL ¶ added in v0.2.0
HSL returns HSL representation of color (hue, saturation, lightness).
func (Oklab) HSV ¶ added in v0.2.0
HSV returns HSV representation of color (hue, saturation, value).
func (Oklab) Lab ¶ added in v0.2.0
Lab returns the color unchanged. This method is required to implement the Color interface.
func (Oklab) Lch ¶ added in v0.2.0
Lch returns Lch representation of color (lightness, chroma, hue).
func (Oklab) Oklab ¶ added in v0.2.0
Oklab returns the color unchanged. This method is required to implement the Color interface.
func (Oklab) Oklch ¶ added in v0.2.0
Oklch returns Oklch representation of color (lightness, chroma, hue).
type Oklch ¶ added in v0.2.0
type Oklch struct {
L float64 // L represents lightness of the color.
C float64 // C represents relative saturation (chroma).
H int // H represents angle of the hue in the Oklab color wheel (in degrees).
}
Oklch represents a color in Oklch colorspace, a cylindrical counterpart of Oklab.
func (Oklch) CMYK ¶ added in v0.2.0
CMYK returns CMYK representation of color (cyan, magenta, yellow, key).
func (*Oklch) Edit ¶ added in v0.2.0
Edit allows in-place modification of the Oklch color instance using the provided editing function.
The returned value is a pointer to the same instance of Oklch, so it should not be used to assign values to other variables. It is intended for method chaining.
func (Oklch) HSL ¶ added in v0.2.0
HSL returns HSL representation of color (hue, saturation, lightness).
func (Oklch) HSV ¶ added in v0.2.0
HSV returns HSV representation of color (hue, saturation, value).
func (Oklch) Lab ¶ added in v0.2.0
Lab returns Lab representation of color (lightness, red-green, yellow-blue).
func (Oklch) Lch ¶ added in v0.2.0
Lch returns Lch representation of color (lightness, chroma, hue).
func (Oklch) Oklab ¶ added in v0.2.0
Oklab returns Oklab representation of color (lightness, red-green, yellow-blue).
func (Oklch) Oklch ¶ added in v0.2.0
Oklch returns the color unchanged. This method is required to implement the Color interface.
type RGB ¶
RGB represents a color in RGB color space.
func MixRGB ¶
MixRGB calculates the average color in RGB colorspace from an arbitrary number of colors.
Example ¶
package main
import (
"fmt"
"github.com/shelepuginivan/color"
)
func main() {
var (
red = color.NewRGB(255, 0, 0)
green = color.NewRGB(0, 255, 0)
blue = color.NewRGB(0, 0, 255)
)
grey := color.MixRGB(red, green, blue)
fmt.Printf("rgb(%d, %d, %d)\n", grey.R, grey.G, green.B)
}
Output:
func NewRGB ¶
NewRGB returns a new instance of RGB.
Example ¶
package main
import (
"fmt"
"github.com/shelepuginivan/color"
)
func main() {
green := color.NewRGB(0, 255, 0)
fmt.Println(green.Hex())
}
Output: #00ff00
func (RGB) CMYK ¶
CMYK returns CMYK representation of color (cyan, magenta, yellow, key).
Example ¶
package main
import (
"fmt"
"github.com/shelepuginivan/color"
)
func main() {
yellow := color.NewRGB(255, 255, 0)
cmyk := yellow.CMYK()
fmt.Printf("cmyk(%d%%, %d%%, %d%%, %d%%)", cmyk.C, cmyk.M, cmyk.Y, cmyk.K)
}
Output: cmyk(0%, 0%, 100%, 0%)
func (*RGB) Edit ¶
Edit allows in-place modification of the RGB color instance using the provided editing function.
The returned value is a pointer to the same instance of RGB, so it should not be used to assign values to other variables. It is intended for method chaining.
Example ¶
package main
import (
"fmt"
"github.com/shelepuginivan/color"
)
func main() {
silver := color.NewRGB(191, 191, 191)
fmt.Println(silver.Edit(func(c *color.RGB) {
c.R += 10
c.G += 20
c.B += 30
}))
}
Output: rgb(201, 211, 221)
func (RGB) HSL ¶
HSL returns HSL representation of color (hue, saturation, lightness).
Example ¶
package main
import (
"fmt"
"github.com/shelepuginivan/color"
)
func main() {
c := color.NewRGB(219, 188, 127)
fmt.Println(c.HSL())
}
Output: hsl(40, 56%, 68%)
func (RGB) Hex ¶
Hex returns hexadecimal representation of color.
Example ¶
package main
import (
"fmt"
"github.com/shelepuginivan/color"
)
func main() {
black := color.NewRGB(0, 0, 0)
fmt.Println(black.Hex())
}
Output: #000000
func (RGB) Lab ¶
Lab returns Lab representation of color (lightness, red-green, yellow-blue).
D65 is used as a reference white. Use XYZ.LabWithWhitepoint to specify a different whitepoint.
func (RGB) Lch ¶
Lch returns Lch representation of color (lightness, chroma, hue).
D65 is used as a reference white. Use XYZ.LabWithWhitepoint to specify a different whitepoint.
func (RGB) Oklab ¶ added in v0.2.0
Oklab returns Oklab representation of color (lightness, red-green, yellow-blue).
func (RGB) Oklch ¶ added in v0.2.0
Oklch returns Oklch representation of color (lightness, chroma, hue).
type XYZ ¶
type XYZ struct {
X float64 // X represents a combination of long wavelengths (red).
Y float64 // Y corresponds to the luminance or brightness of the color.
Z float64 // Z captures the short wavelengths (blue).
}
XYZ represents a color in XYZ color space.
func (*XYZ) Edit ¶
Edit allows in-place modification of the XYZ color instance using the provided editing function.
The returned value is a pointer to the same instance of XYZ, so it should not be used to assign values to other variables. It is intended for method chaining.
func (XYZ) Lab ¶
Lab returns Lab representation of color (lightness, red-green, yellow-blue).
D65 is used as a reference white. Use XYZ.LabWithWhitepoint to specify a different whitepoint.
func (XYZ) LabWithWhitepoint ¶
LabWithWhitepoint returns Lab representation of color, allowing to specify reference white color.
func (XYZ) Lch ¶
Lch returns Lch representation of color (lightness, chroma, hue).
D65 is used as a reference white. Use XYZ.LabWithWhitepoint to specify a different whitepoint.
func (XYZ) Oklab ¶ added in v0.2.0
Oklab returns Oklab representation of color (lightness, red-green, yellow-blue).
func (XYZ) Oklch ¶ added in v0.2.0
Oklch returns Oklch representation of color (lightness, chroma, hue).
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package gradient provides methods for creating color gradients.
|
Package gradient provides methods for creating color gradients. |
|
internal
|
|
|
integers
Package integers provides utilities for integer numbers.
|
Package integers provides utilities for integer numbers. |
|
interpolate
Package interpolate provides helper functions for gradient color interpolation.
|
Package interpolate provides helper functions for gradient color interpolation. |
|
percents
Package percents provides internal normalization functions for percents.
|
Package percents provides internal normalization functions for percents. |