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
21class Rgba32 {
22 public:
23 static constexpr std::uint8_t alpha_transparent = 0;
24 static constexpr std::uint8_t alpha_opaque = 255;
25
26 constexpr Rgba32() : red_{0}, green_{0}, blue_{0}, alpha_{0} {}
27
28 constexpr Rgba32(std::uint8_t red, std::uint8_t green, std::uint8_t blue, std::uint8_t alpha = alpha_opaque)
29 : red_{red}, green_{green}, blue_{blue}, alpha_{alpha}
30 {
31 }
32
33 auto operator<=>(const Rgba32 &rgba) const = default;
34
35 bool operator==(const Rgba32 &rgba) const = default;
36
43 [[nodiscard]] bool is_intrinsically_transparent() const;
44
53 [[nodiscard]] bool is_extrinsically_transparent(const Rgba32 &extrinsic) const;
54
63 [[nodiscard]] bool is_transparent(const Rgba32 &extrinsic) const;
64
65 [[nodiscard]] std::string to_jasc_str() const;
66
67 [[nodiscard]] std::string to_csv_str() const;
68
69 [[nodiscard]] bool equals_ignoring_alpha(const Rgba32 &other) const;
70
71 // friend std::ostream &operator<<(std::ostream &os, const Rgba32 &rgba);
72
73 [[nodiscard]] std::uint8_t red() const
74 {
75 return red_;
76 }
77
78 [[nodiscard]] std::uint8_t green() const
79 {
80 return green_;
81 }
82
83 [[nodiscard]] std::uint8_t blue() const
84 {
85 return blue_;
86 }
87
88 [[nodiscard]] std::uint8_t alpha() const
89 {
90 return alpha_;
91 }
92
93 private:
94 std::uint8_t red_;
95 std::uint8_t green_;
96 std::uint8_t blue_;
97 std::uint8_t alpha_;
98};
99
100inline std::string to_string(const Rgba32 &rgba)
101{
102 return "[" + std::to_string(rgba.red()) + ", " + std::to_string(rgba.green()) + ", " + std::to_string(rgba.blue()) +
103 ", " + std::to_string(rgba.alpha()) + "]";
104}
105
115inline std::ostream &operator<<(std::ostream &os, const Rgba32 &rgba)
116{
117 os << to_string(rgba);
118 return os;
119}
120
122constexpr Rgba32 rgba_white{255, 255, 255, Rgba32::alpha_opaque};
123constexpr Rgba32 rgba_grey{128, 128, 128, Rgba32::alpha_opaque};
129constexpr Rgba32 rgba_cyan{0, 255, 255, Rgba32::alpha_opaque};
131constexpr Rgba32 rgba_lime{128, 255, 128, Rgba32::alpha_opaque};
132
140inline std::vector<Rgba32> standard_greyscale_palette()
141{
142 return {
143 Rgba32{255, 255, 255, 255},
144 Rgba32{238, 238, 238, 255},
145 Rgba32{222, 222, 222, 255},
146 Rgba32{205, 205, 205, 255},
147 Rgba32{189, 189, 189, 255},
148 Rgba32{172, 172, 172, 255},
149 Rgba32{156, 156, 156, 255},
150 Rgba32{139, 139, 139, 255},
151 Rgba32{115, 115, 115, 255},
152 Rgba32{98, 98, 98, 255},
153 Rgba32{82, 82, 82, 255},
154 Rgba32{65, 65, 65, 255},
155 Rgba32{49, 49, 49, 255},
156 Rgba32{32, 32, 32, 255},
157 Rgba32{16, 16, 16, 255},
158 Rgba32{0, 0, 0, 255},
159 };
160}
161
162// std::size_t hash_value(const Rgba32 &obj) {
163// std::size_t seed = 0x7A22F97A;
164// seed ^= (seed << 6) + (seed >> 2) + 0x7687DDBC +
165// static_cast<std::size_t>(obj.red); seed ^= (seed << 6) + (seed >> 2) +
166// 0x63724761 + static_cast<std::size_t>(obj.green); seed ^= (seed << 6) +
167// (seed >> 2) + 0x3E044131 + static_cast<std::size_t>(obj.blue); seed ^=
168// (seed << 6) + (seed >> 2) + 0x00E64742 +
169// static_cast<std::size_t>(obj.alpha); return seed;
170// }
171
172} // namespace porytiles
173
174template <>
175struct std::hash<porytiles::Rgba32> {
176 std::size_t operator()(const porytiles::Rgba32 &rgba) const noexcept
177 {
178 const std::size_t h1 = std::hash<std::uint8_t>{}(rgba.red());
179 const std::size_t h2 = std::hash<std::uint8_t>{}(rgba.green());
180 const std::size_t h3 = std::hash<std::uint8_t>{}(rgba.blue());
181 const std::size_t h4 = std::hash<std::uint8_t>{}(rgba.alpha());
182 return h1 ^ (h2 << 8) ^ (h3 << 16) ^ (h4 << 24);
183 }
184};
185
203template <>
204struct std::formatter<porytiles::Rgba32> {
205 constexpr auto parse(std::format_parse_context &ctx)
206 {
207 return ctx.begin();
208 }
209
210 auto format(const porytiles::Rgba32 &rgba, auto &ctx) const
211 {
212 return std::format_to(ctx.out(), "{}", porytiles::to_string(rgba));
213 }
214};
Represents a 32-bit RGBA color.
Definition rgba32.hpp:21
constexpr Rgba32(std::uint8_t red, std::uint8_t green, std::uint8_t blue, std::uint8_t alpha=alpha_opaque)
Definition rgba32.hpp:28
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:23
std::uint8_t red() const
Definition rgba32.hpp:73
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:83
bool equals_ignoring_alpha(const Rgba32 &other) const
Definition rgba32.cpp:34
constexpr Rgba32()
Definition rgba32.hpp:26
std::uint8_t alpha() const
Definition rgba32.hpp:88
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:24
std::uint8_t green() const
Definition rgba32.hpp:78
bool operator==(const Rgba32 &rgba) const =default
constexpr Rgba32 rgba_yellow
Definition rgba32.hpp:127
constexpr Rgba32 rgba_purple
Definition rgba32.hpp:130
constexpr Rgba32 rgba_cyan
Definition rgba32.hpp:129
std::vector< Rgba32 > standard_greyscale_palette()
Returns the standard 16-color greyscale palette used for indexed tile output.
Definition rgba32.hpp:140
constexpr Rgba32 rgba_magenta
Definition rgba32.hpp:128
constexpr Rgba32 rgba_green
Definition rgba32.hpp:125
constexpr Rgba32 rgba_blue
Definition rgba32.hpp:126
constexpr Rgba32 rgba_grey
Definition rgba32.hpp:123
std::ostream & operator<<(std::ostream &os, const PrimaryPairingMode m)
Stream insertion operator for PrimaryPairingMode.
constexpr Rgba32 rgba_lime
Definition rgba32.hpp:131
constexpr Rgba32 rgba_white
Definition rgba32.hpp:122
constexpr Rgba32 rgba_red
Definition rgba32.hpp:124
std::string to_string(const PrimaryPairingMode m)
Converts a PrimaryPairingMode to its canonical string representation.
constexpr Rgba32 rgba_black
Definition rgba32.hpp:121
auto format(const porytiles::Rgba32 &rgba, auto &ctx) const
Definition rgba32.hpp:210
constexpr auto parse(std::format_parse_context &ctx)
Definition rgba32.hpp:205
std::size_t operator()(const porytiles::Rgba32 &rgba) const noexcept
Definition rgba32.hpp:176