OpenTTD Source 20250205-master-gfd85ab1e2c
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);
212 memcpy(&_cur_palette, &_palette, sizeof(_cur_palette));
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 Colour old_val[PALETTE_ANIM_SIZE];
257 const uint old_tc = palette_animation_counter;
258 uint j;
259
260 if (blitter != nullptr && blitter->UsePaletteAnimation() == Blitter::PaletteAnimation::None) {
261 palette_animation_counter = 0;
262 }
263
264 Colour *palette_pos = &_cur_palette.palette[PALETTE_ANIM_START]; // Points to where animations are taking place on the palette
265 /* Makes a copy of the current animation palette in old_val,
266 * so the work on the current palette could be compared, see if there has been any changes */
267 memcpy(old_val, palette_pos, sizeof(old_val));
268
269 /* Fizzy Drink bubbles animation */
270 s = ev->fizzy_drink;
271 j = EXTR2(512, EPV_CYCLES_FIZZY_DRINK);
272 for (uint i = 0; i != EPV_CYCLES_FIZZY_DRINK; i++) {
273 *palette_pos++ = s[j];
274 j++;
275 if (j == EPV_CYCLES_FIZZY_DRINK) j = 0;
276 }
277
278 /* Oil refinery fire animation */
279 s = ev->oil_refinery;
280 j = EXTR2(512, EPV_CYCLES_OIL_REFINERY);
281 for (uint i = 0; i != EPV_CYCLES_OIL_REFINERY; i++) {
282 *palette_pos++ = s[j];
283 j++;
284 if (j == EPV_CYCLES_OIL_REFINERY) j = 0;
285 }
286
287 /* Radio tower blinking */
288 {
289 uint8_t i = (palette_animation_counter >> 1) & 0x7F;
290 uint8_t v;
291
292 if (i < 0x3f) {
293 v = 255;
294 } else if (i < 0x4A || i >= 0x75) {
295 v = 128;
296 } else {
297 v = 20;
298 }
299 palette_pos->r = v;
300 palette_pos->g = 0;
301 palette_pos->b = 0;
302 palette_pos++;
303
304 i ^= 0x40;
305 if (i < 0x3f) {
306 v = 255;
307 } else if (i < 0x4A || i >= 0x75) {
308 v = 128;
309 } else {
310 v = 20;
311 }
312 palette_pos->r = v;
313 palette_pos->g = 0;
314 palette_pos->b = 0;
315 palette_pos++;
316 }
317
318 /* Handle lighthouse and stadium animation */
319 s = ev->lighthouse;
320 j = EXTR(256, EPV_CYCLES_LIGHTHOUSE);
321 for (uint i = 0; i != EPV_CYCLES_LIGHTHOUSE; i++) {
322 *palette_pos++ = s[j];
323 j++;
324 if (j == EPV_CYCLES_LIGHTHOUSE) j = 0;
325 }
326
327 /* Dark blue water */
328 s = (_settings_game.game_creation.landscape == LandscapeType::Toyland) ? ev->dark_water_toyland : ev->dark_water;
329 j = EXTR(320, EPV_CYCLES_DARK_WATER);
330 for (uint i = 0; i != EPV_CYCLES_DARK_WATER; i++) {
331 *palette_pos++ = s[j];
332 j++;
333 if (j == EPV_CYCLES_DARK_WATER) j = 0;
334 }
335
336 /* Glittery water */
337 s = (_settings_game.game_creation.landscape == LandscapeType::Toyland) ? ev->glitter_water_toyland : ev->glitter_water;
338 j = EXTR(128, EPV_CYCLES_GLITTER_WATER);
339 for (uint i = 0; i != EPV_CYCLES_GLITTER_WATER / 3; i++) {
340 *palette_pos++ = s[j];
341 j += 3;
343 }
344
345 if (blitter != nullptr && blitter->UsePaletteAnimation() == Blitter::PaletteAnimation::None) {
346 palette_animation_counter = old_tc;
347 } else if (_cur_palette.count_dirty == 0 && memcmp(old_val, &_cur_palette.palette[PALETTE_ANIM_START], sizeof(old_val)) != 0) {
348 /* Did we changed anything on the palette? Seems so. Mark it as dirty */
351 }
352}
353
360TextColour GetContrastColour(uint8_t background, uint8_t threshold)
361{
362 Colour c = _cur_palette.palette[background];
363 /* Compute brightness according to http://www.w3.org/TR/AERT#color-contrast.
364 * The following formula computes 1000 * brightness^2, with brightness being in range 0 to 255. */
365 uint sq1000_brightness = c.r * c.r * 299 + c.g * c.g * 587 + c.b * c.b * 114;
366 /* Compare with threshold brightness which defaults to 128 (50%) */
367 return sq1000_brightness < ((uint) threshold) * ((uint) threshold) * 1000 ? TC_WHITE : TC_BLACK;
368}
369
375{
376 using ColourGradient = std::array<uint8_t, SHADE_END>;
377
378 static inline std::array<ColourGradient, COLOUR_END> gradient{};
379};
380
387uint8_t GetColourGradient(Colours colour, ColourShade shade)
388{
389 return ColourGradients::gradient[colour % COLOUR_END][shade % SHADE_END];
390}
391
398void SetColourGradient(Colours colour, ColourShade shade, uint8_t palette_index)
399{
400 assert(colour < COLOUR_END);
401 assert(shade < SHADE_END);
402 ColourGradients::gradient[colour % COLOUR_END][shade % SHADE_END] = palette_index;
403}
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:138
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:327
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:328
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition gfx_type.h:294
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:398
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:387
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:360
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:57
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:375
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:360
int first_dirty
The first dirty element.
Definition gfx_type.h:362
int count_dirty
The number of dirty elements.
Definition gfx_type.h:363
Colour palette[256]
Current palette. Entry 0 has to be always fully transparent!
Definition gfx_type.h:361
Base of all threads.
std::mutex lock
synchronization for playback status fields
Definition win32_m.cpp:35