Update: Translations from eints
[openttd-github.git] / src / palette.cpp
blob3f617b9f922d71e8c510da9067dbf2e3cb8f6ddb
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 */
8 /** @file palette.cpp Handling of palettes. */
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"
20 #include "table/palettes.h"
22 #include "safeguards.h"
24 Palette _cur_palette;
26 static std::recursive_mutex _palette_mutex; ///< To coordinate access to _cur_palette.
28 /**
29 * PALETTE_BITS reduces the bits-per-channel of 32bpp graphics data to allow faster palette lookups from
30 * a smaller lookup table.
32 * 6 bpc is chosen as this results in a palette lookup table of 256KiB with adequate fidelty.
33 * In constract, a 5 bpc lookup table would be 32KiB, and 7 bpc would be 2MiB.
35 * Values in the table are filled as they are first encountered -- larger lookup table means more colour
36 * distance calculations, and is therefore slower.
38 const uint PALETTE_BITS = 6;
39 const uint PALETTE_SHIFT = 8 - PALETTE_BITS;
40 const uint PALETTE_BITS_MASK = ((1U << PALETTE_BITS) - 1) << PALETTE_SHIFT;
41 const uint PALETTE_BITS_OR = (1U << (PALETTE_SHIFT - 1));
43 /* Palette and reshade lookup table. */
44 using PaletteLookup = std::array<uint8_t, 1U << (PALETTE_BITS * 3)>;
45 static PaletteLookup _palette_lookup{};
47 /**
48 * Reduce bits per channel to PALETTE_BITS, and place value in the middle of the reduced range.
49 * This is to counteract the information lost between bright and dark pixels, e.g if PALETTE_BITS was 2:
50 * 0 - 63 -> 32
51 * 64 - 127 -> 96
52 * 128 - 191 -> 160
53 * 192 - 255 -> 224
54 * @param c 8 bit colour component.
55 * @returns Colour component reduced to PALETTE_BITS.
57 inline uint CrunchColour(uint c)
59 return (c & PALETTE_BITS_MASK) | PALETTE_BITS_OR;
62 /**
63 * Calculate distance between two colours.
64 * @param col1 First colour.
65 * @param r2 Red component of second colour.
66 * @param g2 Green component of second colour.
67 * @param b2 Blue component of second colour.
68 * @returns Euclidean distance between first and second colour.
70 static uint CalculateColourDistance(const Colour &col1, int r2, int g2, int b2)
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;
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);
81 /* Palette indexes for conversion. See docs/palettes/palette_key.png */
82 const uint8_t PALETTE_INDEX_CC_START = 198; ///< Palette index of start of company colour remap area.
83 const uint8_t PALETTE_INDEX_CC_END = PALETTE_INDEX_CC_START + 8; ///< Palette index of end of company colour remap area.
84 const uint8_t PALETTE_INDEX_START = 1; ///< Palette index of start of defined palette.
85 const uint8_t PALETTE_INDEX_END = 215; ///< Palette index of end of defined palette.
87 /**
88 * Find nearest colour palette index for a 32bpp pixel.
89 * @param r Red component.
90 * @param g Green component.
91 * @param b Blue component.
92 * @returns palette index of nearest colour.
94 static uint8_t FindNearestColourIndex(uint8_t r, uint8_t g, uint8_t b)
96 r = CrunchColour(r);
97 g = CrunchColour(g);
98 b = CrunchColour(b);
100 uint best_index = 0;
101 uint best_distance = UINT32_MAX;
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;
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;
116 return best_index;
120 * Get nearest colour palette index from an RGB colour.
121 * A search is performed if this colour is not already in the lookup table.
122 * @param r Red component.
123 * @param g Green component.
124 * @param b Blue component.
125 * @returns nearest colour palette index.
127 uint8_t GetNearestColourIndex(uint8_t r, uint8_t g, uint8_t b)
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];
134 void DoPaletteAnimations();
136 void GfxInitPalettes()
138 std::lock_guard<std::recursive_mutex> lock(_palette_mutex);
139 memcpy(&_cur_palette, &_palette, sizeof(_cur_palette));
140 DoPaletteAnimations();
144 * Copy the current palette if the palette was updated.
145 * Used by video-driver to get a current up-to-date version of the palette,
146 * to avoid two threads accessing the same piece of memory (with a good chance
147 * one is already updating the palette while the other is drawing based on it).
148 * @param local_palette The location to copy the palette to.
149 * @param force_copy Whether to ignore if there is an update for the palette.
150 * @return True iff a copy was done.
152 bool CopyPalette(Palette &local_palette, bool force_copy)
154 std::lock_guard<std::recursive_mutex> lock(_palette_mutex);
156 if (!force_copy && _cur_palette.count_dirty == 0) return false;
158 local_palette = _cur_palette;
159 _cur_palette.count_dirty = 0;
161 if (force_copy) {
162 local_palette.first_dirty = 0;
163 local_palette.count_dirty = 256;
166 return true;
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)
172 void DoPaletteAnimations()
174 std::lock_guard<std::recursive_mutex> lock(_palette_mutex);
176 /* Animation counter for the palette animation. */
177 static int palette_animation_counter = 0;
178 palette_animation_counter += 8;
180 Blitter *blitter = BlitterFactory::GetCurrentBlitter();
181 const Colour *s;
182 const ExtraPaletteValues *ev = &_extra_palette_values;
183 Colour old_val[PALETTE_ANIM_SIZE];
184 const uint old_tc = palette_animation_counter;
185 uint j;
187 if (blitter != nullptr && blitter->UsePaletteAnimation() == Blitter::PALETTE_ANIMATION_NONE) {
188 palette_animation_counter = 0;
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));
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;
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;
214 /* Radio tower blinking */
216 uint8_t i = (palette_animation_counter >> 1) & 0x7F;
217 uint8_t v;
219 if (i < 0x3f) {
220 v = 255;
221 } else if (i < 0x4A || i >= 0x75) {
222 v = 128;
223 } else {
224 v = 20;
226 palette_pos->r = v;
227 palette_pos->g = 0;
228 palette_pos->b = 0;
229 palette_pos++;
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;
239 palette_pos->r = v;
240 palette_pos->g = 0;
241 palette_pos->b = 0;
242 palette_pos++;
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;
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;
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;
269 if (j >= EPV_CYCLES_GLITTER_WATER) j -= EPV_CYCLES_GLITTER_WATER;
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 */
276 _cur_palette.first_dirty = PALETTE_ANIM_START;
277 _cur_palette.count_dirty = PALETTE_ANIM_SIZE;
282 * Determine a contrasty text colour for a coloured background.
283 * @param background Background colour.
284 * @param threshold Background colour brightness threshold below which the background is considered dark and TC_WHITE is returned, range: 0 - 255, default 128.
285 * @return TC_BLACK or TC_WHITE depending on what gives a better contrast.
287 TextColour GetContrastColour(uint8_t background, uint8_t threshold)
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;
298 * Lookup table of colour shades for all 16 colour gradients.
299 * 8 colours per gradient from darkest (0) to lightest (7)
301 struct ColourGradients
303 using ColourGradient = std::array<uint8_t, SHADE_END>;
305 static inline std::array<ColourGradient, COLOUR_END> gradient{};
309 * Get colour gradient palette index.
310 * @param colour Colour.
311 * @param shade Shade level from 1 to 7.
312 * @returns palette index of colour.
314 uint8_t GetColourGradient(Colours colour, ColourShade shade)
316 return ColourGradients::gradient[colour % COLOUR_END][shade % SHADE_END];
320 * Set colour gradient palette index.
321 * @param colour Colour.
322 * @param shade Shade level from 1 to 7.
323 * @param palette_index Palette index to set.
325 void SetColourGradient(Colours colour, ColourShade shade, uint8_t palette_index)
327 assert(colour < COLOUR_END);
328 assert(shade < SHADE_END);
329 ColourGradients::gradient[colour % COLOUR_END][shade % SHADE_END] = palette_index;