JNGL
Easy to use cross-platform 2D game library
Loading...
Searching...
No Matches
Rgba.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 "Rgb.hpp"
8
9#include <cstdint>
10
11namespace jngl {
12class Alpha;
13
22class Rgba {
23public:
30 constexpr Rgba(float red, float green, float blue, float alpha)
31 : red(red), green(green), blue(blue), alpha(alpha) {
32 }
33 Rgba(float red, float green, float blue, Alpha alpha);
34 Rgba(Rgb, Alpha);
35
36 constexpr static Rgba u8(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha) {
37 return Rgba{ static_cast<float>(red) / 255.f, static_cast<float>(green) / 255.f,
38 static_cast<float>(blue) / 255.f, static_cast<float>(alpha) / 255.f };
39 }
40
42 constexpr float getRed() const {
43 return red;
44 }
46 void setRed(float);
47
49 constexpr float getGreen() const {
50 return green;
51 }
53 void setGreen(float);
54
56 constexpr float getBlue() const {
57 return blue;
58 }
60 void setBlue(float);
61
63 constexpr float getAlpha() const {
64 return alpha;
65 }
67 void setAlpha(float);
68 void setAlpha(Alpha);
69
71 void setRgb(Rgb color);
72
74 explicit operator Rgb() const;
75
76private:
77 float red;
78 float green;
79 float blue;
80 float alpha;
81};
82
84Rgba mix(Rgba a, Rgba b, float t);
85
87[[deprecated("Use mix instead")]]
88inline Rgba interpolate(Rgba a, Rgba b, float t) {
89 return mix(a, b, t);
90}
91
92} // namespace jngl
93
95jngl::Rgba operator""_rgba(unsigned long long);
96
97namespace jngl::internal {
98// Intentionally declared but not defined — see Rgb.hpp for the pattern.
99void invalid_rgba_color_literal_expected_format_hash_rrggbbaa();
100} // namespace jngl::internal
101
103consteval jngl::Rgba operator""_rgba(const char* hex, size_t length) {
104 using jngl::internal::hexByte;
105 if (length == 9 && hex[0] == '#') {
106 return jngl::Rgba::u8(hexByte(hex + 1), hexByte(hex + 3), hexByte(hex + 5),
107 hexByte(hex + 7));
108 }
109 jngl::internal::invalid_rgba_color_literal_expected_format_hash_rrggbbaa();
110 return jngl::Rgba{ 0, 0, 0, 0 };
111}
Contains jngl::Rgb class.
Object representing only the Alpha Channel in an RGBA color, 0 meaning invisible, 1 fully visible.
Definition Alpha.hpp:12
Object representing a RGB color, new version of jngl::Color (which will be deprecated in the future)
Definition Rgb.hpp:21
Object representing a RGBA color.
Definition Rgba.hpp:22
void setRgb(Rgb color)
Overwrites red, green and blue, but leaves the alpha value untouched.
void setBlue(float)
0.0f ... 1.0f
constexpr float getGreen() const
0.0f ... 1.0fu
Definition Rgba.hpp:49
constexpr Rgba(float red, float green, float blue, float alpha)
Definition Rgba.hpp:30
constexpr float getRed() const
0.0f ... 1.0f
Definition Rgba.hpp:42
void setRed(float)
0.0f ... 1.0f
void setGreen(float)
0.0f ... 1.0f
constexpr float getBlue() const
0.0f ... 1.0f
Definition Rgba.hpp:56
constexpr float getAlpha() const
0.0f ... 1.0f
Definition Rgba.hpp:63
void setAlpha(float)
0.0f ... 1.0f
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 mix(Rgb a, Rgb b, float t)
Returns a color mix between a (t == 0) and b (t == 1)