OpenTTD Source 20241224-master-gf74b0cf984
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
57inline uint CrunchColour(uint c)
58{
59 return (c & PALETTE_BITS_MASK) | PALETTE_BITS_OR;
60}
61
70static uint CalculateColourDistance(const Colour &col1, int r2, int g2, int b2)
71{
72 /* Euclidean colour distance for sRGB based on https://en.wikipedia.org/wiki/Color_difference#sRGB */
73 int r = (int)col1.r - (int)r2;
74 int g = (int)col1.g - (int)g2;
75 int b = (int)col1.b - (int)b2;
76
77 int avgr = (col1.r + r2) / 2;
78 return ((2 + (avgr / 256.0)) * r * r) + (4 * g * g) + ((2 + ((255 - avgr) / 256.0)) * b * b);
79}
80
81/* Palette indexes for conversion. See docs/palettes/palette_key.png */
82const uint8_t PALETTE_INDEX_CC_START = 198;
84const uint8_t PALETTE_INDEX_START = 1;
85const uint8_t PALETTE_INDEX_END = 215;
86
94static uint8_t FindNearestColourIndex(uint8_t r, uint8_t g, uint8_t b)
95{
96 r = CrunchColour(r);
97 g = CrunchColour(g);
98 b = CrunchColour(b);
99
100 uint best_index = 0;
101 uint best_distance = UINT32_MAX;
102
103 for (uint i = PALETTE_INDEX_START; i < PALETTE_INDEX_CC_START; i++) {
104 if (uint distance = CalculateColourDistance(_palette.palette[i], r, g, b); distance < best_distance) {
105 best_index = i;
106 best_distance = distance;
107 }
108 }
109 /* There's a hole in the palette reserved for company colour remaps. */
110 for (uint i = PALETTE_INDEX_CC_END; i < PALETTE_INDEX_END; i++) {
111 if (uint distance = CalculateColourDistance(_palette.palette[i], r, g, b); distance < best_distance) {
112 best_index = i;
113 best_distance = distance;
114 }
115 }
116 return best_index;
117}
118
127uint8_t GetNearestColourIndex(uint8_t r, uint8_t g, uint8_t b)
128{
129 uint32_t key = (r >> PALETTE_SHIFT) | (g >> PALETTE_SHIFT) << PALETTE_BITS | (b >> PALETTE_SHIFT) << (PALETTE_BITS * 2);
130 if (_palette_lookup[key] == 0) _palette_lookup[key] = FindNearestColourIndex(r, g, b);
131 return _palette_lookup[key];
132}
133
134void DoPaletteAnimations();
135
136void GfxInitPalettes()
137{
138 std::lock_guard<std::recursive_mutex> lock(_palette_mutex);
139 memcpy(&_cur_palette, &_palette, sizeof(_cur_palette));
140 DoPaletteAnimations();
141}
142
152bool CopyPalette(Palette &local_palette, bool force_copy)
153{
154 std::lock_guard<std::recursive_mutex> lock(_palette_mutex);
155
156 if (!force_copy && _cur_palette.count_dirty == 0) return false;
157
158 local_palette = _cur_palette;
160
161 if (force_copy) {
162 local_palette.first_dirty = 0;
163 local_palette.count_dirty = 256;
164 }
165
166 return true;
167}
168
169#define EXTR(p, q) (((uint16_t)(palette_animation_counter * (p)) * (q)) >> 16)
170#define EXTR2(p, q) (((uint16_t)(~palette_animation_counter * (p)) * (q)) >> 16)
171
172void DoPaletteAnimations()
173{
174 std::lock_guard<std::recursive_mutex> lock(_palette_mutex);
175
176 /* Animation counter for the palette animation. */
177 static int palette_animation_counter = 0;
178 palette_animation_counter += 8;
179
181 const Colour *s;
183 Colour old_val[PALETTE_ANIM_SIZE];
184 const uint old_tc = palette_animation_counter;
185 uint j;
186
187 if (blitter != nullptr && blitter->UsePaletteAnimation() == Blitter::PALETTE_ANIMATION_NONE) {
188 palette_animation_counter = 0;
189 }
190
191 Colour *palette_pos = &_cur_palette.palette[PALETTE_ANIM_START]; // Points to where animations are taking place on the palette
192 /* Makes a copy of the current animation palette in old_val,
193 * so the work on the current palette could be compared, see if there has been any changes */
194 memcpy(old_val, palette_pos, sizeof(old_val));
195
196 /* Fizzy Drink bubbles animation */
197 s = ev->fizzy_drink;
198 j = EXTR2(512, EPV_CYCLES_FIZZY_DRINK);
199 for (uint i = 0; i != EPV_CYCLES_FIZZY_DRINK; i++) {
200 *palette_pos++ = s[j];
201 j++;
202 if (j == EPV_CYCLES_FIZZY_DRINK) j = 0;
203 }
204
205 /* Oil refinery fire animation */
206 s = ev->oil_refinery;
207 j = EXTR2(512, EPV_CYCLES_OIL_REFINERY);
208 for (uint i = 0; i != EPV_CYCLES_OIL_REFINERY; i++) {
209 *palette_pos++ = s[j];
210 j++;
211 if (j == EPV_CYCLES_OIL_REFINERY) j = 0;
212 }
213
214 /* Radio tower blinking */
215 {
216 uint8_t i = (palette_animation_counter >> 1) & 0x7F;
217 uint8_t v;
218
219 if (i < 0x3f) {
220 v = 255;
221 } else if (i < 0x4A || i >= 0x75) {
222 v = 128;
223 } else {
224 v = 20;
225 }
226 palette_pos->r = v;
227 palette_pos->g = 0;
228 palette_pos->b = 0;
229 palette_pos++;
230
231 i ^= 0x40;
232 if (i < 0x3f) {
233 v = 255;
234 } else if (i < 0x4A || i >= 0x75) {
235 v = 128;
236 } else {
237 v = 20;
238 }
239 palette_pos->r = v;
240 palette_pos->g = 0;
241 palette_pos->b = 0;
242 palette_pos++;
243 }
244
245 /* Handle lighthouse and stadium animation */
246 s = ev->lighthouse;
247 j = EXTR(256, EPV_CYCLES_LIGHTHOUSE);
248 for (uint i = 0; i != EPV_CYCLES_LIGHTHOUSE; i++) {
249 *palette_pos++ = s[j];
250 j++;
251 if (j == EPV_CYCLES_LIGHTHOUSE) j = 0;
252 }
253
254 /* Dark blue water */
255 s = (_settings_game.game_creation.landscape == LT_TOYLAND) ? ev->dark_water_toyland : ev->dark_water;
256 j = EXTR(320, EPV_CYCLES_DARK_WATER);
257 for (uint i = 0; i != EPV_CYCLES_DARK_WATER; i++) {
258 *palette_pos++ = s[j];
259 j++;
260 if (j == EPV_CYCLES_DARK_WATER) j = 0;
261 }
262
263 /* Glittery water */
264 s = (_settings_game.game_creation.landscape == LT_TOYLAND) ? ev->glitter_water_toyland : ev->glitter_water;
265 j = EXTR(128, EPV_CYCLES_GLITTER_WATER);
266 for (uint i = 0; i != EPV_CYCLES_GLITTER_WATER / 3; i++) {
267 *palette_pos++ = s[j];
268 j += 3;
270 }
271
272 if (blitter != nullptr && blitter->UsePaletteAnimation() == Blitter::PALETTE_ANIMATION_NONE) {
273 palette_animation_counter = old_tc;
274 } else if (_cur_palette.count_dirty == 0 && memcmp(old_val, &_cur_palette.palette[PALETTE_ANIM_START], sizeof(old_val)) != 0) {
275 /* Did we changed anything on the palette? Seems so. Mark it as dirty */
278 }
279}
280
287TextColour GetContrastColour(uint8_t background, uint8_t threshold)
288{
289 Colour c = _cur_palette.palette[background];
290 /* Compute brightness according to http://www.w3.org/TR/AERT#color-contrast.
291 * The following formula computes 1000 * brightness^2, with brightness being in range 0 to 255. */
292 uint sq1000_brightness = c.r * c.r * 299 + c.g * c.g * 587 + c.b * c.b * 114;
293 /* Compare with threshold brightness which defaults to 128 (50%) */
294 return sq1000_brightness < ((uint) threshold) * ((uint) threshold) * 1000 ? TC_WHITE : TC_BLACK;
295}
296
302{
303 using ColourGradient = std::array<uint8_t, SHADE_END>;
304
305 static inline std::array<ColourGradient, COLOUR_END> gradient{};
306};
307
314uint8_t GetColourGradient(Colours colour, ColourShade shade)
315{
316 return ColourGradients::gradient[colour % COLOUR_END][shade % SHADE_END];
317}
318
325void SetColourGradient(Colours colour, ColourShade shade, uint8_t palette_index)
326{
327 assert(colour < COLOUR_END);
328 assert(shade < SHADE_END);
329 ColourGradients::gradient[colour % COLOUR_END][shade % SHADE_END] = palette_index;
330}
Base for all blitters.
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.
@ PALETTE_ANIMATION_NONE
No palette animation.
Definition base.hpp:51
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:293
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:294
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition gfx_type.h:260
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:152
const uint8_t PALETTE_INDEX_END
Palette index of end of defined palette.
Definition palette.cpp:85
const uint8_t PALETTE_INDEX_CC_END
Palette index of end of company colour remap area.
Definition palette.cpp:83
void SetColourGradient(Colours colour, ColourShade shade, uint8_t palette_index)
Set colour gradient palette index.
Definition palette.cpp:325
uint CrunchColour(uint c)
Reduce bits per channel to PALETTE_BITS, and place value in the middle of the reduced range.
Definition palette.cpp:57
static std::recursive_mutex _palette_mutex
To coordinate access to _cur_palette.
Definition palette.cpp:26
uint8_t GetColourGradient(Colours colour, ColourShade shade)
Get colour gradient palette index.
Definition palette.cpp:314
static uint CalculateColourDistance(const Colour &col1, int r2, int g2, int b2)
Calculate distance between two colours.
Definition palette.cpp:70
const uint8_t PALETTE_INDEX_CC_START
Palette index of start of company colour remap area.
Definition palette.cpp:82
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:287
const uint8_t PALETTE_INDEX_START
Palette index of start of defined palette.
Definition palette.cpp:84
Palette _cur_palette
Current palette.
Definition palette.cpp:24
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:94
uint8_t GetNearestColourIndex(uint8_t r, uint8_t g, uint8_t b)
Get nearest colour palette index from an RGB colour.
Definition palette.cpp:127
Functions related to palettes.
The colour translation of the GRF palettes.
static const Palette _palette
Colour palette (DOS)
Definition palettes.h:15
static const uint EPV_CYCLES_DARK_WATER
Description of the length of the palette cycle animations.
Definition palettes.h:97
static const uint EPV_CYCLES_OIL_REFINERY
length of the oil refinery's fire animation
Definition palettes.h:99
static const uint EPV_CYCLES_LIGHTHOUSE
length of the lighthouse/stadium animation
Definition palettes.h:98
static const uint EPV_CYCLES_GLITTER_WATER
length of the glittery water animation
Definition palettes.h:101
static const ExtraPaletteValues _extra_palette_values
Actual palette animation tables.
Definition palettes.h:115
static const uint EPV_CYCLES_FIZZY_DRINK
length of the fizzy drinks animation
Definition palettes.h:100
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:302
Description of tables for the palette animation.
Definition palettes.h:104
Colour fizzy_drink[EPV_CYCLES_FIZZY_DRINK]
fizzy drinks
Definition palettes.h:109
Colour lighthouse[EPV_CYCLES_LIGHTHOUSE]
lighthouse & stadium
Definition palettes.h:107
Colour dark_water_toyland[EPV_CYCLES_DARK_WATER]
dark blue water Toyland
Definition palettes.h:106
Colour glitter_water_toyland[EPV_CYCLES_GLITTER_WATER]
glittery water Toyland
Definition palettes.h:111
Colour oil_refinery[EPV_CYCLES_OIL_REFINERY]
oil refinery
Definition palettes.h:108
uint8_t 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:328
int first_dirty
The first dirty element.
Definition gfx_type.h:330
int count_dirty
The number of dirty elements.
Definition gfx_type.h:331
Colour palette[256]
Current palette. Entry 0 has to be always fully transparent!
Definition gfx_type.h:329
Base of all threads.
Structure to access the alpha, red, green, and blue channels from a 32 bit number.
Definition gfx_type.h:165
uint8_t b
colour channels in BE order
Definition gfx_type.h:171
std::mutex lock
synchronization for playback status fields
Definition win32_m.cpp:35