OpenTTD Source: src/palette_func.h File Reference

OpenTTD Source 15.3

Enumerations | Functions | Variables
palette_func.h File Reference

Functions related to palettes. More...

`#include "core/enum_type.hpp"`
`#include "gfx_type.h"`
`#include "strings_type.h"`
`#include "string_type.h"`

Go to the source code of this file.

## Enumerations
enum | ColourShade : uint8_t { SHADE_BEGIN = 0 , SHADE_DARKEST = SHADE_BEGIN , SHADE_DARKER , SHADE_DARK , SHADE_NORMAL , SHADE_LIGHT , SHADE_LIGHTER , SHADE_LIGHTEST , SHADE_LIGHTEREST , SHADE_END }

## Functions
bool | CopyPalette (Palette &local_palette, bool force_copy=false)
| Copy the current palette if the palette was updated.
void | GfxInitPalettes ()
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.
uint8_t | GetNearestColourIndex (const Colour colour)
Colour | ReallyAdjustBrightness (Colour colour, int brightness)
| Adjust brightness of colour.
static Colour | AdjustBrightness (Colour colour, uint8_t brightness)
static uint8_t | GetColourBrightness (Colour colour)
| Get the brightness of a colour.
bool | IsValidColours (Colours colours)
| Checks if a Colours value is valid.
TextColour | GetContrastColour (PixelColour background, uint8_t threshold=128)
| 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_colour)
| Set colour gradient palette index.
constexpr PixelColour | GREY_SCALE (uint8_t level)
| Return the colour for a particular greyscale level.

## Variables
Palette | _cur_palette
| Current palette.
static constexpr int | DEFAULT_BRIGHTNESS = 128
static constexpr PixelColour | PC_BLACK {GREY_SCALE(1)}
| Black palette colour.
static constexpr PixelColour | PC_DARK_GREY {GREY_SCALE(6)}
| Dark grey palette colour.
static constexpr PixelColour | PC_GREY {GREY_SCALE(10)}
| Grey palette colour.
static constexpr PixelColour | PC_WHITE {GREY_SCALE(15)}
| White palette colour.
static constexpr PixelColour | PC_VERY_DARK_RED {0xB2}
| Almost-black red palette colour.
static constexpr PixelColour | PC_DARK_RED {0xB4}
| Dark red palette colour.
static constexpr PixelColour | PC_RED {0xB8}
| Red palette colour.
static constexpr PixelColour | PC_VERY_DARK_BROWN {0x56}
| Almost-black brown palette colour.
static constexpr PixelColour | PC_ORANGE {0xC2}
| Orange palette colour.
static constexpr PixelColour | PC_YELLOW {0xBF}
| Yellow palette colour.
static constexpr PixelColour | PC_LIGHT_YELLOW {0x44}
| Light yellow palette colour.
static constexpr PixelColour | PC_VERY_LIGHT_YELLOW {0x45}
| Almost-white yellow palette colour.
static constexpr PixelColour | PC_GREEN {0xD0}
| Green palette colour.
static constexpr PixelColour | PC_VERY_DARK_BLUE {0x9A}
| Almost-black blue palette colour.
static constexpr PixelColour | PC_DARK_BLUE {0x9D}
| Dark blue palette colour.
static constexpr PixelColour | PC_LIGHT_BLUE {0x98}
| Light blue palette colour.
static constexpr PixelColour | PC_ROUGH_LAND {0x52}
| Dark green palette colour for rough land.
static constexpr PixelColour | PC_GRASS_LAND {0x54}
| Dark green palette colour for grass land.
static constexpr PixelColour | PC_BARE_LAND {0x37}
| Brown palette colour for bare land.
static constexpr PixelColour | PC_RAINFOREST {0x5C}
| Pale green palette colour for rainforest.
static constexpr PixelColour | PC_FIELDS {0x25}
| Light brown palette colour for fields.
static constexpr PixelColour | PC_TREES {0x57}
| Green palette colour for trees.
static constexpr PixelColour | PC_WATER {0xC9}
| Dark blue palette colour for water.

## Detailed Description

Functions related to palettes.

Definition in file palette_func.h.

## Enumeration Type Documentation

## ◆ ColourShade

enum ColourShade : uint8_t

Definition at line 72 of file palette_func.h.

## Function Documentation

## ◆ AdjustBrightness()

static Colour AdjustBrightness | ( | Colour | colour,
|  | uint8_t | brightness
| ) |  |
| inlinestatic

Definition at line 35 of file palette_func.h.

## ◆ CopyPalette()

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).

Parameters

  local_palette | The location to copy the palette to.
  force_copy | Whether to ignore if there is an update for the palette.

Returns
  True iff a copy was done.

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().

## ◆ GetColourBrightness()

static uint8_t GetColourBrightness | ( | Colour | colour | ) |
| inlinestatic

Get the brightness of a colour.

This uses the maximum value of R, G or B channel, instead of perceptual brightness.

Parameters

  colour | Colour to get the brightness of.

Returns
  Brightness of colour.

Definition at line 49 of file palette_func.h.

Referenced by Blitter_40bppAnim::Draw(), and Blitter_40bppAnim::DrawColourMappingRect().

## ◆ GetColourGradient()

PixelColour GetColourGradient | ( | Colours | colour,
|  | ColourShade | shade
| ) |  |

Get colour gradient palette index.

Parameters

  colour | Colour.
  shade | Shade level from 1 to 7.

Returns
  palette index of colour.

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().

## ◆ GetContrastColour()

TextColour GetContrastColour | ( | PixelColour | background,
|  | uint8_t | threshold
| ) |  |

Determine a contrasty text colour for a coloured background.

Parameters

  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.

Returns
  TC_BLACK or TC_WHITE depending on what gives a better contrast.

Definition at line 361 of file palette.cpp.

References _cur_palette, PixelColour::p, and Palette::palette.

Referenced by LinkGraphLegendWindow::DrawWidget(), and StationsWndShowStationRating().

## ◆ GetNearestColourIndex() [1/2]

uint8_t GetNearestColourIndex | ( | const Colour | colour | ) |
| inline

Definition at line 26 of file palette_func.h.

## ◆ GetNearestColourIndex() [2/2]

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.

Parameters

  r | Red component.
  g | Green component.
  b | Blue component.

Returns
  nearest colour palette index.

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().

## ◆ GetNearestColourReshadeIndex()

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.

Parameters

  b | Brightness component.

Returns
  nearest colour palette index.

Definition at line 164 of file palette.cpp.

References FindNearestColourReshadeIndex().

Referenced by Convert32bppTo8bpp().

## ◆ GfxInitPalettes()

void GfxInitPalettes | ( |  | ) |

Definition at line 209 of file palette.cpp.

## ◆ GREY_SCALE()

constexpr PixelColour GREY_SCALE | ( | uint8_t | level | ) |
| inlineconstexpr

Return the colour for a particular greyscale level.

Parameters

  level | Intensity, 0 = black, 15 = white

Returns
  colour

Definition at line 94 of file palette_func.h.

Referenced by NetworkClientListWindow::DrawWidget().

## ◆ IsValidColours()

bool IsValidColours | ( | Colours | colours | ) |
| inline

Checks if a Colours value is valid.

Parameters

  colours | The value to check

Returns
  true if the given value is a valid Colours.

Definition at line 65 of file palette_func.h.

Referenced by StoryPageButtonData::GetColour().

## ◆ ReallyAdjustBrightness()

Colour ReallyAdjustBrightness | ( | Colour | colour,
|  | int | brightness
| ) |  |

Adjust brightness of colour.

Parameters

  colour | Colour to adjust.
  brightness | Brightness to apply to colour.

Returns
  Adjusted colour.

Definition at line 177 of file palette.cpp.

References GB().

## ◆ SetColourGradient()

void SetColourGradient | ( | Colours | colour,
|  | ColourShade | shade,
|  | PixelColour | palette_index
| ) |  |

Set colour gradient palette index.

Parameters

  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().

## Variable Documentation

## ◆ _cur_palette

Palette _cur_palette
| extern

Current palette.

Definition at line 24 of file palette.cpp.

Referenced by CopyPalette(), and GetContrastColour().

## ◆ DEFAULT_BRIGHTNESS

constexpr int DEFAULT_BRIGHTNESS = 128
| staticconstexpr

Definition at line 31 of file palette_func.h.

## ◆ PC_BARE_LAND

constexpr PixelColour PC_BARE_LAND {0x37}
| staticconstexpr

Brown palette colour for bare land.

Definition at line 121 of file palette_func.h.

Referenced by GetSmallMapVegetationPixels().

## ◆ PC_BLACK

constexpr PixelColour PC_BLACK {GREY_SCALE(1)}
| staticconstexpr

Black palette colour.

Definition at line 96 of file palette_func.h.

Referenced by NWidgetLeaf::Draw(), CargoesField::Draw(), SmallMapWindow::DrawSmallMap(), BuildAirportWindow::DrawWidget(), CompanyFinancesWindow::DrawWidget(), DropdownWindow::DrawWidget(), SaveLoadWindow::DrawWidget(), FrametimeGraphWindow::DrawWidget(), BaseCargoGraphWindow::DrawWidget(), BuildIndustryWindow::DrawWidget(), TooltipsWindow::DrawWidget(), MusicTrackSelectionWindow::DrawWidget(), MusicWindow::DrawWidget(), NewGRFWindow::DrawWidget(), SavePresetWindow::DrawWidget(), SmallMapWindow::DrawWidget(), ScriptDebugWindow::DrawWidgetLog(), DrawYearCategory(), DrawYearColumn(), GetSmallMapOwnerPixels(), and IConsoleWindow::OnPaint().

## ◆ PC_DARK_BLUE

constexpr PixelColour PC_DARK_BLUE {0x9D}
| staticconstexpr

Dark blue palette colour.

Definition at line 116 of file palette_func.h.

Referenced by SaveLoadWindow::DrawWidget(), NewGRFWindow::DrawWidget(), and SavePresetWindow::DrawWidget().

## ◆ PC_DARK_GREY

constexpr PixelColour PC_DARK_GREY {GREY_SCALE(6)}
| staticconstexpr

Dark grey palette colour.

Definition at line 97 of file palette_func.h.

Referenced by FrametimeGraphWindow::DrawWidget(), NewGRFWindow::DrawWidget(), and GetSmallMapOwnerPixels().

## ◆ PC_DARK_RED

constexpr PixelColour PC_DARK_RED {0xB4}
| staticconstexpr

Dark red palette colour.

Definition at line 102 of file palette_func.h.

Referenced by NWidgetToolbarContainer::Draw(), FrametimeGraphWindow::DrawWidget(), GetSmallMapOwnerPixels(), GetSmallMapVegetationPixels(), and IConsoleWindow::OnPaint().

## ◆ PC_FIELDS

constexpr PixelColour PC_FIELDS {0x25}
| staticconstexpr

Light brown palette colour for fields.

Definition at line 123 of file palette_func.h.

## ◆ PC_GRASS_LAND

constexpr PixelColour PC_GRASS_LAND {0x54}
| staticconstexpr

Dark green palette colour for grass land.

Definition at line 120 of file palette_func.h.

Referenced by GetSmallMapVegetationPixels().

## ◆ PC_GREEN

constexpr PixelColour PC_GREEN {0xD0}
| staticconstexpr

Green palette colour.

Definition at line 113 of file palette_func.h.

Referenced by GetSmallMapVegetationPixels(), and StationsWndShowStationRating().

## ◆ PC_GREY

constexpr PixelColour PC_GREY {GREY_SCALE(10)}
| staticconstexpr

Grey palette colour.

Definition at line 98 of file palette_func.h.

Referenced by NetworkContentListWindow::DrawMatrix(), NetworkGameWindow::DrawServerLine(), NewGRFWindow::DrawWidget(), and NewsWindow::DrawWidget().

## ◆ PC_LIGHT_BLUE

constexpr PixelColour PC_LIGHT_BLUE {0x98}
| staticconstexpr

Light blue palette colour.

Definition at line 117 of file palette_func.h.

Referenced by SpriteAlignerWindow::DrawWidget(), GetSmallMapRoutesPixels(), and GetSmallMapVegetationPixels().

## ◆ PC_LIGHT_YELLOW

constexpr PixelColour PC_LIGHT_YELLOW {0x44}
| staticconstexpr

Light yellow palette colour.

Definition at line 110 of file palette_func.h.

Referenced by TooltipsWindow::DrawWidget().

## ◆ PC_ORANGE

constexpr PixelColour PC_ORANGE {0xC2}
| staticconstexpr

Orange palette colour.

Definition at line 107 of file palette_func.h.

Referenced by GetSmallMapRoutesPixels(), and GetSmallMapVegetationPixels().

## ◆ PC_RAINFOREST

constexpr PixelColour PC_RAINFOREST {0x5C}
| staticconstexpr

Pale green palette colour for rainforest.

Definition at line 122 of file palette_func.h.

Referenced by GetSmallMapVegetationPixels().

## ◆ PC_RED

constexpr PixelColour PC_RED {0xB8}
| staticconstexpr

Red palette colour.

Definition at line 103 of file palette_func.h.

Referenced by GetSmallMapRoutesPixels(), and StationsWndShowStationRating().

## ◆ PC_ROUGH_LAND

constexpr PixelColour PC_ROUGH_LAND {0x52}
| staticconstexpr

Dark green palette colour for rough land.

Definition at line 119 of file palette_func.h.

## ◆ PC_TREES

constexpr PixelColour PC_TREES {0x57}
| staticconstexpr

Green palette colour for trees.

Definition at line 124 of file palette_func.h.

Referenced by GetSmallMapVegetationPixels().

## ◆ PC_VERY_DARK_BLUE

constexpr PixelColour PC_VERY_DARK_BLUE {0x9A}
| staticconstexpr

Almost-black blue palette colour.

Definition at line 115 of file palette_func.h.

Referenced by SaveLoadWindow::DrawWidget().

## ◆ PC_VERY_DARK_BROWN

constexpr PixelColour PC_VERY_DARK_BROWN {0x56}
| staticconstexpr

Almost-black brown palette colour.

Definition at line 105 of file palette_func.h.

Referenced by GetSmallMapRoutesPixels().

## ◆ PC_VERY_DARK_RED

constexpr PixelColour PC_VERY_DARK_RED {0xB2}
| staticconstexpr

Almost-black red palette colour.

Definition at line 101 of file palette_func.h.

Referenced by NWidgetToolbarContainer::Draw().

## ◆ PC_VERY_LIGHT_YELLOW

constexpr PixelColour PC_VERY_LIGHT_YELLOW {0x45}
| staticconstexpr

Almost-white yellow palette colour.

Definition at line 111 of file palette_func.h.

Referenced by SmallMapWindow::DrawHorizMapIndicator(), and SmallMapWindow::DrawVertMapIndicator().

## ◆ PC_WATER

constexpr PixelColour PC_WATER {0xC9}
| staticconstexpr

Dark blue palette colour for water.

Definition at line 125 of file palette_func.h.

Referenced by GetSmallMapOwnerPixels().

## ◆ PC_WHITE

constexpr PixelColour PC_WHITE {GREY_SCALE(15)}
| staticconstexpr

White palette colour.

Definition at line 99 of file palette_func.h.

Referenced by NWidgetSpacer::Draw(), DrawBox(), BaseGraphWindow::DrawGraph(), SmallMapWindow::DrawVehicles(), CompanyInfrastructureWindow::DrawWidget(), BaseCargoGraphWindow::DrawWidget(), BuildIndustryWindow::DrawWidget(), SpriteAlignerWindow::DrawWidget(), SmallMapWindow::DrawWidget(), TextfileWindow::DrawWidget(), and SmallMapWindow::GetTileColours().

## ◆ PC_YELLOW

constexpr PixelColour PC_YELLOW {0xBF}
| staticconstexpr

Yellow palette colour.

Definition at line 109 of file palette_func.h.

Referenced by GetSmallMapRoutesPixels().