noire

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2021 License: MIT Imports: 4 Imported by: 17

README

Noire GoDoc Coverage Status Build Status Go Report Card

A color library which supports converting between the RGB, HSL, HSV, CMYK, Hex, HTML and some additional functions (tint, saturation).

Requires atleast Go 1.10 version due to the math.Round(https://golang.org/pkg/math/#Round) function call.

Supported colors

Noire is able to convert the colors between:

  • RGB
  • CMYK
  • HSL
  • HSV
  • Hex
  • HTML

Benchmark

Specification:
4.2 GHz Intel Core i7 (8750H)
32 GB 2666 MHz DDR4

goos: windows
goarch: amd64
pkg: github.com/teacat/noire
BenchmarkCMYKToRGB-12         	100000000	        22.5 ns/op	       0 B/op	       0 allocs/op
BenchmarkRGBToCMYK-12         	50000000	        26.9 ns/op	       0 B/op	       0 allocs/op
BenchmarkRGBToHSL-12          	50000000	        26.6 ns/op	       0 B/op	       0 allocs/op
BenchmarkHSLToRGB-12          	100000000	        17.2 ns/op	       0 B/op	       0 allocs/op
BenchmarkHSVToRGB-12          	100000000	        15.3 ns/op	       0 B/op	       0 allocs/op
BenchmarkRGBToHSV-12          	50000000	        29.3 ns/op	       0 B/op	       0 allocs/op
BenchmarkRGBToHex-12          	20000000	        85.4 ns/op	      32 B/op	       4 allocs/op
BenchmarkHexToRGB-12          	50000000	        36.1 ns/op	       8 B/op	       1 allocs/op
BenchmarkHTMLToRGBName-12     	20000000	       118 ns/op	      40 B/op	       3 allocs/op
BenchmarkHTMLToRGBHex-12      	30000000	        41.0 ns/op	       8 B/op	       1 allocs/op
BenchmarkRGBToHTML-12         	20000000	       103 ns/op	      32 B/op	       4 allocs/op
BenchmarkMix-12               	 5000000	       292 ns/op	     112 B/op	       5 allocs/op
BenchmarkHue-12               	50000000	        33.5 ns/op	       0 B/op	       0 allocs/op
BenchmarkSaturation-12        	50000000	        33.7 ns/op	       0 B/op	       0 allocs/op
BenchmarkLightness-12         	50000000	        33.7 ns/op	       0 B/op	       0 allocs/op
BenchmarkAdjustHue-12         	20000000	        79.0 ns/op	      32 B/op	       1 allocs/op
BenchmarkLighten-12           	20000000	        78.2 ns/op	      32 B/op	       1 allocs/op
BenchmarkDarken-12            	20000000	        77.8 ns/op	      32 B/op	       1 allocs/op
BenchmarkSaturate-12          	20000000	        74.1 ns/op	      32 B/op	       1 allocs/op
BenchmarkDesaturate-12        	20000000	        79.0 ns/op	      32 B/op	       1 allocs/op
BenchmarkGrayscale-12         	20000000	        71.8 ns/op	      32 B/op	       1 allocs/op
BenchmarkComplement-12        	20000000	        79.9 ns/op	      32 B/op	       1 allocs/op
BenchmarkTint-12              	30000000	        40.8 ns/op	      32 B/op	       1 allocs/op
BenchmarkShade-12             	30000000	        40.9 ns/op	      32 B/op	       1 allocs/op
BenchmarkInvert-12            	50000000	        24.9 ns/op	      32 B/op	       1 allocs/op
BenchmarkLuminanaceWCAG-12    	10000000	       224 ns/op	       0 B/op	       0 allocs/op
BenchmarkLuminanace-12        	300000000	         6.00 ns/op	       0 B/op	       0 allocs/op
BenchmarkContrast-12          	 5000000	       257 ns/op	       0 B/op	       0 allocs/op
BenchmarkIsLight-12           	2000000000	         0.26 ns/op	       0 B/op	       0 allocs/op
BenchmarkIsDark-12            	2000000000	         0.26 ns/op	       0 B/op	       0 allocs/op
BenchmarkHSV-12               	50000000	        34.2 ns/op	       0 B/op	       0 allocs/op
BenchmarkHSVA-12              	50000000	        35.0 ns/op	       0 B/op	       0 allocs/op
BenchmarkHSL-12               	50000000	        31.9 ns/op	       0 B/op	       0 allocs/op
BenchmarkHSLA-12              	50000000	        32.0 ns/op	       0 B/op	       0 allocs/op
BenchmarkRGB-12               	2000000000	         0.52 ns/op	       0 B/op	       0 allocs/op
BenchmarkRGBA-12              	2000000000	         0.26 ns/op	       0 B/op	       0 allocs/op
BenchmarkCMYK-12              	50000000	        32.9 ns/op	       0 B/op	       0 allocs/op
BenchmarkHex-12               	20000000	        90.1 ns/op	      32 B/op	       4 allocs/op
BenchmarkHTMLHex-12           	10000000	       146 ns/op	      40 B/op	       5 allocs/op
BenchmarkHTMLName-12          	20000000	       107 ns/op	      32 B/op	       4 allocs/op
BenchmarkHTMLRGBA-12          	 2000000	       839 ns/op	      96 B/op	       5 allocs/op
PASS
ok  	github.com/teacat/noire	67.640s
Success: Benchmarks passed.

Installation

To install Noire by simply typing go get in the terminal.

$ go get github.com/teacat/noire

Usage

Initialize a new color with noire.NewRGB (or NewHex) to modify the color with Lighten or Tint, etc.

package main

import (
	"fmt"

	"github.com/teacat/noire"
)

func main() {
	c := noire.NewRGB(255, 255, 255)
	fmt.Println(c.Invert().Hex())       // Output: 000000
	fmt.Println(c.Invert().HTML())      // Output: Black
	fmt.Println(c.Lighten(1).RGB())     // Output: 255, 255, 255
}

Description

There are few functions results cannot be visualized, so make sure to check the GoDoc to see how they work.

  • Hue: Get the Hue angle of the current color based on the HSL algorithm.
  • Saturation: Get the Saturation of the current color based on the HSL algorithm.
  • Lightness: Get the Lightness of the current color based on the HSL algorithm.
  • LuminanaceWCAG:Get the Luminance of the current color based on the WCAG 2.0 algorithm.
  • Luminanace: Get the Luminance of the current color.
  • Contrast: Get the Contrast of the current color based on the WCAG Luminance algorithm.
  • IsLight: Returns true if the color is a light scheme, it might not be the same as what human eyes can see.
  • IsDark: Returns true if the color is a dark scheme, it might not be the same as what human eyes can see.
Lighten

Result preview

Lighten a color based on HSL mode, it might makes the color a bit way too bright or washed out.

func main() {
	c := NewRGB(219, 112, 148)
	fmt.Println(c.Lighten(0.15).Hex())   // Output: EAADC2
}
Brighten

Result preview

Increases the brightness of the color based on RGB mode.

func main() {
	c := NewRGB(0, 0, 0)
	fmt.Println(c.Brighten(0.1).Hex())   // Output: 1A1A1A
}
Tint

Result preview

Mixing with a white color as base to get the best balance to increase the brightness of a color.

func main() {
	c := NewRGB(0, 0, 0)
	fmt.Println(c.Tint(0.1).Hex())   // Output: 1A1A1A
}
Darken

Result preview

Darken a color based on HSL mode, it might makes the color a bit way too dark or dimmed.

func main() {
	c := NewRGB(219, 112, 148)
	fmt.Println(c.Darken(0.15).Hex())   // Output: CB3366
}
Shade

Result preview

Mixing with a black color as base to get the best balance to increase the brightness of a color.

func main() {
	c := NewRGB(219, 112, 148)
	fmt.Println(c.Shade(0.15).Hex())   // Output: BA5F7E
}
Saturate

Result preview

Increases the saturation of the color based on HSL mode.

func main() {
	c := NewRGB(219, 112, 148)
	fmt.Println(c.Saturate(0.5).Hex())   // Output: FF4C88
}
Desaturate

Result preview

Decreases the saturation of the color based on HSL mode.

func main() {
	c := NewRGB(219, 112, 148)
	fmt.Println(c.Desaturate(0.15).Hex())   // Output: AE9DA3
}
AdjustHue

Result preview

Rotates the Hue angle of the color based on HSL mode, it still goes clockwise if the value was set over than 360 degree.

func main() {
	c := NewRGB(219, 112, 148)
	fmt.Println(c.AdjustHue(30).Hex())   // Output: DB8270
}
Mix

Result preview

Mixing two colors with a value that specified the weight of the second color.

func main() {
	c1 := NewHex("F00")
	c2 := NewHex("00F")
	fmt.Println(c1.Mix(c2, 0.5).HTML())   // Output: Purple
}
Invert

Result preview

Get the opposite color that based on the RGB color map (it's not a complementary color).

func main() {
	c := NewRGB(219, 112, 148)
	fmt.Println(c.Invert().Hex())   // Output: 248F6B
}
Complement

Result preview

Get the complementary color of the current color, same as AdjustHue(180).

func main() {
	c := NewRGB(219, 112, 148)
	fmt.Println(c.Complement().Hex())   // Output: 70DBB7
}
Grayscale

Result preview

Converts the color to grayscale, same as Desaturate(1).

func main() {
	c := NewRGB(219, 112, 148)
	fmt.Println(c.Grayscale().Hex())   // Output: A5A5A5
}
Foreground

Result preview

Get the suggested foreground color by calculating the color luminance, it returns a white color when the color is dark, vise versa.

func main() {
	c := NewHTML("Green")
	fmt.Println(c.Foreground().HTML())   // Output: White
	c = NewHTML("Red")
	fmt.Println(c.Foreground().HTML())   // Output: White
	c = NewHTML("Yellow")
	fmt.Println(c.Foreground().HTML())   // Output: Black
}

References

RGB 轉 HSV, HSL (線上色碼轉換 HSL, HSV, RGB, HEX)

ozdemirburak/iris: PHP library for color manipulation and conversion.

G17: Ensuring that a contrast ratio of at least 7:1 exists between text (and images of text) and background behind the text | Techniques for WCAG 2.0

Using Sass to automatically pick text colors

Relative luminance - Wikipedia

user interface - Given a background color, how to get a foreground color that makes it readable on that background color? - Stack Overflow

image - Formula to determine brightness of RGB color - Stack Overflow

Ant Design 色板生成算法演进之路 - 知乎

Sass 基础——颜色函数_Preprocessor, Sass, SCSS 教程_w3cplus

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CMYKToRGB

func CMYKToRGB(c float64, m float64, y float64, k float64) (r float64, g float64, b float64)

CMYKToRGB converts the color from CMYK to RGB with a lossy algorithm.

reference: https://www.ginifab.com.tw/tools/colors/js/colorconverter.js

func HSLToRGB

func HSLToRGB(h float64, s float64, l float64) (r float64, g float64, b float64)

HSLToRGB converts the color from HSL to RGB with a lossy algorithm.

reference: https://www.ginifab.com.tw/tools/colors/js/colorconverter.js

func HSVToRGB

func HSVToRGB(h float64, s float64, v float64) (r float64, g float64, b float64)

HSVToRGB converts the color from HSV to RGB with a lossy algorithm.

reference: https://www.rapidtables.com/convert/color/hsv-to-rgb.html

func HTMLToRGB

func HTMLToRGB(h string) (r float64, g float64, b float64)

HTMLToRGB converts the color from HTML color name or a Hex string (can be `#` prefixed or either a 3 characters shorthand) to RGB.

func HexToRGB

func HexToRGB(h string) (r float64, g float64, b float64)

HexToRGB converts the Hex string (can be `#` prefixed or either a 3 characters shorthand) to RGB.

func HueToRGB

func HueToRGB(p float64, q float64, t float64) float64

HueToRGB converts the color from Hue to RGB.

reference: https://www.ginifab.com.tw/tools/colors/js/colorconverter.js

func RGBToCMYK

func RGBToCMYK(r float64, g float64, b float64) (c float64, m float64, y float64, k float64)

RGBToCMYK converts the color from RGB to CMYK with a lossy algorithm.

reference: https://www.ginifab.com.tw/tools/colors/js/colorconverter.js

func RGBToHSL

func RGBToHSL(r float64, g float64, b float64) (h float64, s float64, l float64)

RGBToHSL converts the color from RGB to HSL with a lossy algorithm.

reference: https://www.ginifab.com.tw/tools/colors/js/colorconverter.js

func RGBToHSV

func RGBToHSV(r float64, g float64, b float64) (h float64, s float64, v float64)

RGBToHSV converts the color from RGB to HSV with a lossy algorithm.

reference: https://www.ginifab.com.tw/tools/colors/js/colorconverter.js

func RGBToHTML

func RGBToHTML(r float64, g float64, b float64) (h string)

RGBToHTML converts the color from RGB to a `#` prefixed Hex string if it doesn't have a HTML color name.

func RGBToHex

func RGBToHex(r float64, g float64, b float64) string

RGBToHex converts the color from RGB to a uppercased Hex string (without the `#` prefix).

Types

type Color

type Color struct {
	Red   float64
	Green float64
	Blue  float64
	Alpha float64
}

Color represents a manipulable color.

func NewCMYK

func NewCMYK(c float64, m float64, y float64, k float64) Color

NewCMYK initializes a color based on CMYK.

func NewCMYKA

func NewCMYKA(c float64, m float64, y float64, k float64, a float64) Color

NewCMYKA initializes a color based on CMYK with an alpha channel.

func NewHSL

func NewHSL(h float64, s float64, l float64) Color

NewHSL initializes a color based on HSL.

func NewHSLA

func NewHSLA(h float64, s float64, l float64, a float64) Color

NewHSLA initializes a color based on HSL with an alpha channel.

func NewHSV

func NewHSV(h float64, s float64, v float64) Color

NewHSV initializes a color based on HSV.

func NewHSVA

func NewHSVA(h float64, s float64, v float64, a float64) Color

NewHSVA initializes a color based on HSV with an alpha channel.

func NewHTML

func NewHTML(color string) Color

NewHTML initializes a color based on the HTML color name.

func NewHTMLA

func NewHTMLA(color string, a float64) Color

NewHTMLA initializes a color based on the HTML color name with an alpha channel.

func NewHex

func NewHex(color string) Color

NewHex initializes a color based on a Hex string.

func NewHexA

func NewHexA(color string, a float64) Color

NewHexA initializes a color based on a Hex string with an alpha channel.

func NewRGB

func NewRGB(r float64, g float64, b float64) Color

NewRGB initializes a color based on RGB.

func NewRGBA

func NewRGBA(r float64, g float64, b float64, a float64) Color

NewRGBA initializes a color based on RGB with an alpha channel.

func (Color) AdjustHue

func (c Color) AdjustHue(degrees float64) Color

AdjustHue rotates the Hue angle of the color based on HSL mode, it still goes clockwise if the value was set over than 360 degree.

func (Color) Brighten

func (c Color) Brighten(percent float64) Color

Brighten increases the brightness of the color. (`0.5` as `50%`)

reference: https://github.com/ozdemirburak/iris

func (Color) CMYK

func (c Color) CMYK() (float64, float64, float64, float64)

CMYK returns the CMYK value of the current color.

func (Color) Complement

func (c Color) Complement() Color

Complement returns the complementary color of the current color, same as `AdjustHue(180)`.

func (Color) Contrast

func (c Color) Contrast(color Color) float64

Contrast returns the Contrast of the current color based on the WCAG Luminance algorithm.

func (Color) Darken

func (c Color) Darken(percent float64) Color

Darken decreases the brightness of the color based on HSL mode. (`0.5` as `50%`)

func (Color) Desaturate

func (c Color) Desaturate(percent float64) Color

Desaturate decreases the saturation of the color based on HSL mode. (`0.5` as `50%`)

func (Color) Foreground

func (c Color) Foreground() Color

Foreground returns suggested foreground color by calculating the color luminance, it returns a white color when the color is dark, vise versa.

func (Color) Grayscale

func (c Color) Grayscale() Color

Grayscale converts the color to grayscale, same as `Desaturate(1)`.

func (Color) HSL

func (c Color) HSL() (float64, float64, float64)

HSL returns the HSL value of the current color.

func (Color) HSLA

func (c Color) HSLA() (float64, float64, float64, float64)

HSLA returns the HSLA value of the current color.

func (Color) HSV

func (c Color) HSV() (float64, float64, float64)

HSV returns the HSV value of the current color.

func (Color) HSVA

func (c Color) HSVA() (float64, float64, float64, float64)

HSVA returns the HSVA value of the current color.

func (Color) HTML

func (c Color) HTML() string

HTML returns a `#` prefixed Hex string or the HTML color name (like: `red`, `yellow`) if it had one. It returns a format with `rgba()` if the color comes with a alpha channel.

func (Color) Hex

func (c Color) Hex() string

Hex returns a Hex string of the current color. (Without the `#` prefix)

func (Color) Hue

func (c Color) Hue() float64

Hue returns the Hue angle of the current color based on the HSL algorithm.

func (Color) Invert

func (c Color) Invert() Color

Invert returns the opposite color that based on the RGB color map (it's not a complementary color).

func (Color) IsDark

func (c Color) IsDark() bool

IsDark returns true if the color is a dark scheme, it might not be the same as what human eyes can see.

func (Color) IsLight

func (c Color) IsLight() bool

IsLight returns true if the color is a light scheme, it might not be the same as what human eyes can see.

func (Color) Lighten

func (c Color) Lighten(percent float64) Color

Lighten increases the brightness of the color based on HSL mode. (`0.5` as `50%`)

func (Color) Lightness

func (c Color) Lightness() float64

Lightness returns the Lightness of the current color based on the HSL algorithm.

func (Color) LuminanaceWCAG

func (c Color) LuminanaceWCAG() float64

LuminanaceWCAG returns the Luminance of the the current color based on the WCAG 2.0 algorithm.

reference: https://www.w3.org/TR/WCAG20-TECHS/G17.html#G17-tests

reference: https://medium.com/dev-channel/using-sass-to-automatically-pick-text-colors-4ba7645d2796

func (Color) Mix

func (c Color) Mix(color Color, weight float64) Color

Mix mixs both color with the specified weight of the second color. (`0.5` as `50%`)

func (Color) RGB

func (c Color) RGB() (float64, float64, float64)

RGB returns the RGB value of the current color.

func (Color) RGBA

func (c Color) RGBA() (float64, float64, float64, float64)

RGBA returns the RGBA value of the current color.

func (Color) Saturate

func (c Color) Saturate(percent float64) Color

Saturate increases the saturation of the color based on HSL mode. (`0.5` as `50%`)

func (Color) Saturation

func (c Color) Saturation() float64

Saturation returns the percentage of the current color saturation based on the HSL algorithm.

func (Color) Shade

func (c Color) Shade(percent float64) Color

Shade decreases the brightness of the color while keeping the color tone, same as `Mix` with a black color. (`0.5` as `50%`)

func (Color) Tint

func (c Color) Tint(percent float64) Color

Tint increases the brightness of the color while keeping the color tone, same as `Mix` with a white color. (`0.5` as `50%`)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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