-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhsv.go
157 lines (136 loc) · 3.4 KB
/
hsv.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package color
import (
"fmt"
"math"
"github.com/shelepuginivan/color/internal/degrees"
"github.com/shelepuginivan/color/internal/percents"
)
// [HSV] representation of color.
//
// [HSV]: https://en.wikipedia.org/wiki/HSL_and_HSV
type HSV struct {
H int // Hue (in degrees).
S int // Saturation (in percents).
V int // Value (in percents).
}
// NewHSV returns a new instance of [HSV].
func NewHSV(h, s, v int) *HSV {
return &HSV{
H: degrees.Normalize(h),
S: percents.Normalize(s),
V: percents.Normalize(v),
}
}
// CMYK returns [CMYK] representation of color (cyan, magenta, yellow, key).
func (c HSV) CMYK() *CMYK {
return c.RGB().CMYK()
}
// Hex returns hexadecimal representation of color.
func (c HSV) Hex() string {
rgb := c.RGB()
return fmt.Sprintf("#%02x%02x%02x", rgb.R, rgb.G, rgb.B)
}
// HSL returns [HSL] representation of color (hue, saturation, lightness).
func (c HSV) HSL() *HSL {
// Value normalization.
var (
s = percents.ToFloat(c.S)
v = percents.ToFloat(c.V)
)
// H_L = H_V
// S_L = 0 if L = 0 or L = 1, (V - L) / min(L, 1 - L) otherwise
// L = V(1 - S_V / 2)
var (
hue = c.H
saturation = 0.0
lightness = v * (1 - s/2)
)
if lightness != 0 && lightness != 1 {
saturation = (v - lightness) / min(lightness, 1-lightness)
}
return &HSL{
H: hue,
S: percents.FromFloat(saturation),
L: percents.FromFloat(lightness),
}
}
// HSV returns the color unchanged. This method is required to implement the
// [Color] interface.
func (c HSV) HSV() *HSV {
return &c
}
// RGB returns [RGB] representation of color (red, green, blue).
func (c HSV) RGB() *RGB {
var (
s = percents.ToFloat(c.S)
v = percents.ToFloat(c.V)
)
// Helper hue value that determines position on color wheel.
h := float64(c.H)
if h < 0 {
h += 360
} else if h >= 360 {
h = math.Mod(h, 360)
}
h /= 60
var (
chroma = s * v
rem = math.Mod(h, 2)
x = chroma * (1 - math.Abs(rem-1))
m = v - chroma
r, g, b float64
)
switch int(h) {
case 0:
r, g, b = chroma, x, 0
case 1:
r, g, b = x, chroma, 0
case 2:
r, g, b = 0, chroma, x
case 3:
r, g, b = 0, x, chroma
case 4:
r, g, b = x, 0, chroma
case 5:
r, g, b = chroma, 0, x
}
return &RGB{
R: uint8(math.Round((r + m) * 255)),
G: uint8(math.Round((g + m) * 255)),
B: uint8(math.Round((b + m) * 255)),
}
}
// XYZ returns [XYZ] representation of color (long wavelengths, brightness,
// short wavelengths).
func (c HSV) XYZ() *XYZ {
return c.RGB().XYZ()
}
// 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 (c HSV) Lab() *Lab {
return c.XYZ().Lab()
}
// 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 (c HSV) Lch() *Lch {
return c.XYZ().Lab().Lch()
}
// 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.
func (c *HSV) Edit(editfn func(c *HSV)) *HSV {
editfn(c)
return c
}
// String returns string representation of [HSV].
func (c HSV) String() string {
return fmt.Sprintf("hsv(%d, %d%%, %d%%)", c.H, c.S, c.V)
}