naev 0.12.5
colours_c_gen.py
1#!/usr/bin/env python3
2
3def gammaToLinear(x):
4 if x <= 0.04045:
5 return x / 12.92
6 return pow((x + 0.055) / 1.055, 2.4)
7
8num_colours = 0
9class Colour:
10 def __init__(self, name, r, g, b, a=1.0):
11 global num_colours
12 num_colours += 1
13 self.name = name
14 self.r = gammaToLinear( r )
15 self.g = gammaToLinear( g )
16 self.b = gammaToLinear( b )
17 self.a = a
18
19 def write_header(self, f):
20 f.write(f"extern const glColour c{self.name};\n")
21
22 def write_source(self, f):
23 f.write(f"const glColour c{self.name} = {{ .r={self.r}, .g={self.g}, .b={self.b}, .a={self.a} }};\n")
24
25 def __lt__(self, other):
26 return self.name < other.name
27
28
29COLOURS = [
30 Colour( "Transparent", 0.00, 0.00, 0.00, 0.00 ),
31 Colour( "White", 1.00, 1.00, 1.00 ),
32 Colour( "Grey90", 0.90, 0.90, 0.90 ),
33 Colour( "Grey80", 0.80, 0.80, 0.80 ),
34 Colour( "Grey70", 0.70, 0.70, 0.70 ),
35 Colour( "Grey60", 0.60, 0.60, 0.60 ),
36 Colour( "Grey50", 0.50, 0.50, 0.50 ),
37 Colour( "Grey45", 0.45, 0.45, 0.45 ),
38 Colour( "Grey40", 0.40, 0.40, 0.40 ),
39 Colour( "Grey35", 0.35, 0.35, 0.35 ),
40 Colour( "Grey30", 0.30, 0.30, 0.30 ),
41 Colour( "Grey25", 0.25, 0.25, 0.25 ),
42 Colour( "Grey20", 0.20, 0.20, 0.20 ),
43 Colour( "Grey15", 0.15, 0.15, 0.15 ),
44 Colour( "Grey10", 0.10, 0.10, 0.10 ),
45 Colour( "Grey5", 0.05, 0.05, 0.05 ),
46 Colour( "Black", 0.00, 0.00, 0.00 ),
47
48 # Greens
49 Colour( "DarkGreen", 0.10, 0.50, 0.10 ),
50 Colour( "Green", 0.20, 0.80, 0.20 ),
51 Colour( "PrimeGreen", 0.00, 1.00, 0.00 ),
52 # Reds
53 Colour( "DarkRed", 0.60, 0.10, 0.10 ),
54 Colour( "Red", 0.80, 0.20, 0.20 ),
55 Colour( "PrimeRed", 1.00, 0.00, 0.00 ),
56 Colour( "BrightRed", 1.00, 0.60, 0.60 ),
57 # Oranges
58 Colour( "Orange", 0.90, 0.70, 0.10 ),
59 # Yellows
60 Colour( "Gold", 1.00, 0.84, 0.00 ),
61 Colour( "Yellow", 0.80, 0.80, 0.00 ),
62 # Blues
63 Colour( "MidnightBlue", 0.10, 0.10, 0.4 ),
64 Colour( "DarkBlue", 0.10, 0.10, 0.60 ),
65 Colour( "Blue", 0.20, 0.20, 0.80 ),
66 Colour( "AquaBlue", 0.30, 0.30, 0.90 ),
67 Colour( "LightBlue", 0.40, 0.40, 1.00 ),
68 Colour( "PrimeBlue", 0.00, 0.00, 1.00 ),
69 Colour( "Cyan", 0.00, 1.00, 1.00 ),
70 # Purples.
71 Colour( "Purple", 0.90, 0.10, 0.90 ),
72 Colour( "DarkPurple", 0.68, 0.18, 0.64 ),
73 # Browns.
74 Colour( "Brown", 0.59, 0.28, 0.00 ),
75 # Misc.
76 Colour( "Silver", 0.75, 0.75, 0.75 ),
77 Colour( "Aqua", 0.00, 0.75, 1.00 ),
78
79 Colour( "BlackHilight", 0.0, 0.0, 0.0, 0.4 ), # Hilight colour over black background
80
81 # Outfit slot colours
82 # Taken from https://cran.r-project.org/web/packages/khroma/vignettes/tol.html#muted
83 Colour( "OutfitHeavy", 0.8, 0.4, 0.46 ),
84 Colour( "OutfitMedium", 0.16, 0.63, 0.81 ),
85 #Colour( "OutfitMedium", 0.55, 0.8, 0.93 ),
86 Colour( "OutfitLight", 0.75, 0.7, 0.40 ),
87 # Objects
88 Colour( "Inert", 221./255., 221./255., 221./255. ),
89 Colour( "Neutral", 221./255., 204./255., 119./255. ),
90 Colour( "Friend", 68./255., 170./255., 153./255. ),
91 Colour( "Hostile", 204./255., 68./255., 153./255. ),
92 Colour( "Restricted", 221./255., 153./255., 51./255. ),
93 # Mission Markers
94 # https://packages.tesselle.org/khroma/articles/tol.html#vibrant
95 Colour( "MarkerNew", 0x33/255., 0xBB/255., 0xEE/255. ),
96 Colour( "MarkerComputer", 0x33/255., 0xBB/255., 0xEE/255. ),
97 Colour( "MarkerLow", 0x00/255., 0x99/255., 0x88/255. ),
98 Colour( "MarkerHigh", 0xEE/255., 0x77/255., 0x33/255. ),
99 Colour( "MarkerPlot", 0xEE/255., 0x33/255., 0x77/255. ),
100 # Radar
101 Colour( "Radar_player", 0.9, 0.1, 0.9 ),
102 Colour( "Radar_tPilot", 1.0, 1.0, 1.0 ),
103 Colour( "Radar_tSpob", 1.0, 1.0, 1.0 ),
104 Colour( "Radar_weap", 0.8, 0.2, 0.2 ),
105 Colour( "Radar_hilight", 0.6, 1.0, 1.0 ),
106 # Health
107 Colour( "Shield", 0.2, 0.2, 0.8 ),
108 Colour( "Armour", 0.5, 0.5, 0.5 ),
109 Colour( "Energy", 0.2, 0.8, 0.2 ),
110 Colour( "Fuel", 0.9, 0.1, 0.4 ),
111
112 # Deiz's Super Font Palette
113 Colour( "FontRed", 1.0, 0.4, 0.4 ),
114 Colour( "FontGreen", 0.6, 1.0, 0.4 ),
115 Colour( "FontBlue", 0.4, 0.6, 1.0 ),
116 Colour( "FontYellow", 1.0, 1.0, 0.5 ),
117 Colour( "FontGrey", 0.7, 0.7, 0.7 ),
118 Colour( "FontPurple", 1.0, 0.3, 1.0 ),
119 Colour( "FontOrange", 1.0, 0.7, 0.3 ),
120 Colour( "FontWhite", 0.95, 0.95, 0.95 ),
121]
122COLOURS.sort()
123
124def write_header(f):
125 f.write(f"/* FILE GENERATED BY {__file__} */")
126
127def generate_h_file(f):
128 write_header(f)
129
130 f.write("""
131#pragma once
132
133""")
134 for col in COLOURS:
135 col.write_header( f )
136 f.write("""
137const glColour* col_fromName( const char* name );
138""")
139
140def generate_c_file(f):
141 write_header(f)
142
143 f.write(f"""
144#include <string.h>
145#include "colour.h"
146#include "log.h"
147
148#define NUM_COLOURS {num_colours}
149
150""")
151 for col in COLOURS:
152 col.write_source( f )
153
154 f.write("""
155typedef struct ColourSort_s {
156 const char *name;
157 const glColour *c;
158} ColourSort_t;
159const ColourSort_t col_list[NUM_COLOURS] = {
160""")
161 for col in COLOURS:
162 f.write(f" {{ .name=\"{col.name}\", .c=&c{col.name} }},\n")
163 f.write("""
164};
165""")
166
167 f.write("""
168static int colour_cmp( const void *p1, const void *p2 )
169{
170 const ColourSort_t *c1, *c2;
171 c1 = (const ColourSort_t*) p1;
172 c2 = (const ColourSort_t*) p2;
173 return strcasecmp( c1->name, c2->name );
174}
175
176const glColour* col_fromName( const char* name )
177{
178 const ColourSort_t k = { .name = name };
179 const ColourSort_t *q = bsearch( &k, col_list, NUM_COLOURS, sizeof(ColourSort_t), colour_cmp );
180 if (q==NULL) {
181 WARN(_("Unknown colour '%s'!"),name);
182 return NULL;
183 }
184 return q->c;
185}""")
186
187with open("colours.gen.h", "w") as colours_ggen_h:
188 generate_h_file(colours_ggen_h)
189
190with open("colours.gen.c", "w") as colours_ggen_c:
191 generate_c_file(colours_ggen_c)