Porytiles
Loading...
Searching...
No Matches
bgr15.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <string>
5
6namespace porytiles {
7
8class Bgr15 {
9 std::uint8_t red_;
10 std::uint8_t green_;
11 std::uint8_t blue_;
12
13 public:
14 constexpr Bgr15() : red_{0}, green_{0}, blue_{0} {}
15
16 constexpr Bgr15(const std::uint8_t red, const std::uint8_t green, const std::uint8_t blue) {
17 // Class invariant: color channels always have the 3 LSBs unset
18 red_ = (red >> 3) << 3;
19 green_ = (green >> 3) << 3;
20 blue_ = (blue >> 3) << 3;
21 }
22
23 [[nodiscard]] std::uint8_t red() const {
24 return red_;
25 }
26
27 [[nodiscard]] std::uint8_t green() const {
28 return green_;
29 }
30
31 [[nodiscard]] std::uint8_t blue() const {
32 return blue_;
33 }
34
35 auto operator<=>(const Bgr15 &) const = default;
36
37 [[nodiscard]] std::uint16_t Pack() const;
38
39 [[nodiscard]] std::string ToJascStr() const;
40
41 [[nodiscard]] static Bgr15 Unpack(std::uint16_t packed_bgr);
42
43 // friend std::ostream &operator<<(std::ostream &os, const Bgr15 &bgr);
44};
45
48inline auto format_as(const Bgr15 &bgr) {
49 return bgr.ToJascStr();
50}
51
52} // namespace porytiles
53
54template <>
55struct std::hash<porytiles::Bgr15> {
56 std::size_t operator()(const porytiles::Bgr15 &bgr) const noexcept {
57 return std::hash<uint16_t>{}(bgr.Pack());
58 }
59};
constexpr Bgr15()
Definition bgr15.hpp:14
static Bgr15 Unpack(std::uint16_t packed_bgr)
Definition bgr15.cpp:13
auto operator<=>(const Bgr15 &) const =default
std::string ToJascStr() const
Definition bgr15.cpp:5
std::uint8_t green() const
Definition bgr15.hpp:27
std::uint8_t blue() const
Definition bgr15.hpp:31
std::uint8_t red() const
Definition bgr15.hpp:23
constexpr Bgr15(const std::uint8_t red, const std::uint8_t green, const std::uint8_t blue)
Definition bgr15.hpp:16
std::uint16_t Pack() const
Definition bgr15.cpp:9
auto format_as(const Bgr15 &bgr)
Definition bgr15.hpp:48
std::size_t operator()(const porytiles::Bgr15 &bgr) const noexcept
Definition bgr15.hpp:56