-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
359 lines (294 loc) · 8.31 KB
/
Copy pathmain.go
File metadata and controls
359 lines (294 loc) · 8.31 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"math"
"math/rand"
"os"
"time"
)
type colorName struct {
name string
}
type config struct {
// Three main colors
Primary string `json:"primary"`
Secondary string `json:"secondary"`
Tertiary string `json:"tertiary"`
// Message colors
Good string `json:"good"`
Caution string `json:"caution"`
Alarm string `json:"alarm"`
DefaultGood []int `json:"rgbgood"` // RGB value
DefaultCaution []int `json:"rgbcaution"` // RGB value
DefaultAlarm []int `json:"rgbalarm"` // RGB value
// Lucents are color with opacity
Lucent string `json:"lucent"`
// Shades are shades of gray.
Shades string `json:"shades"`
// The "Y" spread operator
Yspread int `json:"yspread"`
}
type rgb struct {
red float64
green float64
blue float64
}
type hsl struct {
hue float64
saturation float64
luminance float64
}
func calcrgb(f1, f2, t float64) float64 {
if 6*t < 1.0 {
return f2 + (f1-f2)*6*t
}
if 2*t < 1.0 {
return f1
}
if 3*t < 2.0 {
return f2 + (f1-f2)*(0.666-t)*6
}
return f2
}
func hslToRgb(c hsl) rgb {
var newrgb rgb
//
// W A R N I N G
// THIS FUNCTION "hslToRgb" IS NOT WORKING RIGHT!!!!
// Given a HUE higher than say 100 or 180 not sure, but all goes to hell.
// will remove comment when fixed.
// First check saturation,
// Zero saturation means that hue is irrelevant, it's gray.
// (AKA: achromatic)
//For example H = 0, S = 0 and L = 40%, so L/100 we get 0.4,
// 0.4 * 255 = 102, so R = 102, G = 102 and B = 102
if c.saturation == 0 {
newrgb.red = ((c.luminance / 100) * 255)
newrgb.green = ((c.luminance / 100) * 255)
newrgb.blue = ((c.luminance / 100) * 255)
return newrgb
}
var formula1 float64 = 0
c.luminance = c.luminance / 100
c.saturation = c.saturation / 100
if c.luminance < 0.5 {
formula1 = c.luminance * (1 + c.saturation)
} else {
formula1 = (c.luminance + c.saturation) - (c.luminance * c.saturation)
}
var formula2 float64 = 2*c.luminance - formula1
c.hue = c.hue / 360
newrgb.red = calcrgb(formula1, formula2, c.hue+0.333) * 255
newrgb.green = calcrgb(formula1, formula2, c.hue) * 255
newrgb.blue = calcrgb(formula1, formula2, c.hue-0.333) * 255
return newrgb
}
func rgbToHsl(from rgb) hsl {
from.red = from.red / 255
from.green = from.green / 255
from.blue = from.blue / 255
min := math.Min(from.red, math.Min(from.green, from.blue))
max := math.Max(from.red, math.Max(from.green, from.blue))
var hue float64 = 0.0
var saturation = 0.0
// set luminance
var luminance = (max + min) / 2
var delta = (max - min)
if max == min { // it's gray, no hue or saturation
hue = 0
saturation = 0
} else {
if luminance > 0.5 {
saturation = (delta / (2 - max - min))
} else {
saturation = delta / (max + min)
}
switch max {
case from.red:
hue = (from.green - from.blue) / delta
case from.green:
hue = (from.blue-from.red)/delta + 2
case from.blue:
hue = (from.red-from.green)/delta + 4
}
hue = math.Round(hue * 60)
saturation = (saturation * 100)
luminance = (luminance * 100)
}
if hue < 0 {
hue = hue + 360
}
if hue > 360 {
hue = hue - 360
}
if saturation > 100 {
saturation = 100
}
if luminance > 100 {
luminance = 100
}
return hsl{hue, saturation, luminance}
}
func outputHSL(name string, i int, c hsl) {
fmt.Printf(" --%s%02d: hsl(%v, %v%%, %v%%); \n", name, i,
int(math.Round(c.hue)), int(math.Round(c.saturation)), int(math.Round(c.luminance)))
}
func outputHSLA(name string, i int, c hsl, op float64) {
fmt.Printf(" --%s%02d: hsl(%v, %v%%, %v%%, %1.1f); \n", name, i,
int(math.Round(c.hue)), int(math.Round(c.saturation)), int(math.Round(c.luminance)), op)
}
func outputRGB(name string, c rgb) {
fmt.Printf(" --%s: #%02x%02x%02x;\n", name,
int(math.Round(c.red)), int(math.Round(c.green)), int(math.Round(c.blue)))
}
func randRGB() rgb {
rand.Seed(time.Now().Unix())
x := float64(rand.Intn(10))
rand.Seed(time.Now().Unix())
green := float64(rand.Intn(255))
blue := float64(rand.Intn(255))
rand.Seed(time.Now().Unix())
red2 := float64(rand.Intn(245))
x = x + red2
return rgb{x, green, blue}
}
func newHsl(c hsl, delta int) hsl {
newColor := hsl{c.hue, c.saturation, c.luminance}
newColor.hue = float64(delta) + newColor.hue
if newColor.hue < 0 {
newColor.hue = newColor.hue + 360
}
if newColor.hue > 360 {
newColor.hue = newColor.hue - 360
}
return newColor
}
func outputShades(color hsl, name, prefix, lucentname string) {
// 12 Shades of a color
x := 20
for i := 0; i < 12; i++ {
color.luminance = float64(x)
outputHSL(name, i+1, color)
x = x + 6
}
// 3 Lumens of a color
color.luminance = float64(50)
for i := 0; i < 3; i++ {
x := float64((float64(i) + float64(1.0)) / float64(10))
outputHSLA(prefix+lucentname, i+1, color, x)
}
}
func outputGrays(grayname, lucentname string) {
// 12 Shades of Gray
for i := 0; i < 9; i++ {
x := (i + 1) * 12
if x > 100 {
x = 100
}
outputHSL(grayname, i+1, hsl{float64(0), float64(0), float64(x)})
}
// 8 Lumens of Gray
for i := 0; i < 8; i++ {
x := float64((float64(i) + float64(1.0)) / float64(8))
outputHSLA(lucentname, i+1, hsl{float64(0), float64(0), float64(50)}, x)
}
}
func getRgb() rgb {
rand.Seed(time.Now().Unix())
// variables declaration
var red int
var green int
var blue int
// flags declaration using flag package
flag.IntVar(&red, "red", -1, "Specify RGB red value, default is 255")
flag.IntVar(&green, "green", -1, "Specify RGB green value, default is 255")
flag.IntVar(&blue, "blue", -1, "Specify RGB blue value, default is 255")
flag.Parse() // after declaring flags we need to call it
randomRGB := randRGB()
//fmt.Printf("%v %v %v\n", randomRGB.red, randomRGB.green, randomRGB.blue)
// check if cli params match
if red > 255 || red < 0 {
fmt.Printf("/* red generated randomly. */\n")
red = int(randomRGB.red)
}
if green > 255 || green < 0 {
fmt.Printf("/* green generated randomly. */\n")
green = int(randomRGB.green)
}
if blue > 255 || blue < 0 {
fmt.Printf("/* blue generated randomly.*/ \n")
blue = int(randomRGB.blue)
}
return newRgb(red, green, blue)
}
func newRgb(r int, g int, b int) rgb {
return rgb{float64(r), float64(g), float64(b)}
}
func readConf() (config, error) {
// json data
var obj config
// read file
data, err := ioutil.ReadFile("./rootcss.json")
if err != nil {
fmt.Print(err)
return obj, err
}
// Unmarshal json.
err = json.Unmarshal(data, &obj)
if err != nil {
fmt.Println("error:", err)
return obj, err
}
return obj, nil
}
func main() {
cfg, err := readConf()
if err != nil {
fmt.Println("ERROR: ", err.Error())
os.Exit(1)
}
fmt.Println(":root {")
fmt.Println(" --round-radius: 170960px;")
// Every design must have some gray-scale
outputGrays(cfg.Shades, cfg.Lucent)
// This is the primary color, randomly generated or from the cmd line.
rgb := getRgb()
outputRGB("basecolor", rgb)
// Convert to HSL, easier to manipulate.
primary1 := rgbToHsl(rgb)
outputShades(primary1, cfg.Primary, cfg.Primary[0:3], cfg.Lucent)
// Create a secondary color by altering the primary hue 30 degrees.
secondary := newHsl(primary1, cfg.Yspread)
outputShades(secondary, cfg.Secondary, cfg.Secondary[0:3], cfg.Lucent)
// Create a tertiary color by altering the primary hue.
// Alter the hue by a number of degrees, base on the "Y" spread operator.
degrees := (cfg.Yspread / 2) + 180
tertiary := newHsl(primary1, degrees)
outputShades(tertiary, cfg.Tertiary, cfg.Tertiary[0:3], cfg.Lucent)
// The following colors are for messaging:
// > Good messages
// > Caution messages
// > Alarm messages.
// I decided on using traffic light colors, you may choose other colors.
// To conform better,
// message colors will have the same saturation as the primary color.
good := rgbToHsl(newRgb(cfg.DefaultGood[0],
cfg.DefaultGood[1],
cfg.DefaultGood[2]))
good.saturation = primary1.saturation
outputShades(good, cfg.Good, cfg.Good[0:3], cfg.Lucent)
caution := rgbToHsl(newRgb(cfg.DefaultCaution[0],
cfg.DefaultCaution[1],
cfg.DefaultCaution[2]))
caution.saturation = primary1.saturation
outputShades(caution, cfg.Caution, cfg.Caution[0:3], cfg.Lucent)
alarm := rgbToHsl(newRgb(cfg.DefaultAlarm[0],
cfg.DefaultAlarm[1],
cfg.DefaultAlarm[2]))
alarm.saturation = primary1.saturation
outputShades(alarm, cfg.Alarm, cfg.Alarm[0:3], cfg.Lucent)
fmt.Println("}")
}