OpenTTD Source 20260311-master-g511d3794ce
palette.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#include "stdafx.h"
11#include "blitter/base.hpp"
12#include "blitter/factory.hpp"
13#include "fileio_func.h"
14#include "gfx_type.h"
15#include "landscape_type.h"
16#include "palette_func.h"
17#include "settings_type.h"
18#include "thread.h"
19
20#include "table/palettes.h"
21
22#include "safeguards.h"
23
25
26static std::recursive_mutex _palette_mutex;
27
38const uint PALETTE_BITS = 6;
39const uint PALETTE_SHIFT = 8 - PALETTE_BITS;
40const uint PALETTE_BITS_MASK = ((1U << PALETTE_BITS) - 1) << PALETTE_SHIFT;
41const uint PALETTE_BITS_OR = (1U << (PALETTE_SHIFT - 1));
42
45using PaletteLookup = std::array<uint8_t, 1U << (PALETTE_BITS * 3)>;
48
51using ReshadeLookup = std::array<uint8_t, 1U << PALETTE_BITS>;
54
65inline uint CrunchColour(uint c)
66{
67 return (c & PALETTE_BITS_MASK) | PALETTE_BITS_OR;
68}
69
78static uint CalculateColourDistance(const Colour &col1, int r2, int g2, int b2)
79{
80 /* Euclidean colour distance for sRGB based on https://en.wikipedia.org/wiki/Color_difference#sRGB */
81 int r = (int)col1.r - (int)r2;
82 int g = (int)col1.g - (int)g2;
83 int b = (int)col1.b - (int)b2;
84
85 int avgr = (col1.r + r2) / 2;
86 return ((2 + (avgr / 256.0)) * r * r) + (4 * g * g) + ((2 + ((255 - avgr) / 256.0)) * b * b);
87}
88
89/* Palette indexes for conversion. See docs/palettes/palette_key.png */
90const uint8_t PALETTE_INDEX_CC_START = 198;
92const uint8_t PALETTE_INDEX_START = 1;
93const uint8_t PALETTE_INDEX_END = 215;
94
102static uint8_t FindNearestColourIndex(uint8_t r, uint8_t g, uint8_t b)
103{
104 r = CrunchColour(r);
105 g = CrunchColour(g);
106 b = CrunchColour(b);
107
108 uint best_index = 0;
109 uint best_distance = UINT32_MAX;
110
111 for (uint i = PALETTE_INDEX_START; i < PALETTE_INDEX_CC_START; i++) {
112 if (uint distance = CalculateColourDistance(_palette.palette[i], r, g, b); distance < best_distance) {
113 best_index = i;
114 best_distance = distance;
115 }
116 }
117 /* There's a hole in the palette reserved for company colour remaps. */
118 for (uint i = PALETTE_INDEX_CC_END; i < PALETTE_INDEX_END; i++) {
119 if (uint distance = CalculateColourDistance(_palette.palette[i], r, g, b); distance < best_distance) {
120 best_index = i;
121 best_distance = distance;
122 }
123 }
124 return best_index;
125}
126
132static uint8_t FindNearestColourReshadeIndex(uint8_t b)
133{
134 b = CrunchColour(b);
135
136 uint best_index = 0;
137 uint best_distance = UINT32_MAX;
138
139 for (uint i = PALETTE_INDEX_CC_START; i < PALETTE_INDEX_CC_END; i++) {
140 if (uint distance = CalculateColourDistance(_palette.palette[i], b, b, b); distance < best_distance) {
141 best_index = i;
142 best_distance = distance;
143 }
144 }
145 return best_index;
146}
147
156uint8_t GetNearestColourIndex(uint8_t r, uint8_t g, uint8_t b)
157{
158 uint32_t key = (r >> PALETTE_SHIFT) | (g >> PALETTE_SHIFT) << PALETTE_BITS | (b >> PALETTE_SHIFT) << (PALETTE_BITS * 2);
159 if (_palette_lookup[key] == 0) _palette_lookup[key] = FindNearestColourIndex(r, g, b);
160 return _palette_lookup[key];
161}
162
170{
171 uint32_t key = (b >> PALETTE_SHIFT);
173 return _reshade_lookup[key];
174}
175
182Colour ReallyAdjustBrightness(Colour colour, int brightness)
183{
184 if (brightness == DEFAULT_BRIGHTNESS) return colour;
185
186 uint64_t combined = (static_cast<uint64_t>(colour.r) << 32) | (static_cast<uint64_t>(colour.g) << 16) | static_cast<uint64_t>(colour.b);
187 combined *= brightness;
188
189 uint16_t r = GB(combined, 39, 9);
190 uint16_t g = GB(combined, 23, 9);
191 uint16_t b = GB(combined, 7, 9);
192
193 if ((combined & 0x800080008000L) == 0L) {
194 return Colour(r, g, b, colour.a);
195 }
196
197 uint16_t ob = 0;
198 /* Sum overbright */
199 if (r > 255) ob += r - 255;
200 if (g > 255) ob += g - 255;
201 if (b > 255) ob += b - 255;
202
203 /* Reduce overbright strength */
204 ob /= 2;
205 return Colour(
206 r >= 255 ? 255 : std::min(r + ob * (255 - r) / 256, 255),
207 g >= 255 ? 255 : std::min(g + ob * (255 - g) / 256, 255),
208 b >= 255 ? 255 : std::min(b + ob * (255 - b) / 256, 255),
209 colour.a);
210}
211
212void DoPaletteAnimations();
213
214void GfxInitPalettes()
215{
216 std::lock_guard<std::recursive_mutex> lock(_palette_mutex);
218 DoPaletteAnimations();
219}
220
230bool CopyPalette(Palette &local_palette, bool force_copy)
231{
232 std::lock_guard<std::recursive_mutex> lock(_palette_mutex);
233
234 if (!force_copy && _cur_palette.count_dirty == 0) return false;
235
236 local_palette = _cur_palette;
237 _cur_palette.count_dirty = 0;
238
239 if (force_copy) {
240 local_palette.first_dirty = 0;
241 local_palette.count_dirty = 256;
242 }
243
244 return true;
245}
246
247#define EXTR(p, q) (((uint16_t)(palette_animation_counter * (p)) * (q)) >> 16)
248#define EXTR2(p, q) (((uint16_t)(~palette_animation_counter * (p)) * (q)) >> 16)
249
250void DoPaletteAnimations()
251{
252 std::lock_guard<std::recursive_mutex> lock(_palette_mutex);
253
254 /* Animation counter for the palette animation. */
255 static int palette_animation_counter = 0;
256 palette_animation_counter += 8;
257
259 const Colour *s;
261 const uint old_tc = palette_animation_counter;
262 uint j;
263
264 if (blitter != nullptr && blitter->UsePaletteAnimation() == Blitter::PaletteAnimation::None) {
265 palette_animation_counter = 0;
266 }
267
268 std::span<Colour> current_palette{&_cur_palette.palette[PALETTE_ANIM_START], PALETTE_ANIM_SIZE};
269 /* Makes a copy of the current animation palette in old_val,
270 * so the work on the current palette could be compared, see if there has been any changes */
271 std::array<Colour, PALETTE_ANIM_SIZE> original_palette;
272 std::ranges::copy(current_palette, original_palette.begin());
273 auto palette_pos = current_palette.begin(); // Points to where animations are taking place on the palette
274
275 /* Fizzy Drink bubbles animation */
276 s = ev->fizzy_drink;
277 j = EXTR2(512, EPV_CYCLES_FIZZY_DRINK);
278 for (uint i = 0; i != EPV_CYCLES_FIZZY_DRINK; i++) {
279 *palette_pos++ = s[j];
280 j++;
281 if (j == EPV_CYCLES_FIZZY_DRINK) j = 0;
282 }
283
284 /* Oil refinery fire animation */
285 s = ev->oil_refinery;
286 j = EXTR2(512, EPV_CYCLES_OIL_REFINERY);
287 for (uint i = 0; i != EPV_CYCLES_OIL_REFINERY; i++) {
288 *palette_pos++ = s[j];
289 j++;
290 if (j == EPV_CYCLES_OIL_REFINERY) j = 0;
291 }
292
293 /* Radio tower blinking */
294 {
295 uint8_t i = (palette_animation_counter >> 1) & 0x7F;
296 uint8_t v;
297
298 if (i < 0x3f) {
299 v = 255;
300 } else if (i < 0x4A || i >= 0x75) {
301 v = 128;
302 } else {
303 v = 20;
304 }
305 palette_pos->r = v;
306 palette_pos->g = 0;
307 palette_pos->b = 0;
308 palette_pos++;
309
310 i ^= 0x40;
311 if (i < 0x3f) {
312 v = 255;
313 } else if (i < 0x4A || i >= 0x75) {
314 v = 128;
315 } else {
316 v = 20;
317 }
318 palette_pos->r = v;
319 palette_pos->g = 0;
320 palette_pos->b = 0;
321 palette_pos++;
322 }
323
324 /* Handle lighthouse and stadium animation */
325 s = ev->lighthouse;
326 j = EXTR(256, EPV_CYCLES_LIGHTHOUSE);
327 for (uint i = 0; i != EPV_CYCLES_LIGHTHOUSE; i++) {
328 *palette_pos++ = s[j];
329 j++;
330 if (j == EPV_CYCLES_LIGHTHOUSE) j = 0;
331 }
332
333 /* Dark blue water */
334 s = (_settings_game.game_creation.landscape == LandscapeType::Toyland) ? ev->dark_water_toyland : ev->dark_water;
335 j = EXTR(320, EPV_CYCLES_DARK_WATER);
336 for (uint i = 0; i != EPV_CYCLES_DARK_WATER; i++) {
337 *palette_pos++ = s[j];
338 j++;
339 if (j == EPV_CYCLES_DARK_WATER) j = 0;
340 }
341
342 /* Glittery water */
343 s = (_settings_game.game_creation.landscape == LandscapeType::Toyland) ? ev->glitter_water_toyland : ev->glitter_water;
344 j = EXTR(128, EPV_CYCLES_GLITTER_WATER);
345 for (uint i = 0; i != EPV_CYCLES_GLITTER_WATER / 3; i++) {
346 *palette_pos++ = s[j];
347 j += 3;
349 }
350
351 if (blitter != nullptr && blitter->UsePaletteAnimation() == Blitter::PaletteAnimation::None) {
352 palette_animation_counter = old_tc;
353 } else if (_cur_palette.count_dirty == 0 && !std::ranges::equal(current_palette, original_palette)) {
354 /* Did we changed anything on the palette? Seems so. Mark it as dirty */
355 _cur_palette.first_dirty = PALETTE_ANIM_START;
356 _cur_palette.count_dirty = PALETTE_ANIM_SIZE;
357 }
358}
359
366TextColour GetContrastColour(PixelColour background, uint8_t threshold)
367{
368 Colour c = _cur_palette.palette[background.p];
369 /* Compute brightness according to http://www.w3.org/TR/AERT#color-contrast.
370 * The following formula computes 1000 * brightness^2, with brightness being in range 0 to 255. */
371 uint sq1000_brightness = c.r * c.r * 299 + c.g * c.g * 587 + c.b * c.b * 114;
372 /* Compare with threshold brightness which defaults to 128 (50%) */
373 return sq1000_brightness < ((uint) threshold) * ((uint) threshold) * 1000 ? TC_WHITE : TC_BLACK;
374}
375
381{
382 using ColourGradient = std::array<PixelColour, SHADE_END>;
383
384 static inline std::array<ColourGradient, COLOUR_END> gradient{};
385};
386
393PixelColour GetColourGradient(Colours colour, ColourShade shade)
394{
395 return ColourGradients::gradient[colour % COLOUR_END][shade % SHADE_END];
396}
397
404void SetColourGradient(Colours colour, ColourShade shade, PixelColour palette_index)
405{
406 assert(colour < COLOUR_END);
407 assert(shade < SHADE_END);
408 ColourGradients::gradient[colour % COLOUR_END][shade % SHADE_END] = palette_index;
409}
Base for all blitters.
static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
Definition factory.hpp:139
How all blitters should look like.
Definition base.hpp:29
virtual Blitter::PaletteAnimation UsePaletteAnimation()=0
Check if the blitter uses palette animation at all.
@ None
No palette animation.
Definition base.hpp:51
Factory to 'query' all available blitters.
Functions for standard in/out file operations.
Palette _cur_palette
Current palette.
Definition palette.cpp:24
Types related to the graphics and/or input devices.
static constexpr uint8_t PALETTE_ANIM_SIZE
number of animated colours
Definition gfx_type.h:340
static constexpr uint8_t PALETTE_ANIM_START
Index in the _palettes array from which all animations are taking places (table/palettes....
Definition gfx_type.h:341
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition gfx_type.h:307
Types related to the landscape.
@ Toyland
Landscape with funky industries and vehicles.
bool CopyPalette(Palette &local_palette, bool force_copy)
Copy the current palette if the palette was updated.
Definition palette.cpp:230
const uint8_t PALETTE_INDEX_END
Palette index of end of defined palette.
Definition palette.cpp:93
const uint8_t PALETTE_INDEX_CC_END
Palette index of end of company colour remap area.
Definition palette.cpp:91
uint CrunchColour(uint c)
Reduce bits per channel to PALETTE_BITS, and place value in the middle of the reduced range.
Definition palette.cpp:65
static std::recursive_mutex _palette_mutex
To coordinate access to _cur_palette.
Definition palette.cpp:26
static uint8_t FindNearestColourReshadeIndex(uint8_t b)
Find nearest company colour palette index for a brightness level.
Definition palette.cpp:132
static uint CalculateColourDistance(const Colour &col1, int r2, int g2, int b2)
Calculate distance between two colours.
Definition palette.cpp:78
const uint8_t PALETTE_INDEX_CC_START
Palette index of start of company colour remap area.
Definition palette.cpp:90
const uint PALETTE_BITS
PALETTE_BITS reduces the bits-per-channel of 32bpp graphics data to allow faster palette lookups from...
Definition palette.cpp:38
static ReshadeLookup _reshade_lookup
Definition palette.cpp:52
TextColour GetContrastColour(PixelColour background, uint8_t threshold)
Determine a contrasty text colour for a coloured background.
Definition palette.cpp:366
Colour ReallyAdjustBrightness(Colour colour, int brightness)
Adjust brightness of colour.
Definition palette.cpp:182
const uint8_t PALETTE_INDEX_START
Palette index of start of defined palette.
Definition palette.cpp:92
uint8_t GetNearestColourReshadeIndex(uint8_t b)
Get nearest colour palette index from a brightness level.
Definition palette.cpp:169
std::array< uint8_t, 1U<<(PALETTE_BITS *3)> PaletteLookup
Definition palette.cpp:45
void SetColourGradient(Colours colour, ColourShade shade, PixelColour palette_index)
Set colour gradient palette index.
Definition palette.cpp:404
static PaletteLookup _palette_lookup
Definition palette.cpp:46
static uint8_t FindNearestColourIndex(uint8_t r, uint8_t g, uint8_t b)
Find nearest colour palette index for a 32bpp pixel.
Definition palette.cpp:102
uint8_t GetNearestColourIndex(uint8_t r, uint8_t g, uint8_t b)
Get nearest colour palette index from an RGB colour.
Definition palette.cpp:156
PixelColour GetColourGradient(Colours colour, ColourShade shade)
Get colour gradient palette index.
Definition palette.cpp:393
std::array< uint8_t, 1U<< PALETTE_BITS > ReshadeLookup
Definition palette.cpp:51
Functions related to palettes.
The colour translation of the GRF palettes.
static const Palette _palette
Colour palette (DOS).
Definition palettes.h:13
static const uint EPV_CYCLES_DARK_WATER
Description of the length of the palette cycle animations.
Definition palettes.h:95
static const uint EPV_CYCLES_OIL_REFINERY
length of the oil refinery's fire animation
Definition palettes.h:97
static const uint EPV_CYCLES_LIGHTHOUSE
length of the lighthouse/stadium animation
Definition palettes.h:96
static const uint EPV_CYCLES_GLITTER_WATER
length of the glittery water animation
Definition palettes.h:99
static const ExtraPaletteValues _extra_palette_values
Actual palette animation tables.
Definition palettes.h:113
static const uint EPV_CYCLES_FIZZY_DRINK
length of the fizzy drinks animation
Definition palettes.h:98
A number of safeguards to prevent using unsafe methods.
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition settings.cpp:61
Types related to global configuration settings.
Definition of base types and functions in a cross-platform compatible way.
Lookup table of colour shades for all 16 colour gradients.
Definition palette.cpp:381
Description of tables for the palette animation.
Definition palettes.h:102
Colour fizzy_drink[EPV_CYCLES_FIZZY_DRINK]
fizzy drinks
Definition palettes.h:107
Colour lighthouse[EPV_CYCLES_LIGHTHOUSE]
lighthouse & stadium
Definition palettes.h:105
Colour dark_water_toyland[EPV_CYCLES_DARK_WATER]
dark blue water Toyland
Definition palettes.h:104
Colour glitter_water_toyland[EPV_CYCLES_GLITTER_WATER]
glittery water Toyland
Definition palettes.h:109
Colour oil_refinery[EPV_CYCLES_OIL_REFINERY]
oil refinery
Definition palettes.h:106
int first_dirty
The first dirty element.
Definition gfx_type.h:375
int count_dirty
The number of dirty elements.
Definition gfx_type.h:376
Colour for pixel/line drawing.
Definition gfx_type.h:405
uint8_t p
Palette index.
Definition gfx_type.h:406
Base of all threads.
std::mutex lock
synchronization for playback status fields
Definition win32_m.cpp:35