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 <string>
6
7namespace porytiles {
8
9class Rgba32 {
10 std::uint8_t red_;
11 std::uint8_t green_;
12 std::uint8_t blue_;
13 std::uint8_t alpha_;
14
15 public:
16 static constexpr std::uint8_t kAlphaTransparent = 0;
17 static constexpr std::uint8_t kAlphaOpaque = 255;
18
19 constexpr Rgba32() : red_{0}, green_{0}, blue_{0}, alpha_{0} {}
20
21 constexpr Rgba32(std::uint8_t red, std::uint8_t green, std::uint8_t blue, std::uint8_t alpha = kAlphaOpaque)
22 : red_{red}, green_{green}, blue_{blue}, alpha_{alpha} {}
23
24 [[nodiscard]] std::uint8_t red() const {
25 return red_;
26 }
27
28 [[nodiscard]] std::uint8_t green() const {
29 return green_;
30 }
31
32 [[nodiscard]] std::uint8_t blue() const {
33 return blue_;
34 }
35
36 [[nodiscard]] std::uint8_t alpha() const {
37 return alpha_;
38 }
39
40 bool operator==(const Rgba32 &rgba) const = default;
41 auto operator<=>(const Rgba32 &rgba) const = default;
42
43 [[nodiscard]] std::string ToJascStr() const;
44
45 [[nodiscard]] bool EqualsIgnoringAlpha(const Rgba32 &other) const;
46
47 // friend std::ostream &operator<<(std::ostream &os, const Rgba32 &rgba);
48};
49
52inline auto format_as(const Rgba32 &rgba) {
53 return rgba.ToJascStr();
54}
55
57constexpr Rgba32 kRgbaWhite{255, 255, 255, Rgba32::kAlphaOpaque};
58constexpr Rgba32 kRgbaGrey{128, 128, 128, Rgba32::kAlphaOpaque};
59constexpr Rgba32 kRgbaRed{255, 0, 0, Rgba32::kAlphaOpaque};
62constexpr Rgba32 kRgbaYellow{255, 255, 0, Rgba32::kAlphaOpaque};
64constexpr Rgba32 kRgbaCyan{0, 255, 255, Rgba32::kAlphaOpaque};
65constexpr Rgba32 kRgbaPurple{128, 0, 255, Rgba32::kAlphaOpaque};
66constexpr Rgba32 kRgbaLime{128, 255, 128, Rgba32::kAlphaOpaque};
67
68// std::size_t hash_value(const Rgba32 &obj) {
69// std::size_t seed = 0x7A22F97A;
70// seed ^= (seed << 6) + (seed >> 2) + 0x7687DDBC + static_cast<std::size_t>(obj.red);
71// seed ^= (seed << 6) + (seed >> 2) + 0x63724761 + static_cast<std::size_t>(obj.green);
72// seed ^= (seed << 6) + (seed >> 2) + 0x3E044131 + static_cast<std::size_t>(obj.blue);
73// seed ^= (seed << 6) + (seed >> 2) + 0x00E64742 + static_cast<std::size_t>(obj.alpha);
74// return seed;
75// }
76
77} // namespace porytiles
78
79template <>
80struct std::hash<porytiles::Rgba32> {
81 std::size_t operator()(const porytiles::Rgba32 &rgba) const noexcept {
82 const std::size_t h1 = std::hash<std::uint8_t>{}(rgba.red());
83 const std::size_t h2 = std::hash<std::uint8_t>{}(rgba.green());
84 const std::size_t h3 = std::hash<std::uint8_t>{}(rgba.blue());
85 const std::size_t h4 = std::hash<std::uint8_t>{}(rgba.alpha());
86 return h1 ^ (h2 << 8) ^ (h3 << 16) ^ (h4 << 24);
87 }
88};
auto operator<=>(const Rgba32 &rgba) const =default
std::uint8_t red() const
Definition rgba32.hpp:24
std::string ToJascStr() const
Definition rgba32.cpp:7
static constexpr std::uint8_t kAlphaTransparent
Definition rgba32.hpp:16
std::uint8_t blue() const
Definition rgba32.hpp:32
bool EqualsIgnoringAlpha(const Rgba32 &other) const
Definition rgba32.cpp:11
static constexpr std::uint8_t kAlphaOpaque
Definition rgba32.hpp:17
constexpr Rgba32()
Definition rgba32.hpp:19
std::uint8_t alpha() const
Definition rgba32.hpp:36
constexpr Rgba32(std::uint8_t red, std::uint8_t green, std::uint8_t blue, std::uint8_t alpha=kAlphaOpaque)
Definition rgba32.hpp:21
std::uint8_t green() const
Definition rgba32.hpp:28
bool operator==(const Rgba32 &rgba) const =default
constexpr Rgba32 kRgbaCyan
Definition rgba32.hpp:64
constexpr Rgba32 kRgbaBlack
Definition rgba32.hpp:56
constexpr Rgba32 kRgbaLime
Definition rgba32.hpp:66
auto format_as(const Bgr15 &bgr)
Definition bgr15.hpp:48
constexpr Rgba32 kRgbaYellow
Definition rgba32.hpp:62
constexpr Rgba32 kRgbaWhite
Definition rgba32.hpp:57
constexpr Rgba32 kRgbaGrey
Definition rgba32.hpp:58
constexpr Rgba32 kRgbaGreen
Definition rgba32.hpp:60
constexpr Rgba32 kRgbaPurple
Definition rgba32.hpp:65
constexpr Rgba32 kRgbaBlue
Definition rgba32.hpp:61
constexpr Rgba32 kRgbaRed
Definition rgba32.hpp:59
constexpr Rgba32 kRgbaMagenta
Definition rgba32.hpp:63
std::size_t operator()(const porytiles::Rgba32 &rgba) const noexcept
Definition rgba32.hpp:81