OpenTTD Source: src/palette_func.h Source File

OpenTTD Source 15.0-RC2

palette_func.h
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#ifndef PALETTE_FUNC_H
11#define PALETTE_FUNC_H
12
13#include "core/enum_type.hpp"
14#include "gfx_type.h"
15#include "strings_type.h"
16#include "string_type.h"
17
18extern Palette _cur_palette;
19
20bool CopyPalette(Palette &local_palette, bool force_copy = false);
21void GfxInitPalettes();
22
23uint8_t GetNearestColourIndex(uint8_t r, uint8_t g, uint8_t b);
24uint8_t GetNearestColourReshadeIndex(uint8_t b);
25
26inline uint8_t GetNearestColourIndex(const Colour colour)
27{
28 return GetNearestColourIndex(colour.r, colour.g, colour.b);
29}
30
31static constexpr int DEFAULT_BRIGHTNESS = 128;
32
33Colour ReallyAdjustBrightness(Colour colour, int brightness);
34
35static inline Colour AdjustBrightness(Colour colour, uint8_t brightness)
36{
37 /* Shortcut for normal brightness */
38 if (brightness == DEFAULT_BRIGHTNESS) return colour;
39
40 return ReallyAdjustBrightness(colour, brightness);
41}
42
49 static inline uint8_t GetColourBrightness(Colour colour)
50{
51 uint8_t rgb_max = std::max(colour.r, std::max(colour.g, colour.b));
52
53 /* Black pixel (8bpp or old 32bpp image), so use default value */
54 if (rgb_max == 0) rgb_max = DEFAULT_BRIGHTNESS;
55
56 return rgb_max;
57}
58
65 inline bool IsValidColours(Colours colours)
66{
67 return colours < COLOUR_END;
68}
69
70 TextColour GetContrastColour(PixelColour background, uint8_t threshold = 128);
71
72enum ColourShade : uint8_t {
73 SHADE_BEGIN = 0,
74 SHADE_DARKEST = SHADE_BEGIN,
75 SHADE_DARKER,
76 SHADE_DARK,
77 SHADE_NORMAL,
78 SHADE_LIGHT,
79 SHADE_LIGHTER,
80 SHADE_LIGHTEST,
81 SHADE_LIGHTEREST,
82 SHADE_END,
83};
84 DECLARE_INCREMENT_DECREMENT_OPERATORS(ColourShade)
85
86 PixelColour GetColourGradient(Colours colour, ColourShade shade);
87void SetColourGradient(Colours colour, ColourShade shade, PixelColour palette_colour);
88
94 inline constexpr PixelColour GREY_SCALE(uint8_t level) { return PixelColour{level}; }
95
96 static constexpr PixelColour PC_BLACK {GREY_SCALE(1)};
97 static constexpr PixelColour PC_DARK_GREY {GREY_SCALE(6)};
98 static constexpr PixelColour PC_GREY {GREY_SCALE(10)};
99 static constexpr PixelColour PC_WHITE {GREY_SCALE(15)};
100
101 static constexpr PixelColour PC_VERY_DARK_RED {0xB2};
102 static constexpr PixelColour PC_DARK_RED {0xB4};
103 static constexpr PixelColour PC_RED {0xB8};
104
105 static constexpr PixelColour PC_VERY_DARK_BROWN {0x56};
106
107 static constexpr PixelColour PC_ORANGE {0xC2};
108
109 static constexpr PixelColour PC_YELLOW {0xBF};
110 static constexpr PixelColour PC_LIGHT_YELLOW {0x44};
111 static constexpr PixelColour PC_VERY_LIGHT_YELLOW {0x45};
112
113 static constexpr PixelColour PC_GREEN {0xD0};
114
115 static constexpr PixelColour PC_VERY_DARK_BLUE {0x9A};
116 static constexpr PixelColour PC_DARK_BLUE {0x9D};
117 static constexpr PixelColour PC_LIGHT_BLUE {0x98};
118
119 static constexpr PixelColour PC_ROUGH_LAND {0x52};
120 static constexpr PixelColour PC_GRASS_LAND {0x54};
121 static constexpr PixelColour PC_BARE_LAND {0x37};
122 static constexpr PixelColour PC_RAINFOREST {0x5C};
123 static constexpr PixelColour PC_FIELDS {0x25};
124 static constexpr PixelColour PC_TREES {0x57};
125 static constexpr PixelColour PC_WATER {0xC9};
126
127#endif /* PALETTE_FUNC_H */
enum_type.hpp
Type (helpers) for enums.
DECLARE_INCREMENT_DECREMENT_OPERATORS
#define DECLARE_INCREMENT_DECREMENT_OPERATORS(enum_type)
For some enums it is useful to have pre/post increment/decrement operators.
Definition enum_type.hpp:63
gfx_type.h
Types related to the graphics and/or input devices.
TextColour
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition gfx_type.h:307
GetColourBrightness
static uint8_t GetColourBrightness(Colour colour)
Get the brightness of a colour.
Definition palette_func.h:49
PC_TREES
static constexpr PixelColour PC_TREES
Green palette colour for trees.
Definition palette_func.h:124
PC_VERY_DARK_RED
static constexpr PixelColour PC_VERY_DARK_RED
Almost-black red palette colour.
Definition palette_func.h:101
CopyPalette
bool CopyPalette(Palette &local_palette, bool force_copy=false)
Copy the current palette if the palette was updated.
Definition palette.cpp:225
PC_VERY_LIGHT_YELLOW
static constexpr PixelColour PC_VERY_LIGHT_YELLOW
Almost-white yellow palette colour.
Definition palette_func.h:111
PC_ROUGH_LAND
static constexpr PixelColour PC_ROUGH_LAND
Dark green palette colour for rough land.
Definition palette_func.h:119
PC_YELLOW
static constexpr PixelColour PC_YELLOW
Yellow palette colour.
Definition palette_func.h:109
PC_GREEN
static constexpr PixelColour PC_GREEN
Green palette colour.
Definition palette_func.h:113
PC_FIELDS
static constexpr PixelColour PC_FIELDS
Light brown palette colour for fields.
Definition palette_func.h:123
GREY_SCALE
constexpr PixelColour GREY_SCALE(uint8_t level)
Return the colour for a particular greyscale level.
Definition palette_func.h:94
PC_DARK_RED
static constexpr PixelColour PC_DARK_RED
Dark red palette colour.
Definition palette_func.h:102
IsValidColours
bool IsValidColours(Colours colours)
Checks if a Colours value is valid.
Definition palette_func.h:65
PC_WATER
static constexpr PixelColour PC_WATER
Dark blue palette colour for water.
Definition palette_func.h:125
SetColourGradient
void SetColourGradient(Colours colour, ColourShade shade, PixelColour palette_colour)
Set colour gradient palette index.
Definition palette.cpp:399
PC_ORANGE
static constexpr PixelColour PC_ORANGE
Orange palette colour.
Definition palette_func.h:107
PC_GRASS_LAND
static constexpr PixelColour PC_GRASS_LAND
Dark green palette colour for grass land.
Definition palette_func.h:120
PC_DARK_BLUE
static constexpr PixelColour PC_DARK_BLUE
Dark blue palette colour.
Definition palette_func.h:116
PC_BARE_LAND
static constexpr PixelColour PC_BARE_LAND
Brown palette colour for bare land.
Definition palette_func.h:121
ReallyAdjustBrightness
Colour ReallyAdjustBrightness(Colour colour, int brightness)
Adjust brightness of colour.
Definition palette.cpp:177
_cur_palette
Palette _cur_palette
Current palette.
Definition palette.cpp:24
PC_GREY
static constexpr PixelColour PC_GREY
Grey palette colour.
Definition palette_func.h:98
PC_RED
static constexpr PixelColour PC_RED
Red palette colour.
Definition palette_func.h:103
PC_DARK_GREY
static constexpr PixelColour PC_DARK_GREY
Dark grey palette colour.
Definition palette_func.h:97
GetNearestColourReshadeIndex
uint8_t GetNearestColourReshadeIndex(uint8_t b)
Get nearest colour palette index from a brightness level.
Definition palette.cpp:164
PC_BLACK
static constexpr PixelColour PC_BLACK
Black palette colour.
Definition palette_func.h:96
GetContrastColour
TextColour GetContrastColour(PixelColour background, uint8_t threshold=128)
Determine a contrasty text colour for a coloured background.
Definition palette.cpp:361
GetNearestColourIndex
uint8_t GetNearestColourIndex(uint8_t r, uint8_t g, uint8_t b)
Get nearest colour palette index from an RGB colour.
Definition palette.cpp:151
PC_LIGHT_YELLOW
static constexpr PixelColour PC_LIGHT_YELLOW
Light yellow palette colour.
Definition palette_func.h:110
PC_RAINFOREST
static constexpr PixelColour PC_RAINFOREST
Pale green palette colour for rainforest.
Definition palette_func.h:122
PC_WHITE
static constexpr PixelColour PC_WHITE
White palette colour.
Definition palette_func.h:99
GetColourGradient
PixelColour GetColourGradient(Colours colour, ColourShade shade)
Get colour gradient palette index.
Definition palette.cpp:388
PC_VERY_DARK_BROWN
static constexpr PixelColour PC_VERY_DARK_BROWN
Almost-black brown palette colour.
Definition palette_func.h:105
PC_LIGHT_BLUE
static constexpr PixelColour PC_LIGHT_BLUE
Light blue palette colour.
Definition palette_func.h:117
PC_VERY_DARK_BLUE
static constexpr PixelColour PC_VERY_DARK_BLUE
Almost-black blue palette colour.
Definition palette_func.h:115
string_type.h
Types for strings.
strings_type.h
Types related to strings.
Palette
Information about the currently used palette.
Definition gfx_type.h:373
PixelColour
Colour for pixel/line drawing.
Definition gfx_type.h:405