This repository has been archived by the owner on Oct 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
163 lines (153 loc) · 3.55 KB
/
config.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
158
159
160
161
162
163
package jdenticon
import (
"fmt"
"image/color"
"strconv"
)
// DefaultConfig var
var DefaultConfig = &Config{ // nolint:gochecknoglobals
Hues: -1,
Colored: Color{
Saturation: 0.5,
Lightness: []float64{0.4, 0.8},
},
Grayscale: Color{
Saturation: 0.0,
Lightness: []float64{0.3, 0.9},
},
Background: color.RGBA{0xff, 0xff, 0xff, 0x00},
Width: 200,
Height: 200,
Padding: 0.08,
}
type Config struct {
Hues int
Colored Color
Grayscale Color
Background color.Color
Width int
Height int
Padding float64
}
type Color struct {
Lightness []float64
Saturation float64
}
func (c Color) lightness(p float64) float64 {
if len(c.Lightness) == 0 {
return 0
}
if len(c.Lightness) == 1 || c.Lightness[0] == c.Lightness[1] || p == 0 {
return c.Lightness[0]
}
if p >= 1 {
return c.Lightness[1]
}
return c.Lightness[0] + c.Lightness[0]*p
}
func (c Color) color(hue float64, lightness float64) string {
return correctedHsl(hue, c.Saturation, c.lightness(lightness))
}
/*
864444000141320028501e5a
^^ R color
864444000141320028501e5a
^^ G color
864444000141320028501e5a
^^ B color
864444000141320028501e5a
^^ A color
864444000141320028501e5a
^ single hue
864444000141320028501e5a
^^^ hue
864444000141320028501e5a
^^ color saturation
864444000141320028501e5a
^^ gray saturation
864444000141320028501e5a
^^ color lightness 1
864444000141320028501e5a
^^ color lightness 2
864444000141320028501e5a
^^ gray lightness 1
864444000141320028501e5a
^^ gray lightness 2
*/
func ConfigFromString(h string) (c *Config, err error) {
if len(h) != 24 {
err = fmt.Errorf("invalid config length")
return
}
var (
hues int
R, G, B, A int64
colorSaturation int64
colorLightness1 int64
colorLightness2 int64
graySaturation int64
grayLightness1 int64
grayLightness2 int64
)
if R, err = strconv.ParseInt("0x"+h[0:2], 0, 64); err != nil {
return
}
if G, err = strconv.ParseInt("0x"+h[2:4], 0, 64); err != nil {
return
}
if B, err = strconv.ParseInt("0x"+h[4:6], 0, 64); err != nil {
return
}
if A, err = strconv.ParseInt("0x"+h[6:8], 0, 64); err != nil {
return
}
if colorSaturation, err = strconv.ParseInt("0x"+h[12:14], 0, 64); err != nil {
return
}
if graySaturation, err = strconv.ParseInt("0x"+h[14:16], 0, 64); err != nil {
return
}
if colorLightness1, err = strconv.ParseInt("0x"+h[16:18], 0, 64); err != nil {
return
}
if colorLightness2, err = strconv.ParseInt("0x"+h[18:20], 0, 64); err != nil {
return
}
if grayLightness1, err = strconv.ParseInt("0x"+h[20:22], 0, 64); err != nil {
return
}
if grayLightness2, err = strconv.ParseInt("0x"+h[22:24], 0, 64); err != nil {
return
}
if h[8] == '1' {
var hue int64
if hue, err = strconv.ParseInt("0x"+h[9:12], 0, 64); err != nil {
return nil, err
}
hues = int(hue) - 1
if hues > 360 {
hues = 360
}
} else {
hues = -1
}
c = &Config{
Hues: hues,
Colored: Color{
Saturation: float64(colorSaturation) / 100,
Lightness: []float64{float64(colorLightness1) / 100, float64(colorLightness2) / 100},
},
Grayscale: Color{
Saturation: float64(graySaturation) / 100,
Lightness: []float64{float64(grayLightness1) / 100, float64(grayLightness2) / 100},
},
Background: color.RGBA{uint8(R), uint8(G), uint8(B), uint8(A)},
Width: 200,
Height: 200,
Padding: 0.08,
}
return c, nil
}
func ConfigFromBytes(h []byte) (c *Config, err error) {
return ConfigFromString(string(h))
}