-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcolor.go
107 lines (97 loc) · 1.92 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
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
// Copyright (c) 2012 Guillermo Estrada. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
package blend
import (
"image/color"
"math"
)
type rgbaf64 struct {
r, g, b, a float64
}
func (c rgbaf64) RGBA() (uint32, uint32, uint32, uint32) {
r := float64ToUint16(c.r)
g := float64ToUint16(c.g)
b := float64ToUint16(c.b)
a := float64ToUint16(c.a)
return uint32(r), uint32(g), uint32(b), uint32(a)
}
type hslf64 struct {
h, s, l float64
}
func (c hslf64) RGBA() (uint32, uint32, uint32, uint32) {
return hsl2rgb(c.h, c.s, c.l).RGBA()
}
func color2rgbaf64(c color.Color) rgbaf64 {
r, g, b, a := c.RGBA()
return rgbaf64{float64(r), float64(g), float64(b), float64(a)}
}
func rgb2hsl(c color.Color) hslf64 {
var h, s, l float64
col := color2rgbaf64(c)
r, g, b := col.r/max, col.g/max, col.b/max
cmax := math.Max(math.Max(r, g), b)
cmin := math.Min(math.Min(r, g), b)
l = (cmax + cmin) / 2.0
if cmax == cmin {
// Achromatic.
h, s = 0.0, 0.0
} else {
// Chromatic.
delta := cmax - cmin
if l > 0.5 {
s = delta / (2.0 - cmax - cmin)
} else {
s = delta / (cmax + cmin)
}
switch cmax {
case r:
h = (g - b) / delta
if g < b {
h += 6.0
}
case g:
h = (b-r)/delta + 2.0
case b:
h = (r-g)/delta + 4.0
}
h /= 6.0
}
return hslf64{h, s, l}
}
func hsl2rgb(h, s, l float64) color.Color {
var r, g, b float64
if s == 0 {
r, g, b = l, l, l
} else {
var q float64
if l < 0.5 {
q = l * (1 + s)
} else {
q = l + s - s*l
}
p := 2*l - q
r = hue2rgb(p, q, h+1.0/3)
g = hue2rgb(p, q, h)
b = hue2rgb(p, q, h-1.0/3)
}
return rgbaf64{r*max + 0.5, g*max + 0.5, b*max + 0.5, max}
}
func hue2rgb(p, q, t float64) float64 {
if t < 0.0 {
t += 1.0
}
if t > 1.0 {
t -= 1.0
}
if t < 1.0/6.0 {
return p + (q-p)*6.0*t
}
if t < 0.5 {
return q
}
if t < 2.0/3.0 {
return p + (q-p)*(2.0/3.0-t)*6.0
}
return p
}