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