|
OpenTTD Source 20251116-master-g21329071df
|
Handling of palettes. More...
#include "stdafx.h"#include "blitter/base.hpp"#include "blitter/factory.hpp"#include "fileio_func.h"#include "gfx_type.h"#include "landscape_type.h"#include "palette_func.h"#include "settings_type.h"#include "thread.h"#include "table/palettes.h"#include "safeguards.h"Go to the source code of this file.
Data Structures | |
| struct | ColourGradients |
| Lookup table of colour shades for all 16 colour gradients. More... | |
Macros | |
| #define | EXTR(p, q) (((uint16_t)(palette_animation_counter * (p)) * (q)) >> 16) |
| #define | EXTR2(p, q) (((uint16_t)(~palette_animation_counter * (p)) * (q)) >> 16) |
Typedefs | |
| using | PaletteLookup = std::array< uint8_t, 1U<<(PALETTE_BITS *3)> |
| using | ReshadeLookup = std::array< uint8_t, 1U<< PALETTE_BITS > |
Functions | |
| uint | CrunchColour (uint c) |
| Reduce bits per channel to PALETTE_BITS, and place value in the middle of the reduced range. | |
| static uint | CalculateColourDistance (const Colour &col1, int r2, int g2, int b2) |
| Calculate distance between two colours. | |
| static uint8_t | FindNearestColourIndex (uint8_t r, uint8_t g, uint8_t b) |
| Find nearest colour palette index for a 32bpp pixel. | |
| static uint8_t | FindNearestColourReshadeIndex (uint8_t b) |
| Find nearest company colour palette index for a brightness level. | |
| uint8_t | GetNearestColourIndex (uint8_t r, uint8_t g, uint8_t b) |
| Get nearest colour palette index from an RGB colour. | |
| uint8_t | GetNearestColourReshadeIndex (uint8_t b) |
| Get nearest colour palette index from a brightness level. | |
| Colour | ReallyAdjustBrightness (Colour colour, int brightness) |
| Adjust brightness of colour. | |
| void | DoPaletteAnimations () |
| void | GfxInitPalettes () |
| bool | CopyPalette (Palette &local_palette, bool force_copy) |
| Copy the current palette if the palette was updated. | |
| TextColour | GetContrastColour (PixelColour background, uint8_t threshold) |
| Determine a contrasty text colour for a coloured background. | |
| PixelColour | GetColourGradient (Colours colour, ColourShade shade) |
| Get colour gradient palette index. | |
| void | SetColourGradient (Colours colour, ColourShade shade, PixelColour palette_index) |
| Set colour gradient palette index. | |
Variables | |
| Palette | _cur_palette |
| Current palette. | |
| static std::recursive_mutex | _palette_mutex |
| To coordinate access to _cur_palette. | |
| const uint | PALETTE_BITS = 6 |
| PALETTE_BITS reduces the bits-per-channel of 32bpp graphics data to allow faster palette lookups from a smaller lookup table. | |
| const uint | PALETTE_SHIFT = 8 - PALETTE_BITS |
| const uint | PALETTE_BITS_MASK = ((1U << PALETTE_BITS) - 1) << PALETTE_SHIFT |
| const uint | PALETTE_BITS_OR = (1U << (PALETTE_SHIFT - 1)) |
| static PaletteLookup | _palette_lookup {} |
| static ReshadeLookup | _reshade_lookup {} |
| const uint8_t | PALETTE_INDEX_CC_START = 198 |
| Palette index of start of company colour remap area. | |
| const uint8_t | PALETTE_INDEX_CC_END = PALETTE_INDEX_CC_START + 8 |
| Palette index of end of company colour remap area. | |
| const uint8_t | PALETTE_INDEX_START = 1 |
| Palette index of start of defined palette. | |
| const uint8_t | PALETTE_INDEX_END = 215 |
| Palette index of end of defined palette. | |
Handling of palettes.
Definition in file palette.cpp.
| #define EXTR | ( | p, | |
| q | |||
| ) | (((uint16_t)(palette_animation_counter * (p)) * (q)) >> 16) |
Definition at line 242 of file palette.cpp.
| #define EXTR2 | ( | p, | |
| q | |||
| ) | (((uint16_t)(~palette_animation_counter * (p)) * (q)) >> 16) |
Definition at line 243 of file palette.cpp.
| using PaletteLookup = std::array<uint8_t, 1U << (PALETTE_BITS * 3)> |
Definition at line 44 of file palette.cpp.
| using ReshadeLookup = std::array<uint8_t, 1U << PALETTE_BITS> |
Definition at line 47 of file palette.cpp.
|
static |
Calculate distance between two colours.
| col1 | First colour. |
| r2 | Red component of second colour. |
| g2 | Green component of second colour. |
| b2 | Blue component of second colour. |
Definition at line 73 of file palette.cpp.
Referenced by FindNearestColourIndex(), and FindNearestColourReshadeIndex().
| bool CopyPalette | ( | Palette & | local_palette, |
| bool | force_copy | ||
| ) |
Copy the current palette if the palette was updated.
Used by video-driver to get a current up-to-date version of the palette, to avoid two threads accessing the same piece of memory (with a good chance one is already updating the palette while the other is drawing based on it).
| local_palette | The location to copy the palette to. |
| force_copy | Whether to ignore if there is an update for the palette. |
Definition at line 225 of file palette.cpp.
References _cur_palette, _palette_mutex, Palette::count_dirty, Palette::first_dirty, and lock.
Referenced by VideoDriver_SDL_OpenGL::AllocateBackingStore(), VideoDriver_SDL_Base::CheckPaletteAnim(), VideoDriver_Win32Base::CheckPaletteAnim(), and VideoDriver_SDL_Base::ClientSizeChanged().
|
inline |
Reduce bits per channel to PALETTE_BITS, and place value in the middle of the reduced range.
This is to counteract the information lost between bright and dark pixels, e.g if PALETTE_BITS was 2: 0 - 63 -> 32 64 - 127 -> 96 128 - 191 -> 160 192 - 255 -> 224
| c | 8 bit colour component. |
Definition at line 60 of file palette.cpp.
Referenced by FindNearestColourIndex(), and FindNearestColourReshadeIndex().
| void DoPaletteAnimations | ( | ) |
Definition at line 245 of file palette.cpp.
|
static |
Find nearest colour palette index for a 32bpp pixel.
| r | Red component. |
| g | Green component. |
| b | Blue component. |
Definition at line 97 of file palette.cpp.
References _palette, CalculateColourDistance(), CrunchColour(), Palette::palette, PALETTE_INDEX_CC_END, PALETTE_INDEX_CC_START, PALETTE_INDEX_END, and PALETTE_INDEX_START.
Referenced by GetNearestColourIndex().
|
static |
Find nearest company colour palette index for a brightness level.
| pixel | Pixel to find. |
Definition at line 127 of file palette.cpp.
References _palette, CalculateColourDistance(), CrunchColour(), Palette::palette, PALETTE_INDEX_CC_END, and PALETTE_INDEX_CC_START.
Referenced by GetNearestColourReshadeIndex().
| PixelColour GetColourGradient | ( | Colours | colour, |
| ColourShade | shade | ||
| ) |
Get colour gradient palette index.
| colour | Colour. |
| shade | Shade level from 1 to 7. |
Definition at line 388 of file palette.cpp.
Referenced by BuildOwnerLegend(), NWidgetMatrix::Draw(), NWidgetBackground::Draw(), NWidgetScrollbar::Draw(), NWidgetLeaf::Draw(), BaseSettingEntry::Draw(), DrawArrowButtons(), DrawBoolButton(), ButtonLine::DrawButtons(), DrawCaption(), LinkGraphOverlay::DrawContent(), NetworkContentListWindow::DrawDetails(), DrawDropDownButton(), DrawEngineList(), DrawFrame(), DrawFrameRect(), VehicleGroupWindow::DrawGroupInfo(), DrawHorizontalScrollbar(), DrawMatrix(), DrawResizeBox(), DrawSliderWidget(), LinkGraphOverlay::DrawStationDots(), DrawTrainDetails(), DrawUpDownButtons(), DrawVehicleRefitWindow(), DrawVerticalScrollbar(), DepotWindow::DrawWidget(), BaseGraphWindow::DrawWidget(), PerformanceRatingDetailWindow::DrawWidget(), VehicleGroupWindow::DrawWidget(), NewGRFWindow::DrawWidget(), OrdersWindow::DrawWidget(), PickerWindow::DrawWidget(), GetDrawStringCompanyColour(), HighlightDragPosition(), IsValidConsoleColour(), and BaseCompanyGraphWindow::UpdateStatistics().
| TextColour GetContrastColour | ( | PixelColour | background, |
| uint8_t | threshold | ||
| ) |
Determine a contrasty text colour for a coloured background.
| background | Background colour. |
| threshold | Background colour brightness threshold below which the background is considered dark and TC_WHITE is returned, range: 0 - 255, default 128. |
Definition at line 361 of file palette.cpp.
References _cur_palette, PixelColour::p, and Palette::palette.
Referenced by LinkGraphLegendWindow::DrawWidget(), and StationsWndShowStationRating().
| uint8_t GetNearestColourIndex | ( | uint8_t | r, |
| uint8_t | g, | ||
| uint8_t | b | ||
| ) |
Get nearest colour palette index from an RGB colour.
A search is performed if this colour is not already in the lookup table.
| r | Red component. |
| g | Green component. |
| b | Blue component. |
Definition at line 151 of file palette.cpp.
References FindNearestColourIndex(), and PALETTE_BITS.
Referenced by Convert32bppTo8bpp(), Blitter_32bppSimple::Draw(), Blitter_32bppOptimized::Draw(), and Blitter_40bppAnim::Draw().
| uint8_t GetNearestColourReshadeIndex | ( | uint8_t | b | ) |
Get nearest colour palette index from a brightness level.
A search is performed if this brightness level is not already in the lookup table.
| b | Brightness component. |
Definition at line 164 of file palette.cpp.
References FindNearestColourReshadeIndex().
Referenced by Convert32bppTo8bpp().
| void GfxInitPalettes | ( | ) |
Definition at line 209 of file palette.cpp.
| Colour ReallyAdjustBrightness | ( | Colour | colour, |
| int | brightness | ||
| ) |
Adjust brightness of colour.
| colour | Colour to adjust. |
| brightness | Brightness to apply to colour. |
Definition at line 177 of file palette.cpp.
References GB().
| void SetColourGradient | ( | Colours | colour, |
| ColourShade | shade, | ||
| PixelColour | palette_index | ||
| ) |
Set colour gradient palette index.
| colour | Colour. |
| shade | Shade level from 1 to 7. |
| palette_index | Palette index to set. |
Definition at line 399 of file palette.cpp.
Referenced by HandleBootstrap(), and SetupColoursAndInitialWindow().
| Palette _cur_palette |
Current palette.
Definition at line 24 of file palette.cpp.
Referenced by Convert32bppTo8bpp(), CopyPalette(), DrawSpriteToRgbaBuffer(), GetContrastColour(), Blitter_32bppBase::LookupColourInPalette(), MakeLargeWorldScreenshot(), MakeMinimapWorldScreenshot(), and MakeSmallScreenshot().
|
static |
Definition at line 45 of file palette.cpp.
|
static |
To coordinate access to _cur_palette.
Definition at line 26 of file palette.cpp.
Referenced by CopyPalette().
|
static |
Definition at line 48 of file palette.cpp.
| const uint PALETTE_BITS = 6 |
PALETTE_BITS reduces the bits-per-channel of 32bpp graphics data to allow faster palette lookups from a smaller lookup table.
6 bpc is chosen as this results in a palette lookup table of 256KiB with adequate fidelity. In contrast, a 5 bpc lookup table would be 32KiB, and 7 bpc would be 2MiB.
Values in the table are filled as they are first encountered – larger lookup table means more colour distance calculations, and is therefore slower.
Definition at line 38 of file palette.cpp.
Referenced by GetNearestColourIndex().
| const uint PALETTE_BITS_MASK = ((1U << PALETTE_BITS) - 1) << PALETTE_SHIFT |
Definition at line 40 of file palette.cpp.
| const uint PALETTE_BITS_OR = (1U << (PALETTE_SHIFT - 1)) |
Definition at line 41 of file palette.cpp.
| const uint8_t PALETTE_INDEX_CC_END = PALETTE_INDEX_CC_START + 8 |
Palette index of end of company colour remap area.
Definition at line 86 of file palette.cpp.
Referenced by FindNearestColourIndex(), and FindNearestColourReshadeIndex().
| const uint8_t PALETTE_INDEX_CC_START = 198 |
Palette index of start of company colour remap area.
Definition at line 85 of file palette.cpp.
Referenced by FindNearestColourIndex(), and FindNearestColourReshadeIndex().
| const uint8_t PALETTE_INDEX_END = 215 |
Palette index of end of defined palette.
Definition at line 88 of file palette.cpp.
Referenced by FindNearestColourIndex().
| const uint8_t PALETTE_INDEX_START = 1 |
Palette index of start of defined palette.
Definition at line 87 of file palette.cpp.
Referenced by FindNearestColourIndex().
| const uint PALETTE_SHIFT = 8 - PALETTE_BITS |
Definition at line 39 of file palette.cpp.