40const uint PALETTE_BITS_MASK = ((1U <<
PALETTE_BITS) - 1) << PALETTE_SHIFT;
41const uint PALETTE_BITS_OR = (1U << (PALETTE_SHIFT - 1));
44using PaletteLookup = std::array<uint8_t, 1U << (
PALETTE_BITS * 3)>;
45static PaletteLookup _palette_lookup{};
47using ReshadeLookup = std::array<uint8_t, 1U << PALETTE_BITS>;
48static ReshadeLookup _reshade_lookup{};
62 return (c & PALETTE_BITS_MASK) | PALETTE_BITS_OR;
76 int r = (int)col1.r - (
int)r2;
77 int g = (int)col1.g - (
int)g2;
78 int b = (int)col1.b - (
int)b2;
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);
104 uint best_distance = UINT32_MAX;
109 best_distance = distance;
116 best_distance = distance;
132 uint best_distance = UINT32_MAX;
137 best_distance = distance;
153 uint32_t key = (r >> PALETTE_SHIFT) | (g >> PALETTE_SHIFT) <<
PALETTE_BITS | (b >> PALETTE_SHIFT) << (
PALETTE_BITS * 2);
155 return _palette_lookup[key];
166 uint32_t key = (b >> PALETTE_SHIFT);
168 return _reshade_lookup[key];
179 if (brightness == DEFAULT_BRIGHTNESS)
return colour;
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;
184 uint16_t r =
GB(combined, 39, 9);
185 uint16_t g =
GB(combined, 23, 9);
186 uint16_t b =
GB(combined, 7, 9);
188 if ((combined & 0x800080008000L) == 0L) {
189 return Colour(r, g, b, colour.a);
194 if (r > 255) ob += r - 255;
195 if (g > 255) ob += g - 255;
196 if (b > 255) ob += b - 255;
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),
207void DoPaletteAnimations();
209void GfxInitPalettes()
213 DoPaletteAnimations();
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)
245void DoPaletteAnimations()
250 static int palette_animation_counter = 0;
251 palette_animation_counter += 8;
256 const uint old_tc = palette_animation_counter;
260 palette_animation_counter = 0;
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();
274 *palette_pos++ = s[j];
283 *palette_pos++ = s[j];
290 uint8_t i = (palette_animation_counter >> 1) & 0x7F;
295 }
else if (i < 0x4A || i >= 0x75) {
308 }
else if (i < 0x4A || i >= 0x75) {
323 *palette_pos++ = s[j];
332 *palette_pos++ = s[j];
341 *palette_pos++ = s[j];
347 palette_animation_counter = old_tc;
366 uint sq1000_brightness = c.r * c.r * 299 + c.g * c.g * 587 + c.b * c.b * 114;
368 return sq1000_brightness < ((uint) threshold) * ((uint) threshold) * 1000 ? TC_WHITE : TC_BLACK;
377 using ColourGradient = std::array<PixelColour, SHADE_END>;
379 static inline std::array<ColourGradient, COLOUR_END> gradient{};
390 return ColourGradients::gradient[colour % COLOUR_END][shade % SHADE_END];
401 assert(colour < COLOUR_END);
402 assert(shade < SHADE_END);
403 ColourGradients::gradient[colour % COLOUR_END][shade % SHADE_END] = palette_index;
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).
How all blitters should look like.
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
static constexpr uint8_t PALETTE_ANIM_START
Index in the _palettes array from which all animations are taking places (table/palettes....
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Types related to the landscape.
bool CopyPalette(Palette &local_palette, bool force_copy)
Copy the current palette if the palette was updated.
const uint8_t PALETTE_INDEX_END
Palette index of end of defined palette.
const uint8_t PALETTE_INDEX_CC_END
Palette index of end of company colour remap area.
uint CrunchColour(uint c)
Reduce bits per channel to PALETTE_BITS, and place value in the middle of the reduced range.
static std::recursive_mutex _palette_mutex
To coordinate access to _cur_palette.
static uint8_t FindNearestColourReshadeIndex(uint8_t b)
Find nearest company colour palette index for a brightness level.
static uint CalculateColourDistance(const Colour &col1, int r2, int g2, int b2)
Calculate distance between two colours.
const uint8_t PALETTE_INDEX_CC_START
Palette index of start of company colour remap area.
const uint PALETTE_BITS
PALETTE_BITS reduces the bits-per-channel of 32bpp graphics data to allow faster palette lookups from...
TextColour GetContrastColour(PixelColour background, uint8_t threshold)
Determine a contrasty text colour for a coloured background.
Colour ReallyAdjustBrightness(Colour colour, int brightness)
Adjust brightness of colour.
const uint8_t PALETTE_INDEX_START
Palette index of start of defined palette.
Palette _cur_palette
Current palette.
uint8_t GetNearestColourReshadeIndex(uint8_t b)
Get nearest colour palette index from a brightness level.
void SetColourGradient(Colours colour, ColourShade shade, PixelColour palette_index)
Set colour gradient palette index.
static uint8_t FindNearestColourIndex(uint8_t r, uint8_t g, uint8_t b)
Find nearest colour palette index for a 32bpp pixel.
uint8_t GetNearestColourIndex(uint8_t r, uint8_t g, uint8_t b)
Get nearest colour palette index from an RGB colour.
PixelColour GetColourGradient(Colours colour, ColourShade shade)
Get colour gradient palette index.
Functions related to palettes.
The colour translation of the GRF palettes.
static const Palette _palette
Colour palette (DOS)
static const uint EPV_CYCLES_DARK_WATER
Description of the length of the palette cycle animations.
static const uint EPV_CYCLES_OIL_REFINERY
length of the oil refinery's fire animation
static const uint EPV_CYCLES_LIGHTHOUSE
length of the lighthouse/stadium animation
static const uint EPV_CYCLES_GLITTER_WATER
length of the glittery water animation
static const ExtraPaletteValues _extra_palette_values
Actual palette animation tables.
static const uint EPV_CYCLES_FIZZY_DRINK
length of the fizzy drinks animation
A number of safeguards to prevent using unsafe methods.
GameSettings _settings_game
Game settings of a running game or the scenario editor.
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.
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.
int first_dirty
The first dirty element.
int count_dirty
The number of dirty elements.
Colour palette[256]
Current palette. Entry 0 has to be always fully transparent!
Colour for pixel/line drawing.
std::mutex lock
synchronization for playback status fields