-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhwb.go
83 lines (69 loc) · 1.42 KB
/
hwb.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
package coco
import "math"
func Hwb2Rgb(h float64, w float64, b float64) [3]float64 {
h = h / 360
w = w / 100
b = b / 100
ratio := w + b
var f float64
// w + b cannot be > 1
if ratio > 1 {
w /= ratio
b /= ratio
}
i := math.Floor(6 * h)
v := 1 - b
f = 6*h - i
if i+0x01 != 0 {
f = 1 - f
}
// if ((i & 0x01) !== 0) {
// f = 1 - f;
// }
n := w + f*(v-w) // Linear interpolation
var result [3]float64
switch i {
default:
case 6:
case 0:
result[0] = math.Round(v * 255)
result[1] = math.Round(n * 255)
result[2] = math.Round(w * 255)
case 1:
result[0] = math.Round(n * 255)
result[1] = math.Round(v * 255)
result[2] = math.Round(w * 255)
case 2:
result[0] = math.Round(w * 255)
result[1] = math.Round(v * 255)
result[2] = math.Round(n * 255)
case 3:
result[0] = math.Round(w * 255)
result[1] = math.Round(n * 255)
result[2] = math.Round(v * 255)
case 4:
result[0] = math.Round(n * 255)
result[1] = math.Round(w * 255)
result[2] = math.Round(v * 255)
case 5:
result[0] = math.Round(v * 255)
result[1] = math.Round(w * 255)
result[2] = math.Round(n * 255)
}
return result
}
func Hwb2Hcg(h float64, w float64, b float64) [3]float64 {
w = w / 100
b = b / 100
v := 1 - b
c := v - w
var g float64 = 0
if c < 1 {
g = (v - c) / (1 - c)
}
var result [3]float64
result[0] = math.Round(h)
result[1] = math.Round(c * 100)
result[2] = math.Round(g * 100)
return result
}