JNGL
Easy to use cross-platform 2D game library
Loading...
Searching...
No Matches
Rgb.hpp
Go to the documentation of this file.
1// Copyright 2024-2026 Jan Niklas Hasse <jhasse@bixense.com>
2// For conditions of distribution and use, see copyright notice in LICENSE.txt
5#pragma once
6
7#include <cstddef>
8#include <cstdint>
9#include <iosfwd>
10
11#if __has_include(<format>) && (!defined(_LIBCPP_VERSION) || _LIBCPP_VERSION >= 170000)
12#include <format>
13#endif
14
15namespace jngl {
16
17class Color;
18
21class Rgb {
22public:
28 constexpr Rgb(float red, float green, float blue) : red(red), green(green), blue(blue) {
29 }
30
31 constexpr static Rgb u8(uint8_t red, uint8_t green, uint8_t blue) {
32 return Rgb{ static_cast<float>(red) / 255.f, static_cast<float>(green) / 255.f,
33 static_cast<float>(blue) / 255.f };
34 }
35
37 Rgb(Color); // NOLINT
38
40 constexpr float getRed() const {
41 return red;
42 }
44 void setRed(float);
45
47 constexpr float getGreen() const {
48 return green;
49 }
51 void setGreen(float);
52
54 constexpr float getBlue() const {
55 return blue;
56 }
58 void setBlue(float);
59
61 uint8_t getRed_u8() const;
62
64 uint8_t getGreen_u8() const;
65
67 uint8_t getBlue_u8() const;
68
70 explicit operator Color() const;
71
72private:
73 float red;
74 float green;
75 float blue;
76};
77
79Rgb mix(Rgb a, Rgb b, float t);
80
82[[deprecated("Use mix instead")]]
83inline Rgb interpolate(Rgb a, Rgb b, float t) {
84 return mix(a, b, t);
85}
86
90
93bool operator==(Rgb a, Rgb b);
94
97
98} // namespace jngl
99
100#if __has_include(<format>) && (!defined(_LIBCPP_VERSION) || _LIBCPP_VERSION >= 170000)
101template <> struct std::formatter<jngl::Rgb> {
102 constexpr static auto parse(std::format_parse_context& ctx) {
103 return ctx.begin();
104 }
105 auto format(const jngl::Rgb& c, auto& ctx) const {
106 return std::format_to(ctx.out(), "jngl::Rgb{{ {}, {}, {} }}", c.getRed(), c.getGreen(),
107 c.getBlue());
108 }
109};
110#endif
111
113jngl::Rgb operator""_rgb(unsigned long long);
114
115namespace jngl::internal {
116// These are intentionally declared but not defined. When reached in a consteval context,
117// the compiler error will include the function name as the diagnostic message.
118void invalid_hex_character_in_color_literal();
119void invalid_rgb_color_literal_expected_format_hash_rrggbb();
120
121consteval uint8_t hexVal(char c) {
122 if (c >= '0' && c <= '9') {
123 return static_cast<uint8_t>(c - '0');
124 }
125 if (c >= 'a' && c <= 'f') {
126 return static_cast<uint8_t>(c - 'a' + 10);
127 }
128 if (c >= 'A' && c <= 'F') {
129 return static_cast<uint8_t>(c - 'A' + 10);
130 }
131 invalid_hex_character_in_color_literal();
132 return 0;
133}
134consteval uint8_t hexByte(const char* s) {
135 return static_cast<uint8_t>(hexVal(s[0]) * 16 + hexVal(s[1]));
136}
137} // namespace jngl::internal
138
140consteval jngl::Rgb operator""_rgb(const char* hex, size_t length) {
141 using jngl::internal::hexByte;
142 if (length == 7 && hex[0] == '#') {
143 return jngl::Rgb::u8(hexByte(hex + 1), hexByte(hex + 3), hexByte(hex + 5));
144 }
145 jngl::internal::invalid_rgb_color_literal_expected_format_hash_rrggbb();
146 return jngl::Rgb{ 0, 0, 0 };
147}
Object representing a RGB color.
Definition Color.hpp:28
Object representing a RGB color, new version of jngl::Color (which will be deprecated in the future)
Definition Rgb.hpp:21
uint8_t getGreen_u8() const
0 ... 255
uint8_t getRed_u8() const
0 ... 255
void setBlue(float)
0.0f ... 1.0f
constexpr float getGreen() const
0.0f ... 1.0f
Definition Rgb.hpp:47
constexpr float getRed() const
0.0f ... 1.0f
Definition Rgb.hpp:40
void setRed(float)
0.0f ... 1.0f
void setGreen(float)
0.0f ... 1.0f
constexpr float getBlue() const
0.0f ... 1.0f
Definition Rgb.hpp:54
Rgb(Color)
Implicit conversion for backwards compatibility.
constexpr Rgb(float red, float green, float blue)
Definition Rgb.hpp:28
uint8_t getBlue_u8() const
0 ... 255
T format(T... args)
T format_to(T... args)
JNGL's main namespace.
Color interpolate(Color a, Color b, float t)
Returns a color mix between a (t == 0) and b (t == 1)
Rgb getBackgroundColor()
Returns the screen's background color which is visible when nothing is drawn and also used by jngl::F...
bool operator==(Rgb a, Rgb b)
Returns true if the two colors are equal in 24 bit SDR space, i.e.
Rgb mix(Rgb a, Rgb b, float t)
Returns a color mix between a (t == 0) and b (t == 1)
std::ostream & operator<<(std::ostream &os, Rgb color)
Prints the color as jngl::Rgb{ red, green, blue }