Created
May 14, 2021 10:51
-
-
Save AliLogic/cf07e470aaf0f5a5dd08ee7cd567ed80 to your computer and use it in GitHub Desktop.
Gets the closest game vehicle color ID from hex (made for UGMP)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Credits to https://github.com/lucasb-eyer/go-colorful for the RGB color distance functions | |
// Credits to https://gist.github.com/CraigChilds94/6514edbc6a2db5e434a245487c525c75 for the Text to Hex conversion | |
package main | |
import ( | |
"bufio" | |
"fmt" | |
"math" | |
"os" | |
"strconv" | |
) | |
type Hex string | |
type RGB struct { | |
Red, Green, Blue uint8 | |
} | |
type Color struct { | |
Red, Green, Blue float64 | |
} | |
func (h Hex) Convert() (RGB, error) { | |
return Hex2RGB(h) | |
} | |
func Hex2RGB(hex Hex) (RGB, error) { | |
var rgb RGB | |
values, err := strconv.ParseUint(string(hex), 16, 32) | |
if err != nil { | |
return RGB{}, err | |
} | |
rgb = RGB{ | |
Red: uint8(values >> 16), | |
Green: uint8((values >> 8) & 0xFF), | |
Blue: uint8(values & 0xFF), | |
} | |
return rgb, nil | |
} | |
func sq(v float64) float64 { | |
return v * v | |
} | |
func (c1 Color) DistanceLinearRgb(c2 Color) float64 { | |
r1, g1, b1 := c1.LinearRgb() | |
r2, g2, b2 := c2.LinearRgb() | |
return math.Sqrt(sq(r1-r2) + sq(g1-g2) + sq(b1-b2)) | |
} | |
func (col Color) LinearRgb() (r, g, b float64) { | |
r = linearize(col.Red) | |
g = linearize(col.Green) | |
b = linearize(col.Blue) | |
return | |
} | |
func linearize(v float64) float64 { | |
if v <= 0.04045 { | |
return v / 12.92 | |
} | |
return math.Pow((v+0.055)/1.055, 2.4) | |
} | |
func main() { | |
vehColors := map[int]string{ | |
0: "000000", | |
1: "f5f5f5", | |
2: "2a77a1", | |
3: "840410", | |
4: "263739", | |
5: "86446e", | |
6: "d78e10", | |
7: "4c75b7", | |
8: "bdbec6", | |
9: "5e7072", | |
10: "46597a", | |
11: "656a79", | |
12: "5d7e8d", | |
13: "58595a", | |
14: "d6dad6", | |
15: "9ca1a3", | |
16: "335f3f", | |
17: "730e1a", | |
18: "7b0a2a", | |
19: "9f9d94", | |
20: "3b4e78", | |
21: "732e3e", | |
22: "691e3b", | |
23: "96918c", | |
25: "3f3e45", | |
26: "a5a9a7", | |
27: "635c5a", | |
28: "3d4a68", | |
29: "979592", | |
30: "421f21", | |
31: "5f272b", | |
32: "8494ab", | |
33: "767b7c", | |
35: "5a5752", | |
36: "252527", | |
37: "2d3a35", | |
38: "93a396", | |
39: "6d7a88", | |
40: "221918", | |
41: "6f675f", | |
42: "7c1c2a", | |
43: "5f0a15", | |
44: "193826", | |
45: "5d1b20", | |
46: "9d9872", | |
47: "7a7560", | |
48: "989586", | |
49: "adb0b0", | |
50: "848988", | |
51: "304f45", | |
52: "4d6268", | |
53: "162248", | |
54: "272f4b", | |
55: "7d6256", | |
56: "9ea4ab", | |
57: "9c8d71", | |
58: "6d1822", | |
59: "4e6881", | |
60: "9c9c98", | |
61: "917347", | |
62: "661c26", | |
63: "949d9f", | |
64: "a4a7a5", | |
65: "8e8c46", | |
66: "341a1e", | |
67: "6a7a8c", | |
68: "aaad8e", | |
69: "ab988f", | |
70: "851f2e", | |
71: "6f8297", | |
75: "20202c", | |
76: "a4a096", | |
77: "aa9d84", | |
78: "78222b", | |
79: "0e316d", | |
80: "722a3f", | |
81: "7b715e", | |
82: "741d28", | |
83: "1e2e32", | |
84: "4d322f", | |
85: "7c1b44", | |
86: "2e5b20", | |
87: "395a83", | |
88: "6d2837", | |
89: "a7a28f", | |
90: "afb1b1", | |
91: "364155", | |
92: "6d6c6e", | |
93: "0f6a89", | |
94: "204b6b", | |
95: "2b3e57", | |
96: "9b9f9d", | |
97: "6c8495", | |
98: "4d6000", | |
99: "ae9b7f", | |
100: "406c8f", | |
101: "1f253b", | |
102: "ab9276", | |
103: "134573", | |
104: "96816c", | |
105: "64686a", | |
106: "105082", | |
107: "a19983", | |
108: "385694", | |
109: "525661", | |
110: "7f6956", | |
111: "8c929a", | |
112: "596e87", | |
113: "473532", | |
114: "44624f", | |
115: "730a27", | |
116: "223457", | |
117: "640d1b", | |
118: "a3adc6", | |
119: "695853", | |
120: "9b8b80", | |
121: "620b1c", | |
122: "5b5d5e", | |
123: "624428", | |
124: "731827", | |
125: "1b376d", | |
126: "ec6aae", | |
127: "bc0f0f", | |
128: "662b2b", | |
130: "3e4154", | |
131: "454545", | |
132: "a11717", | |
133: "110d1d", | |
134: "8c939a", | |
135: "a1a1a1", | |
136: "842827", | |
138: "667292", | |
139: "2e2e2e", | |
140: "640d1a", | |
141: "737373", | |
143: "371002", | |
144: "2d683e", | |
145: "1b1b1b", | |
146: "d57433", | |
147: "141414", | |
148: "9f2020", | |
149: "ff9800", | |
154: "b87c26", | |
155: "d35733", | |
164: "8c7b6b", | |
165: "515459", | |
166: "585853", | |
167: "646464", | |
170: "601a23", | |
172: "3b3b4b", | |
175: "9e94a9", | |
176: "050505", | |
179: "b3363a", | |
182: "f3ed47", | |
186: "352224", | |
187: "5a2124", | |
189: "63322e", | |
191: "8a3a42", | |
192: "682731", | |
193: "8b3c44", | |
194: "9e2f2b", | |
195: "a33a2f", | |
196: "d25633", | |
197: "925635", | |
198: "f4723a", | |
200: "e25a59", | |
201: "772a25", | |
202: "e17743", | |
203: "c44636", | |
204: "e17844", | |
205: "c35938", | |
206: "464840", | |
207: "747761", | |
208: "757763", | |
209: "918a3d", | |
210: "948c66", | |
211: "998d79", | |
212: "d8a534", | |
213: "c9bd7d", | |
214: "c9c591", | |
215: "d4c84e", | |
216: "1a332e", | |
217: "242f2b", | |
218: "1d373f", | |
219: "3c4a3b", | |
220: "2d5037", | |
221: "3a6c60", | |
222: "3a623c", | |
223: "7ca282", | |
224: "4c524e", | |
225: "56775b", | |
226: "1c4650", | |
227: "485e84", | |
228: "1c2745", | |
229: "1f3468", | |
230: "2b4878", | |
231: "475c83", | |
232: "447c92", | |
233: "3d67ab", | |
234: "4b7d82", | |
235: "80b0b7", | |
236: "3d2333", | |
237: "1c2948", | |
238: "343941", | |
239: "40454c", | |
240: "4a2d2b", | |
241: "563e33", | |
242: "41464c", | |
243: "672731", | |
244: "835a75", | |
245: "868587", | |
246: "171717", | |
249: "5c5c5c", | |
251: "8a8a8a", | |
253: "b8b8b8", | |
254: "cfcfcf", | |
255: "e6e6e6", | |
256: "aaafaa", | |
257: "6a736b", | |
258: "ffef64", | |
259: "9aa790", | |
262: "6877dc", | |
263: "dc5056", | |
264: "4a7288", | |
265: "dfcb62", | |
} | |
var main RGB | |
var err error | |
var index int | |
fmt.Printf("-------------------\n") | |
fmt.Printf("Insert color hex (RRGGBB)\n") | |
scanner := bufio.NewScanner(os.Stdin) | |
for scanner.Scan() { | |
distance := 99999.0 | |
index = 0 | |
text := scanner.Text() | |
var hex Hex = Hex(text) | |
main, err = hex.Convert() | |
if err != nil { | |
fmt.Printf("(1) Couldn't convert hex to rgb\n") | |
} | |
for k := 1; k < len(vehColors); k++ { | |
if _, ok := vehColors[k]; !ok { | |
continue | |
} | |
var rgb RGB | |
c1 := Color{float64(main.Red) / 255.0, float64(main.Green) / 255.0, float64(main.Blue) / 255.0} | |
hex = Hex(vehColors[k]) | |
rgb, err = hex.Convert() | |
if err != nil { | |
fmt.Printf("(2) Couldn't convert hex to rgb\n") | |
} | |
c2 := Color{float64(rgb.Red) / 255.0, float64(rgb.Green) / 255.0, float64(rgb.Blue) / 255.0} | |
tempDistance := c2.DistanceLinearRgb(c1) * 100.0 | |
if tempDistance <= distance { | |
distance = tempDistance | |
index = k | |
} | |
} | |
fmt.Printf("%s (%d) is the closest color to %v\n", vehColors[index], index, text) | |
fmt.Printf("-------------------\n") | |
fmt.Printf("Insert color hex (RRGGBB)\n") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment