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 <ostream>
6#include <set>
7#include <string>
8
9namespace porytiles2 {
10
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
47 [[nodiscard]] bool is_transparent(const Rgba32 &extrinsic) const;
48
49 [[nodiscard]] std::string to_jasc_str() const;
50
51 [[nodiscard]] bool equals_ignoring_alpha(const Rgba32 &other) const;
52
53 // friend std::ostream &operator<<(std::ostream &os, const Rgba32 &rgba);
54
55 [[nodiscard]] std::uint8_t red() const
56 {
57 return red_;
58 }
59
60 [[nodiscard]] std::uint8_t green() const
61 {
62 return green_;
63 }
64
65 [[nodiscard]] std::uint8_t blue() const
66 {
67 return blue_;
68 }
69
70 [[nodiscard]] std::uint8_t alpha() const
71 {
72 return alpha_;
73 }
74
75 private:
76 std::uint8_t red_;
77 std::uint8_t green_;
78 std::uint8_t blue_;
79 std::uint8_t alpha_;
80};
81
92inline std::ostream &operator<<(std::ostream &os, const Rgba32 &rgba)
93{
94 os << rgba.to_jasc_str();
95 return os;
96}
97
104inline auto format_as(const Rgba32 &rgba)
105{
106 return rgba.to_jasc_str();
107}
108
109inline std::string to_string(const Rgba32 &rgba)
110{
111 return rgba.to_jasc_str();
112}
113
115constexpr Rgba32 rgba_white{255, 255, 255, Rgba32::alpha_opaque};
116constexpr Rgba32 rgba_grey{128, 128, 128, Rgba32::alpha_opaque};
122constexpr Rgba32 rgba_cyan{0, 255, 255, Rgba32::alpha_opaque};
124constexpr Rgba32 rgba_lime{128, 255, 128, Rgba32::alpha_opaque};
125
126// std::size_t hash_value(const Rgba32 &obj) {
127// std::size_t seed = 0x7A22F97A;
128// seed ^= (seed << 6) + (seed >> 2) + 0x7687DDBC +
129// static_cast<std::size_t>(obj.red); seed ^= (seed << 6) + (seed >> 2) +
130// 0x63724761 + static_cast<std::size_t>(obj.green); seed ^= (seed << 6) +
131// (seed >> 2) + 0x3E044131 + static_cast<std::size_t>(obj.blue); seed ^=
132// (seed << 6) + (seed >> 2) + 0x00E64742 +
133// static_cast<std::size_t>(obj.alpha); return seed;
134// }
135
136} // namespace porytiles2
137
138template <>
139struct std::hash<porytiles2::Rgba32> {
140 std::size_t operator()(const porytiles2::Rgba32 &rgba) const noexcept
141 {
142 const std::size_t h1 = std::hash<std::uint8_t>{}(rgba.red());
143 const std::size_t h2 = std::hash<std::uint8_t>{}(rgba.green());
144 const std::size_t h3 = std::hash<std::uint8_t>{}(rgba.blue());
145 const std::size_t h4 = std::hash<std::uint8_t>{}(rgba.alpha());
146 return h1 ^ (h2 << 8) ^ (h3 << 16) ^ (h4 << 24);
147 }
148};
Represents a 32-bit RGBA color.
Definition rgba32.hpp:21
bool equals_ignoring_alpha(const Rgba32 &other) const
Definition rgba32.cpp:23
bool operator==(const Rgba32 &rgba) const =default
constexpr Rgba32()
Definition rgba32.hpp:26
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_jasc_str() const
Definition rgba32.cpp:18
static constexpr std::uint8_t alpha_opaque
Definition rgba32.hpp:24
std::uint8_t alpha() const
Definition rgba32.hpp:70
static constexpr std::uint8_t alpha_transparent
Definition rgba32.hpp:23
std::uint8_t green() const
Definition rgba32.hpp:60
auto operator<=>(const Rgba32 &rgba) const =default
std::uint8_t blue() const
Definition rgba32.hpp:65
std::uint8_t red() const
Definition rgba32.hpp:55
bool is_transparent(const Rgba32 &extrinsic) const
Checks if this color should be treated as transparent.
Definition rgba32.cpp:9
auto format_as(const Rgba32 &rgba)
Provides a simple way for fmtlib to format an Rgba32.
Definition rgba32.hpp:104
constexpr Rgba32 rgba_red
Definition rgba32.hpp:117
constexpr Rgba32 rgba_green
Definition rgba32.hpp:118
constexpr Rgba32 rgba_lime
Definition rgba32.hpp:124
constexpr Rgba32 rgba_cyan
Definition rgba32.hpp:122
constexpr Rgba32 rgba_yellow
Definition rgba32.hpp:120
constexpr Rgba32 rgba_purple
Definition rgba32.hpp:123
constexpr Rgba32 rgba_blue
Definition rgba32.hpp:119
std::ostream & operator<<(std::ostream &os, const Rgba32 &rgba)
Stream insertion operator for Rgba32.
Definition rgba32.hpp:92
constexpr Rgba32 rgba_black
Definition rgba32.hpp:114
constexpr Rgba32 rgba_white
Definition rgba32.hpp:115
constexpr Rgba32 rgba_grey
Definition rgba32.hpp:116
constexpr Rgba32 rgba_magenta
Definition rgba32.hpp:121
std::string to_string(const IncrementalBuildMode &value)
std::size_t operator()(const porytiles2::Rgba32 &rgba) const noexcept
Definition rgba32.hpp:140