Porytiles
Loading...
Searching...
No Matches
rgba32.hpp
Go to the documentation of this file.
1#pragma once
2
3// ReSharper disable once CppUnusedIncludeDirective
4#include <cstdint>
5#include <format>
6#include <ostream>
7#include <set>
8#include <string>
9#include <vector>
10
11namespace porytiles {
12
23class Rgba32 {
24 public:
25 static constexpr std::uint8_t alpha_transparent = 0;
26 static constexpr std::uint8_t alpha_opaque = 255;
27
28 constexpr Rgba32() : red_{0}, green_{0}, blue_{0}, alpha_{0} {}
29
30 constexpr Rgba32(std::uint8_t red, std::uint8_t green, std::uint8_t blue, std::uint8_t alpha = alpha_opaque)
31 : red_{red}, green_{green}, blue_{blue}, alpha_{alpha}
32 {
33 }
34
35 auto operator<=>(const Rgba32 &rgba) const = default;
36
37 bool operator==(const Rgba32 &rgba) const = default;
38
47 [[nodiscard]] bool is_intrinsically_transparent() const;
48
59 [[nodiscard]] bool is_extrinsically_transparent(const Rgba32 &extrinsic) const;
60
71 [[nodiscard]] bool is_transparent(const Rgba32 &extrinsic) const;
72
73 [[nodiscard]] std::string to_jasc_str() const;
74
75 [[nodiscard]] std::string to_csv_str() const;
76
77 [[nodiscard]] bool equals_ignoring_alpha(const Rgba32 &other) const;
78
79 // friend std::ostream &operator<<(std::ostream &os, const Rgba32 &rgba);
80
81 [[nodiscard]] std::uint8_t red() const
82 {
83 return red_;
84 }
85
86 [[nodiscard]] std::uint8_t green() const
87 {
88 return green_;
89 }
90
91 [[nodiscard]] std::uint8_t blue() const
92 {
93 return blue_;
94 }
95
96 [[nodiscard]] std::uint8_t alpha() const
97 {
98 return alpha_;
99 }
100
101 private:
102 std::uint8_t red_;
103 std::uint8_t green_;
104 std::uint8_t blue_;
105 std::uint8_t alpha_;
106};
107
108inline std::string to_string(const Rgba32 &rgba)
109{
110 return "[" + std::to_string(rgba.red()) + ", " + std::to_string(rgba.green()) + ", " + std::to_string(rgba.blue()) +
111 ", " + std::to_string(rgba.alpha()) + "]";
112}
113
125inline std::ostream &operator<<(std::ostream &os, const Rgba32 &rgba)
126{
127 os << to_string(rgba);
128 return os;
129}
130
132constexpr Rgba32 rgba_white{255, 255, 255, Rgba32::alpha_opaque};
133constexpr Rgba32 rgba_grey{128, 128, 128, Rgba32::alpha_opaque};
139constexpr Rgba32 rgba_cyan{0, 255, 255, Rgba32::alpha_opaque};
141constexpr Rgba32 rgba_lime{128, 255, 128, Rgba32::alpha_opaque};
142
152inline std::vector<Rgba32> standard_greyscale_pal()
153{
154 return {
155 Rgba32{255, 255, 255, 255},
156 Rgba32{238, 238, 238, 255},
157 Rgba32{222, 222, 222, 255},
158 Rgba32{205, 205, 205, 255},
159 Rgba32{189, 189, 189, 255},
160 Rgba32{172, 172, 172, 255},
161 Rgba32{156, 156, 156, 255},
162 Rgba32{139, 139, 139, 255},
163 Rgba32{115, 115, 115, 255},
164 Rgba32{98, 98, 98, 255},
165 Rgba32{82, 82, 82, 255},
166 Rgba32{65, 65, 65, 255},
167 Rgba32{49, 49, 49, 255},
168 Rgba32{32, 32, 32, 255},
169 Rgba32{16, 16, 16, 255},
170 Rgba32{0, 0, 0, 255},
171 };
172}
173
174// std::size_t hash_value(const Rgba32 &obj) {
175// std::size_t seed = 0x7A22F97A;
176// seed ^= (seed << 6) + (seed >> 2) + 0x7687DDBC +
177// static_cast<std::size_t>(obj.red); seed ^= (seed << 6) + (seed >> 2) +
178// 0x63724761 + static_cast<std::size_t>(obj.green); seed ^= (seed << 6) +
179// (seed >> 2) + 0x3E044131 + static_cast<std::size_t>(obj.blue); seed ^=
180// (seed << 6) + (seed >> 2) + 0x00E64742 +
181// static_cast<std::size_t>(obj.alpha); return seed;
182// }
183
184} // namespace porytiles
185
186template <>
187struct std::hash<porytiles::Rgba32> {
188 std::size_t operator()(const porytiles::Rgba32 &rgba) const noexcept
189 {
190 const std::size_t h1 = std::hash<std::uint8_t>{}(rgba.red());
191 const std::size_t h2 = std::hash<std::uint8_t>{}(rgba.green());
192 const std::size_t h3 = std::hash<std::uint8_t>{}(rgba.blue());
193 const std::size_t h4 = std::hash<std::uint8_t>{}(rgba.alpha());
194 return h1 ^ (h2 << 8) ^ (h3 << 16) ^ (h4 << 24);
195 }
196};
197
217template <>
218struct std::formatter<porytiles::Rgba32> {
219 constexpr auto parse(std::format_parse_context &ctx)
220 {
221 return ctx.begin();
222 }
223
224 auto format(const porytiles::Rgba32 &rgba, auto &ctx) const
225 {
226 return std::format_to(ctx.out(), "{}", porytiles::to_string(rgba));
227 }
228};
Represents a 32-bit RGBA color.
Definition rgba32.hpp:23
constexpr Rgba32(std::uint8_t red, std::uint8_t green, std::uint8_t blue, std::uint8_t alpha=alpha_opaque)
Definition rgba32.hpp:30
std::string to_csv_str() const
Definition rgba32.cpp:29
auto operator<=>(const Rgba32 &rgba) const =default
static constexpr std::uint8_t alpha_transparent
Definition rgba32.hpp:25
std::uint8_t red() const
Definition rgba32.hpp:81
bool is_intrinsically_transparent() const
Checks if this color is intrinsically transparent based on its alpha channel.
Definition rgba32.cpp:9
bool is_transparent(const Rgba32 &extrinsic) const
Checks if this color should be treated as transparent.
Definition rgba32.cpp:19
std::uint8_t blue() const
Definition rgba32.hpp:91
bool equals_ignoring_alpha(const Rgba32 &other) const
Definition rgba32.cpp:34
constexpr Rgba32()
Definition rgba32.hpp:28
std::uint8_t alpha() const
Definition rgba32.hpp:96
bool is_extrinsically_transparent(const Rgba32 &extrinsic) const
Checks if this color matches the extrinsic transparency color.
Definition rgba32.cpp:14
std::string to_jasc_str() const
Definition rgba32.cpp:24
static constexpr std::uint8_t alpha_opaque
Definition rgba32.hpp:26
std::uint8_t green() const
Definition rgba32.hpp:86
bool operator==(const Rgba32 &rgba) const =default
constexpr Rgba32 rgba_yellow
Definition rgba32.hpp:137
constexpr Rgba32 rgba_purple
Definition rgba32.hpp:140
constexpr Rgba32 rgba_cyan
Definition rgba32.hpp:139
std::vector< Rgba32 > standard_greyscale_pal()
Returns the standard 16-color greyscale palette used for indexed tile output.
Definition rgba32.hpp:152
constexpr Rgba32 rgba_magenta
Definition rgba32.hpp:138
constexpr Rgba32 rgba_green
Definition rgba32.hpp:135
constexpr Rgba32 rgba_blue
Definition rgba32.hpp:136
constexpr Rgba32 rgba_grey
Definition rgba32.hpp:133
std::ostream & operator<<(std::ostream &os, const PrimaryPairingMode m)
Stream insertion operator for PrimaryPairingMode.
constexpr Rgba32 rgba_lime
Definition rgba32.hpp:141
constexpr Rgba32 rgba_white
Definition rgba32.hpp:132
constexpr Rgba32 rgba_red
Definition rgba32.hpp:134
std::string to_string(const PrimaryPairingMode m)
Converts a PrimaryPairingMode to its canonical string representation.
constexpr Rgba32 rgba_black
Definition rgba32.hpp:131
auto format(const porytiles::Rgba32 &rgba, auto &ctx) const
Definition rgba32.hpp:224
constexpr auto parse(std::format_parse_context &ctx)
Definition rgba32.hpp:219
std::size_t operator()(const porytiles::Rgba32 &rgba) const noexcept
Definition rgba32.hpp:188