OpenTTD Source: src/palette.cpp Source File

OpenTTD Source 15.3

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
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
24 Palette _cur_palette;
25
26 static std::recursive_mutex _palette_mutex;
27
38 const 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
60 inline uint CrunchColour(uint c)
61{
62 return (c & PALETTE_BITS_MASK) | PALETTE_BITS_OR;
63}
64
73 static 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 */
85 const uint8_t PALETTE_INDEX_CC_START = 198;
86 const uint8_t PALETTE_INDEX_CC_END = PALETTE_INDEX_CC_START + 8;
87 const uint8_t PALETTE_INDEX_START = 1;
88 const uint8_t PALETTE_INDEX_END = 215;
89
97 static 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
127 static 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
164uint8_t GetNearestColourReshadeIndex(uint8_t b)
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 _cur_palette = _palette;
213 DoPaletteAnimations();
214}
215
225 bool 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;
232 _cur_palette.count_dirty = 0;
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
253 Blitter *blitter = BlitterFactory::GetCurrentBlitter();
254 const Colour *s;
255 const ExtraPaletteValues *ev = &_extra_palette_values;
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;
343 if (j >= EPV_CYCLES_GLITTER_WATER) j -= EPV_CYCLES_GLITTER_WATER;
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 */
350 _cur_palette.first_dirty = PALETTE_ANIM_START;
351 _cur_palette.count_dirty = PALETTE_ANIM_SIZE;
352 }
353}
354
361 TextColour GetContrastColour(PixelColour background, uint8_t threshold)
362{
363 Colour c = _cur_palette.palette[background.p];
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
375 struct ColourGradients
376{
377 using ColourGradient = std::array<PixelColour, SHADE_END>;
378
379 static inline std::array<ColourGradient, COLOUR_END> gradient{};
380};
381
388 PixelColour GetColourGradient(Colours colour, ColourShade shade)
389{
390 return ColourGradients::gradient[colour % COLOUR_END][shade % SHADE_END];
391}
392
399 void SetColourGradient(Colours colour, ColourShade shade, PixelColour 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.hpp
Base for all blitters.
GB
static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
Definition bitmath_func.hpp:32
BlitterFactory::GetCurrentBlitter
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
Definition factory.hpp:136
Blitter
How all blitters should look like.
Definition base.hpp:29
Blitter::UsePaletteAnimation
virtual Blitter::PaletteAnimation UsePaletteAnimation()=0
Check if the blitter uses palette animation at all.
Blitter::PaletteAnimation::None
@ None
No palette animation.
factory.hpp
Factory to 'query' all available blitters.
fileio_func.h
Functions for Standard In/Out file operations.
gfx_type.h
Types related to the graphics and/or input devices.
PALETTE_ANIM_SIZE
static constexpr uint8_t PALETTE_ANIM_SIZE
number of animated colours
Definition gfx_type.h:340
PALETTE_ANIM_START
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
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition gfx_type.h:307
landscape_type.h
Types related to the landscape.
CopyPalette
bool CopyPalette(Palette &local_palette, bool force_copy)
Copy the current palette if the palette was updated.
Definition palette.cpp:225
PALETTE_INDEX_END
const uint8_t PALETTE_INDEX_END
Palette index of end of defined palette.
Definition palette.cpp:88
PALETTE_INDEX_CC_END
const uint8_t PALETTE_INDEX_CC_END
Palette index of end of company colour remap area.
Definition palette.cpp:86
CrunchColour
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
_palette_mutex
static std::recursive_mutex _palette_mutex
To coordinate access to _cur_palette.
Definition palette.cpp:26
FindNearestColourReshadeIndex
static uint8_t FindNearestColourReshadeIndex(uint8_t b)
Find nearest company colour palette index for a brightness level.
Definition palette.cpp:127
CalculateColourDistance
static uint CalculateColourDistance(const Colour &col1, int r2, int g2, int b2)
Calculate distance between two colours.
Definition palette.cpp:73
PALETTE_INDEX_CC_START
const uint8_t PALETTE_INDEX_CC_START
Palette index of start of company colour remap area.
Definition palette.cpp:85
PALETTE_BITS
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
GetContrastColour
TextColour GetContrastColour(PixelColour background, uint8_t threshold)
Determine a contrasty text colour for a coloured background.
Definition palette.cpp:361
ReallyAdjustBrightness
Colour ReallyAdjustBrightness(Colour colour, int brightness)
Adjust brightness of colour.
Definition palette.cpp:177
PALETTE_INDEX_START
const uint8_t PALETTE_INDEX_START
Palette index of start of defined palette.
Definition palette.cpp:87
_cur_palette
Palette _cur_palette
Current palette.
Definition palette.cpp:24
GetNearestColourReshadeIndex
uint8_t GetNearestColourReshadeIndex(uint8_t b)
Get nearest colour palette index from a brightness level.
Definition palette.cpp:164
SetColourGradient
void SetColourGradient(Colours colour, ColourShade shade, PixelColour palette_index)
Set colour gradient palette index.
Definition palette.cpp:399
FindNearestColourIndex
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
GetNearestColourIndex
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
GetColourGradient
PixelColour GetColourGradient(Colours colour, ColourShade shade)
Get colour gradient palette index.
Definition palette.cpp:388
palette_func.h
Functions related to palettes.
palettes.h
The colour translation of the GRF palettes.
_palette
static const Palette _palette
Colour palette (DOS)
Definition palettes.h:13
EPV_CYCLES_DARK_WATER
static const uint EPV_CYCLES_DARK_WATER
Description of the length of the palette cycle animations.
Definition palettes.h:95
EPV_CYCLES_OIL_REFINERY
static const uint EPV_CYCLES_OIL_REFINERY
length of the oil refinery's fire animation
Definition palettes.h:97
EPV_CYCLES_LIGHTHOUSE
static const uint EPV_CYCLES_LIGHTHOUSE
length of the lighthouse/stadium animation
Definition palettes.h:96
EPV_CYCLES_GLITTER_WATER
static const uint EPV_CYCLES_GLITTER_WATER
length of the glittery water animation
Definition palettes.h:99
_extra_palette_values
static const ExtraPaletteValues _extra_palette_values
Actual palette animation tables.
Definition palettes.h:113
EPV_CYCLES_FIZZY_DRINK
static const uint EPV_CYCLES_FIZZY_DRINK
length of the fizzy drinks animation
Definition palettes.h:98
safeguards.h
A number of safeguards to prevent using unsafe methods.
_settings_game
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition settings.cpp:61
settings_type.h
Types related to global configuration settings.
stdafx.h
Definition of base types and functions in a cross-platform compatible way.
ColourGradients
Lookup table of colour shades for all 16 colour gradients.
Definition palette.cpp:376
ExtraPaletteValues
Description of tables for the palette animation.
Definition palettes.h:102
ExtraPaletteValues::fizzy_drink
Colour fizzy_drink[EPV_CYCLES_FIZZY_DRINK]
fizzy drinks
Definition palettes.h:107
ExtraPaletteValues::lighthouse
Colour lighthouse[EPV_CYCLES_LIGHTHOUSE]
lighthouse & stadium
Definition palettes.h:105
ExtraPaletteValues::dark_water_toyland
Colour dark_water_toyland[EPV_CYCLES_DARK_WATER]
dark blue water Toyland
Definition palettes.h:104
ExtraPaletteValues::glitter_water_toyland
Colour glitter_water_toyland[EPV_CYCLES_GLITTER_WATER]
glittery water Toyland
Definition palettes.h:109
ExtraPaletteValues::oil_refinery
Colour oil_refinery[EPV_CYCLES_OIL_REFINERY]
oil refinery
Definition palettes.h:106
GameCreationSettings::landscape
LandscapeType landscape
the landscape we're currently in
Definition settings_type.h:395
GameSettings::game_creation
GameCreationSettings game_creation
settings used during the creation of a game (map)
Definition settings_type.h:634
Palette
Information about the currently used palette.
Definition gfx_type.h:373
Palette::first_dirty
int first_dirty
The first dirty element.
Definition gfx_type.h:375
Palette::count_dirty
int count_dirty
The number of dirty elements.
Definition gfx_type.h:376
Palette::palette
Colour palette[256]
Current palette. Entry 0 has to be always fully transparent!
Definition gfx_type.h:374
PixelColour
Colour for pixel/line drawing.
Definition gfx_type.h:405
PixelColour::p
uint8_t p
Palette index.
Definition gfx_type.h:406
thread.h
Base of all threads.
lock
std::mutex lock
synchronization for playback status fields
Definition win32_m.cpp:35