-
Notifications
You must be signed in to change notification settings - Fork 0
/
color.go
69 lines (61 loc) · 2.42 KB
/
color.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package blink1
import (
"image/color"
"strings"
)
// GetColorNames returns the color names from the preset color map.
func GetColorNames() []string {
// copy name slice
cls := make([]string, len(colorNames))
copy(cls, colorNames)
return cls
}
// GetColorByName returns the color corresponding to the given name from the preset color map.
// If the color is found, it returns the color and true, otherwise it returns nil and false.
func GetColorByName(name string) (cl color.Color, found bool) {
n := strings.TrimSpace(strings.ToLower(name))
cl, found = presetColorMap[n]
return
}
// GetNameByColor returns the name corresponding to the given color from the preset color map.
// If the color is found, it returns the name and true.
// If the color is not found, it returns the hex string and false.
func GetNameByColor(cl color.Color) (name string, found bool) {
// check if color is in map
if name, ok := hexNameMap[convColorToHex(cl)]; ok {
return name, true
}
return convColorToHex(cl), false
}
// GetNameOrHexByColor returns the name corresponding to the given color from the preset color map, or the hex string if the color is not found.
func GetNameOrHexByColor(cl color.Color) string {
name, _ := GetNameByColor(cl)
return name
}
// RandomColor returns a bright random color.
func RandomColor() color.Color {
// helper function to get a random float64
rand := func(mul float64) float64 {
f, _ := getRandomFloat(1 << 16)
return f * mul
}
// hue between 0 and 360 to get a full range of colors
hue := rand(360)
// saturation between 50 and 100 to ensure a bright color
saturation := 50. + rand(50)
// max brightness for a bright color
brightness := 90. + rand(10)
// convert to RGB and return
return convRGBToColor(convHSBToRGB(hue, saturation, brightness))
}
// DecodeGammaColor operates gamma correction on the given color and returns the decoded color.
// The gamma correction is automatically applied for state and pattern while playing or writing.
// This is the inverse function of EncodeGammaColor.
func DecodeGammaColor(cl color.Color) color.Color {
return convRGBToColor(degammaRGB(convColorToRGB(cl)))
}
// EncodeGammaColor operates gamma correction on the given color and returns the encoded color.
// Encoding a decoded color may not return the original color, but it will be the same color when decoded again.
func EncodeGammaColor(cl color.Color) color.Color {
return convRGBToColor(engammaRGB(convColorToRGB(cl)))
}