OpenTTD Source 20250524-master-gc366e6a48e
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 <http://www.gnu.org/licenses/>.
6 */
7
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
43/* Palette and reshade lookup table. */
44using PaletteLookup = std::array<uint8_t, 1U << (PALETTE_BITS * 3)>;
45static PaletteLookup _palette_lookup{};
46
47using ReshadeLookup = std::array<uint8_t, 1U << PALETTE_BITS>;
48static ReshadeLookup _reshade_lookup{};
49
60inline uint CrunchColour(uint c)
61{
62 return (c & PALETTE_BITS_MASK) | PALETTE_BITS_OR;
63}
64
73static uint CalculateColourDistance(const Colour &col1, int r2, int g2, int b2)
74{
75 /* Euclidean colour distance for sRGB based on https://en.wikipedia.org/wiki/Color_difference#sRGB */
76 int r = (int)col1.r - (int)r2;
77 int g = (int)col1.g - (int)g2;
78 int b = (int)col1.b - (int)b2;
79
80 int avgr = (col1.r + r2) / 2;
81 return ((2 + (avgr / 256.0)) * r * r) + (4 * g * g) + ((2 + ((255 - avgr) / 256.0)) * b * b);
82}
83
84/* Palette indexes for conversion. See docs/palettes/palette_key.png */
85const uint8_t PALETTE_INDEX_CC_START = 198;
87const uint8_t PALETTE_INDEX_START = 1;
88const uint8_t PALETTE_INDEX_END = 215;
89
97static uint8_t FindNearestColourIndex(uint8_t r, uint8_t g, uint8_t b)
98{
99 r = CrunchColour(r);
100 g = CrunchColour(g);
101 b = CrunchColour(b);
102
103 uint best_index = 0;
104 uint best_distance = UINT32_MAX;
105
106 for (uint i = PALETTE_INDEX_START; i < PALETTE_INDEX_CC_START; i++) {
107 if (uint distance = CalculateColourDistance(_palette.palette[i], r, g, b); distance < best_distance) {
108 best_index = i;
109 best_distance = distance;
110 }
111 }
112 /* There's a hole in the palette reserved for company colour remaps. */
113 for (uint i = PALETTE_INDEX_CC_END; i < PALETTE_INDEX_END; i++) {
114 if (uint distance = CalculateColourDistance(_palette.palette[i], r, g, b); distance < best_distance) {
115 best_index = i;
116 best_distance = distance;
117 }
118 }
119 return best_index;
120}
121
127static uint8_t FindNearestColourReshadeIndex(uint8_t b)
128{
129 b = CrunchColour(b);
130
131 uint best_index = 0;
132 uint best_distance = UINT32_MAX;
133
134 for (uint i = PALETTE_INDEX_CC_START; i < PALETTE_INDEX_CC_END; i++) {
135 if (uint distance = CalculateColourDistance(_palette.palette[i], b, b, b); distance < best_distance) {
136 best_index = i;
137 best_distance = distance;
138 }
139 }
140 return best_index;
141}
142
151uint8_t GetNearestColourIndex(uint8_t r, uint8_t g, uint8_t b)
152{
153 uint32_t key = (r >> PALETTE_SHIFT) | (g >> PALETTE_SHIFT) << PALETTE_BITS | (b >> PALETTE_SHIFT) << (PALETTE_BITS * 2);
154 if (_palette_lookup[key] == 0) _palette_lookup[key] = FindNearestColourIndex(r, g, b);
155 return _palette_lookup[key];
156}
157
165{
166 uint32_t key = (b >> PALETTE_SHIFT);
167 if (_reshade_lookup[key] == 0) _reshade_lookup[key] = FindNearestColourReshadeIndex(b);
168 return _reshade_lookup[key];
169}
170
177Colour ReallyAdjustBrightness(Colour colour, int brightness)
178{
179 if (brightness == DEFAULT_BRIGHTNESS) return colour;
180
181 uint64_t combined = (static_cast<uint64_t>(colour.r) << 32) | (static_cast<uint64_t>(colour.g) << 16) | static_cast<uint64_t>(colour.b);
182 combined *= brightness;
183
184 uint16_t r = GB(combined, 39, 9);
185 uint16_t g = GB(combined, 23, 9);
186 uint16_t b = GB(combined, 7, 9);
187
188 if ((combined & 0x800080008000L) == 0L) {
189 return Colour(r, g, b, colour.a);
190 }
191
192 uint16_t ob = 0;
193 /* Sum overbright */
194 if (r > 255) ob += r - 255;
195 if (g > 255) ob += g - 255;
196 if (b > 255) ob += b - 255;
197
198 /* Reduce overbright strength */
199 ob /= 2;
200 return Colour(
201 r >= 255 ? 255 : std::min(r + ob * (255 - r) / 256, 255),
202 g >= 255 ? 255 : std::min(g + ob * (255 - g) / 256, 255),
203 b >= 255 ? 255 : std::min(b + ob * (255 - b) / 256, 255),
204 colour.a);
205}
206
207void DoPaletteAnimations();
208
209void GfxInitPalettes()
210{
211 std::lock_guard<std::recursive_mutex> lock(_palette_mutex);
213 DoPaletteAnimations();
214}
215
225bool CopyPalette(Palette &local_palette, bool force_copy)
226{
227 std::lock_guard<std::recursive_mutex> lock(_palette_mutex);
228
229 if (!force_copy && _cur_palette.count_dirty == 0) return false;
230
231 local_palette = _cur_palette;
233
234 if (force_copy) {
235 local_palette.first_dirty = 0;
236 local_palette.count_dirty = 256;
237 }
238
239 return true;
240}
241
242#define EXTR(p, q) (((uint16_t)(palette_animation_counter * (p)) * (q)) >> 16)
243#define EXTR2(p, q) (((uint16_t)(~palette_animation_counter * (p)) * (q)) >> 16)
244
245void DoPaletteAnimations()
246{
247 std::lock_guard<std::recursive_mutex> lock(_palette_mutex);
248
249 /* Animation counter for the palette animation. */
250 static int palette_animation_counter = 0;
251 palette_animation_counter += 8;
252
254 const Colour *s;
256 const uint old_tc = palette_animation_counter;
257 uint j;
258
259 if (blitter != nullptr && blitter->UsePaletteAnimation() == Blitter::PaletteAnimation::None) {
260 palette_animation_counter = 0;
261 }
262
263 std::span<Colour> current_palette{&_cur_palette.palette[PALETTE_ANIM_START], PALETTE_ANIM_SIZE};
264 /* Makes a copy of the current animation palette in old_val,
265 * so the work on the current palette could be compared, see if there has been any changes */
266 std::array<Colour, PALETTE_ANIM_SIZE> original_palette;
267 std::ranges::copy(current_palette, original_palette.begin());
268 auto palette_pos = current_palette.begin(); // Points to where animations are taking place on the palette
269
270 /* Fizzy Drink bubbles animation */
271 s = ev->fizzy_drink;
272 j = EXTR2(512, EPV_CYCLES_FIZZY_DRINK);
273 for (uint i = 0; i != EPV_CYCLES_FIZZY_DRINK; i++) {
274 *palette_pos++ = s[j];
275 j++;
276 if (j == EPV_CYCLES_FIZZY_DRINK) j = 0;
277 }
278
279 /* Oil refinery fire animation */
280 s = ev->oil_refinery;
281 j = EXTR2(512, EPV_CYCLES_OIL_REFINERY);
282 for (uint i = 0; i != EPV_CYCLES_OIL_REFINERY; i++) {
283 *palette_pos++ = s[j];
284 j++;
285 if (j == EPV_CYCLES_OIL_REFINERY) j = 0;
286 }
287
288 /* Radio tower blinking */
289 {
290 uint8_t i = (palette_animation_counter >> 1) & 0x7F;
291 uint8_t v;
292
293 if (i < 0x3f) {
294 v = 255;
295 } else if (i < 0x4A || i >= 0x75) {
296 v = 128;
297 } else {
298 v = 20;
299 }
300 palette_pos->r = v;
301 palette_pos->g = 0;
302 palette_pos->b = 0;
303 palette_pos++;
304
305 i ^= 0x40;
306 if (i < 0x3f) {
307 v = 255;
308 } else if (i < 0x4A || i >= 0x75) {
309 v = 128;
310 } else {
311 v = 20;
312 }
313 palette_pos->r = v;
314 palette_pos->g = 0;
315 palette_pos->b = 0;
316 palette_pos++;
317 }
318
319 /* Handle lighthouse and stadium animation */
320 s = ev->lighthouse;
321 j = EXTR(256, EPV_CYCLES_LIGHTHOUSE);
322 for (uint i = 0; i != EPV_CYCLES_LIGHTHOUSE; i++) {
323 *palette_pos++ = s[j];
324 j++;
325 if (j == EPV_CYCLES_LIGHTHOUSE) j = 0;
326 }
327
328 /* Dark blue water */
329 s = (_settings_game.game_creation.landscape == LandscapeType::Toyland) ? ev->dark_water_toyland : ev->dark_water;
330 j = EXTR(320, EPV_CYCLES_DARK_WATER);
331 for (uint i = 0; i != EPV_CYCLES_DARK_WATER; i++) {
332 *palette_pos++ = s[j];
333 j++;
334 if (j == EPV_CYCLES_DARK_WATER) j = 0;
335 }
336
337 /* Glittery water */
338 s = (_settings_game.game_creation.landscape == LandscapeType::Toyland) ? ev->glitter_water_toyland : ev->glitter_water;
339 j = EXTR(128, EPV_CYCLES_GLITTER_WATER);
340 for (uint i = 0; i != EPV_CYCLES_GLITTER_WATER / 3; i++) {
341 *palette_pos++ = s[j];
342 j += 3;
344 }
345
346 if (blitter != nullptr && blitter->UsePaletteAnimation() == Blitter::PaletteAnimation::None) {
347 palette_animation_counter = old_tc;
348 } else if (_cur_palette.count_dirty == 0 && !std::ranges::equal(current_palette, original_palette)) {
349 /* Did we changed anything on the palette? Seems so. Mark it as dirty */
352 }
353}
354
361TextColour GetContrastColour(uint8_t background, uint8_t threshold)
362{
363 Colour c = _cur_palette.palette[background];
364 /* Compute brightness according to http://www.w3.org/TR/AERT#color-contrast.
365 * The following formula computes 1000 * brightness^2, with brightness being in range 0 to 255. */
366 uint sq1000_brightness = c.r * c.r * 299 + c.g * c.g * 587 + c.b * c.b * 114;
367 /* Compare with threshold brightness which defaults to 128 (50%) */
368 return sq1000_brightness < ((uint) threshold) * ((uint) threshold) * 1000 ? TC_WHITE : TC_BLACK;
369}
370
376{
377 using ColourGradient = std::array<uint8_t, SHADE_END>;
378
379 static inline std::array<ColourGradient, COLOUR_END> gradient{};
380};
381
388uint8_t GetColourGradient(Colours colour, ColourShade shade)
389{
390 return ColourGradients::gradient[colour % COLOUR_END][shade % SHADE_END];
391}
392
399void SetColourGradient(Colours colour, ColourShade shade, uint8_t palette_index)
400{
401 assert(colour < COLOUR_END);
402 assert(shade < SHADE_END);
403 ColourGradients::gradient[colour % COLOUR_END][shade % SHADE_END] = palette_index;
404}
Base for all blitters.
debug_inline 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:136
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.
Factory to 'query' all available blitters.
Functions for Standard In/Out file operations.
Types related to the graphics and/or input devices.
static constexpr uint8_t PALETTE_ANIM_SIZE
number of animated colours
Definition gfx_type.h:335
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:336
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition gfx_type.h:302
Types related to the landscape.
bool CopyPalette(Palette &local_palette, bool force_copy)
Copy the current palette if the palette was updated.
Definition palette.cpp:225
const uint8_t PALETTE_INDEX_END
Palette index of end of defined palette.
Definition palette.cpp:88
const uint8_t PALETTE_INDEX_CC_END
Palette index of end of company colour remap area.
Definition palette.cpp:86
void SetColourGradient(Colours colour, ColourShade shade, uint8_t palette_index)
Set colour gradient palette index.
Definition palette.cpp:399
uint CrunchColour(uint c)
Reduce bits per channel to PALETTE_BITS, and place value in the middle of the reduced range.
Definition palette.cpp:60
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:127
uint8_t GetColourGradient(Colours colour, ColourShade shade)
Get colour gradient palette index.
Definition palette.cpp:388
static uint CalculateColourDistance(const Colour &col1, int r2, int g2, int b2)
Calculate distance between two colours.
Definition palette.cpp:73
const uint8_t PALETTE_INDEX_CC_START
Palette index of start of company colour remap area.
Definition palette.cpp:85
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
TextColour GetContrastColour(uint8_t background, uint8_t threshold)
Determine a contrasty text colour for a coloured background.
Definition palette.cpp:361
Colour ReallyAdjustBrightness(Colour colour, int brightness)
Adjust brightness of colour.
Definition palette.cpp:177
const uint8_t PALETTE_INDEX_START
Palette index of start of defined palette.
Definition palette.cpp:87
Palette _cur_palette
Current palette.
Definition palette.cpp:24
uint8_t GetNearestColourReshadeIndex(uint8_t b)
Get nearest colour palette index from a brightness level.
Definition palette.cpp:164
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:97
uint8_t GetNearestColourIndex(uint8_t r, uint8_t g, uint8_t b)
Get nearest colour palette index from an RGB colour.
Definition palette.cpp:151
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:60
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:376
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
LandscapeType landscape
the landscape we're currently in
GameCreationSettings game_creation
settings used during the creation of a game (map)
Information about the currently used palette.
Definition gfx_type.h:368
int first_dirty
The first dirty element.
Definition gfx_type.h:370
int count_dirty
The number of dirty elements.
Definition gfx_type.h:371
Colour palette[256]
Current palette. Entry 0 has to be always fully transparent!
Definition gfx_type.h:369
Base of all threads.
std::mutex lock
synchronization for playback status fields
Definition win32_m.cpp:35