This repository was archived by the owner on Nov 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgen-colors-h.go
More file actions
95 lines (81 loc) · 1.75 KB
/
gen-colors-h.go
File metadata and controls
95 lines (81 loc) · 1.75 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
//go:build ignore
// +build ignore
// reads names.go (var ColorNames) and generates colors.h
package main
import (
"fmt"
"io/ioutil"
"log"
"strconv"
"strings"
"github.com/ilius/termcolor"
"github.com/lucasb-eyer/go-colorful"
)
func uint8to32(x uint8) uint32 {
return uint32(float64(x) * 257.0)
}
type simpleRGB [3]uint8
func (c simpleRGB) RGBA() (r, g, b, a uint32) {
return uint8to32(c[0]), uint8to32(c[1]), uint8to32(c[2]), 0xffff
}
func formatFloat(f float64) string {
return strconv.FormatFloat(f, 'f', -1, 64)
}
func formatHSL(h, s, l float64) string {
return fmt.Sprintf(
"{%s, %s, %s}",
formatFloat(h),
formatFloat(s),
formatFloat(l),
)
}
func main() {
cCode := `#include <stdio.h>
#include <stdint.h>
typedef struct ColorProp {
uint8_t code;
uint8_t rgba[4];
double hsl[3];
char *hex;
char *names; // separated by ","
uint8_t nameCount;
} ColorProp;
ColorProp colors[] = {
`
for code, names := range termcolor.ColorNames {
red, green, blue := termcolor.CodeToRGB(uint8(code))
cf, ok := colorful.MakeColor(simpleRGB{red, green, blue})
if !ok {
panic("failed to make color")
}
H, S, L := cf.Hsl()
htmlColor := termcolor.RGBToHexColor(red, green, blue)
for _, name := range names {
if strings.Contains(name, ",") {
log.Fatalf("invalid name = %#v\n", name)
}
}
cCode += fmt.Sprintf(
"\t{\n"+
"\t\t.code = %d,\n"+
"\t\t.rgba = {%d, %d, %d, 255},\n"+
"\t\t.hsl = "+formatHSL(H, S, L)+",\n"+
"\t\t.hex = %#v,\n"+
"\t\t.names = %#v,\n"+
"\t\t.nameCount = %d,\n"+
"\t},\n",
code,
red, green, blue,
htmlColor,
strings.Join(names, ","),
len(names),
)
}
cCode += "};"
{
err := ioutil.WriteFile("colors.h", []byte(cCode), 0644)
if err != nil {
panic(err)
}
}
}