-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-json.go
171 lines (141 loc) · 3.34 KB
/
generate-json.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
164
165
166
167
168
169
170
171
package main
import (
//"fmt"
"math"
"strconv"
"encoding/json"
"io/ioutil"
"net/http"
"sort"
"gopkg.in/yaml.v2"
)
type Color struct {
Hex string
R float64
G float64
B float64
Hue float64
}
type Language struct {
Name string
Color Color
}
// Sorting implementation for language list
type Languages []Language
func (slice Languages) Len() int {
return len(slice)
}
func (slice Languages) Less(i, j int) bool {
return slice[i].Color.Hue < slice[j].Color.Hue
}
func (slice Languages) Swap(i, j int) {
slice[i], slice[j] = slice[j], slice[i]
}
func check(e error) {
if e != nil {
panic(e)
}
}
func ternaryMax(a float64, b float64, c float64) float64 {
if (a > b) && (a > c) {
return a
} else if (b > a) && (b > c) {
return b
} else {
return c
}
}
func ternaryMin(a float64, b float64, c float64) float64 {
if (a < b) && (a < c) {
return a
} else if (b < a) && (b < c) {
return b
} else {
return c
}
}
func hexToRGB(str string) (float64, float64, float64) {
var r, g, b int64
if(len(str) == 4){
r, _ = strconv.ParseInt(str[1:3], 16, 0)
g, _ = strconv.ParseInt(str[1:3], 16, 0)
b, _ = strconv.ParseInt(str[1:3], 16, 0)
} else {
r, _ = strconv.ParseInt(str[1:3], 16, 0)
g, _ = strconv.ParseInt(str[3:5], 16, 0)
b, _ = strconv.ParseInt(str[5:7], 16, 0)
}
return float64(r), float64(g), float64(b)
}
func hueFromRGB(r float64, g float64, b float64) float64 {
r = r / 255
g = g / 255
b = b / 255
max := ternaryMax(r, g, b)
min := ternaryMin(r, g, b)
delta := max - min
if (min == 0) {
return float64(0)
}
if ((max - min) == 0) {
return float64(0)
}
var hue float64
if (max == r) {
hue = 60 * math.Mod(((g - b) / delta), float64(6))
} else if (max == g) {
hue = 60 * ((2.0 + (b - r)) / delta)
} else {
hue = 60 * (4.0 + ((r - g) / delta))
}
return hue
}
func createColor(str string) Color {
r, g, b := hexToRGB(str)
hue := hueFromRGB(r, g, b)
c := Color{Hex: str, R: r, G: g, B: b, Hue: hue}
return c
}
func createLangStruct(name string, hex string) Language {
color := createColor(hex)
l := Language{Name: name, Color: color}
return l
}
type YamlResponse struct {
}
func main() {
// Get YAML representing all languages
response, err := http.Get("https://raw.githubusercontent.com/github/linguist/master/lib/linguist/languages.yml")
check(err)
defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body)
check(err)
unmarshalled_yaml := []byte(contents)
// Parse YAML into map representation
yamlMap := make(map[string]map[string]interface{})
err = yaml.Unmarshal(unmarshalled_yaml, &yamlMap)
check(err)
// Convert map to JSON
colorMap := make(map[string] Language)
for k, v := range yamlMap {
if val, ok := v["color"]; ok { // color exists
if str, ok := val.(string); ok { // string type check (required)
colorMap[k] = createLangStruct(k, str)
}
}
}
languages := make(Languages, 0, len(colorMap))
for k := range colorMap {
languages = append(languages, colorMap[k])
}
sort.Sort(languages)
colorJSON, err := json.MarshalIndent(languages, "", " ")
check(err)
allJSON, err := json.MarshalIndent(yamlMap, "", " ")
check(err)
// Write to file
err = ioutil.WriteFile("color-info.json", []byte("color_data = " + string(colorJSON)), 0644)
check(err)
err = ioutil.WriteFile("all-info.json", []byte("all_data = " + string(allJSON)), 0644)
check(err)
}