FastEngine 0.9.5
A multiplayer oriented 2D engine made with Vulkan.
Loading...
Searching...
No Matches
C_color.hpp
1/*
2 * Copyright 2026 Guillaume Guillet
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _FGE_GRAPHIC_C_COLOR_HPP_INCLUDED
18#define _FGE_GRAPHIC_C_COLOR_HPP_INCLUDED
19
20/*
21 * Original from : https://github.com/SFML/SFML
22 * Copyright (C) 2007-2022 Laurent Gomila
23 *
24 * Altered/Modified by Guillaume Guillet
25 */
26
27#include "volk.h"
28#include "FastEngine/C_vector.hpp"
29#include "SDL_pixels.h"
30
31namespace fge
32{
33
34class Color
35{
36public:
37 constexpr Color() noexcept :
38 _r(0),
39 _g(0),
40 _b(0),
41 _a(255)
42 {}
43 constexpr explicit Color(SDL_Color const& sdlColor) noexcept :
44 _r(sdlColor.r),
45 _g(sdlColor.g),
46 _b(sdlColor.b),
47 _a(sdlColor.a)
48 {}
49 constexpr explicit Color(VkClearColorValue const& clearColorValue) noexcept :
50 _r(static_cast<uint8_t>(clearColorValue.float32[0] * 255.0f)),
51 _g(static_cast<uint8_t>(clearColorValue.float32[1] * 255.0f)),
52 _b(static_cast<uint8_t>(clearColorValue.float32[2] * 255.0f)),
53 _a(static_cast<uint8_t>(clearColorValue.float32[3] * 255.0f))
54 {}
55 constexpr explicit Color(glm::vec4 const& vec4) noexcept :
56 _r(static_cast<uint8_t>(vec4.r * 255.0f)),
57 _g(static_cast<uint8_t>(vec4.g * 255.0f)),
58 _b(static_cast<uint8_t>(vec4.b * 255.0f)),
59 _a(static_cast<uint8_t>(vec4.a * 255.0f))
60 {}
61 constexpr Color(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha = 255) noexcept :
62 _r(red),
63 _g(green),
64 _b(blue),
65 _a(alpha)
66 {}
67 constexpr explicit Color(uint32_t color) noexcept :
68 _r(static_cast<uint8_t>((color & 0xFF000000) >> 24)),
69 _g(static_cast<uint8_t>((color & 0x00FF0000) >> 16)),
70 _b(static_cast<uint8_t>((color & 0x0000FF00) >> 8)),
71 _a(static_cast<uint8_t>(color & 0x000000FF))
72 {}
73
74 constexpr operator SDL_Color() const
75 {
76 return SDL_Color{.r = this->_r, .g = this->_g, .b = this->_b, .a = this->_a};
77 }
78 constexpr operator VkClearColorValue() const
79 {
80 return {{static_cast<float>(this->_r) / 255.0f, static_cast<float>(this->_g) / 255.0f,
81 static_cast<float>(this->_b) / 255.0f, static_cast<float>(this->_a) / 255.0f}};
82 }
83 constexpr operator glm::vec4() const
84 {
85 return {static_cast<float>(this->_r) / 255.0f, static_cast<float>(this->_g) / 255.0f,
86 static_cast<float>(this->_b) / 255.0f, static_cast<float>(this->_a) / 255.0f};
87 }
88 constexpr operator glm::uvec4() const { return {this->_r, this->_g, this->_b, this->_a}; }
89 constexpr operator uint32_t() const { return this->toInteger(); }
90
91 [[nodiscard]] constexpr uint32_t toInteger() const
92 {
93 return (static_cast<uint32_t>(this->_r) << 24) | (static_cast<uint32_t>(this->_g) << 16) |
94 (static_cast<uint32_t>(this->_b) << 8) | static_cast<uint32_t>(this->_a);
95 }
96
97 constexpr bool operator==(Color const& right) const
98 {
99 return this->_r == right._r && this->_g == right._g && this->_b == right._b && this->_a == right._a;
100 }
101 constexpr bool operator!=(Color const& right) const { return !this->operator==(right); }
102
103 constexpr Color operator+(Color const& right) const
104 {
105 uint16_t const red = static_cast<uint16_t>(this->_r) + right._r;
106 uint16_t const green = static_cast<uint16_t>(this->_g) + right._g;
107 uint16_t const blue = static_cast<uint16_t>(this->_b) + right._b;
108 uint16_t const alpha = static_cast<uint16_t>(this->_a) + right._a;
109 return {red > 255 ? uint8_t(255) : static_cast<uint8_t>(red),
110 green > 255 ? uint8_t(255) : static_cast<uint8_t>(green),
111 blue > 255 ? uint8_t(255) : static_cast<uint8_t>(blue),
112 alpha > 255 ? uint8_t(255) : static_cast<uint8_t>(alpha)};
113 }
114 constexpr Color operator-(Color const& right) const
115 {
116 auto const red = static_cast<int16_t>(this->_r - right._r);
117 auto const green = static_cast<int16_t>(this->_g - right._g);
118 auto const blue = static_cast<int16_t>(this->_b - right._b);
119 auto const alpha = static_cast<int16_t>(this->_a - right._a);
120 return {red < 0 ? uint8_t(0) : static_cast<uint8_t>(red), green < 0 ? uint8_t(0) : static_cast<uint8_t>(green),
121 blue < 0 ? uint8_t(0) : static_cast<uint8_t>(blue),
122 alpha < 0 ? uint8_t(0) : static_cast<uint8_t>(alpha)};
123 }
124 constexpr Color operator*(Color const& right) const
125 {
126 uint16_t const red = static_cast<uint16_t>(this->_r) * right._r;
127 uint16_t const green = static_cast<uint16_t>(this->_g) * right._g;
128 uint16_t const blue = static_cast<uint16_t>(this->_b) * right._b;
129 uint16_t const alpha = static_cast<uint16_t>(this->_a) * right._a;
130 return {static_cast<uint8_t>(red / 255), static_cast<uint8_t>(green / 255), static_cast<uint8_t>(blue / 255),
131 static_cast<uint8_t>(alpha / 255)};
132 }
133
134 constexpr Color& operator+=(Color const& right) { return this->operator=(this->operator+(right)); }
135 constexpr Color& operator-=(Color const& right) { return this->operator=(this->operator-(right)); }
136 constexpr Color& operator*=(Color const& right) { return this->operator=(this->operator*(right)); }
137
138 [[nodiscard]] constexpr Color setAlpha(uint8_t alpha) const
139 {
140 Color copy = *this;
141 copy._a = alpha;
142 return copy;
143 }
144 [[nodiscard]] constexpr Color setRed(uint8_t red) const
145 {
146 Color copy = *this;
147 copy._r = red;
148 return copy;
149 }
150 [[nodiscard]] constexpr Color setGreen(uint8_t green) const
151 {
152 Color copy = *this;
153 copy._g = green;
154 return copy;
155 }
156 [[nodiscard]] constexpr Color setBlue(uint8_t blue) const
157 {
158 Color copy = *this;
159 copy._b = blue;
160 return copy;
161 }
162
163 uint8_t _r;
164 uint8_t _g;
165 uint8_t _b;
166 uint8_t _a;
167
168 static Color const Black;
169 static Color const White;
170 static Color const Red;
171 static Color const Green;
172 static Color const Blue;
173 static Color const Yellow;
174 static Color const Magenta;
175 static Color const Cyan;
176 static Color const Transparent;
177};
178
179inline Color const Color::Black(0, 0, 0);
180inline Color const Color::White(255, 255, 255);
181inline Color const Color::Red(255, 0, 0);
182inline Color const Color::Green(0, 255, 0);
183inline Color const Color::Blue(0, 0, 255);
184inline Color const Color::Yellow(255, 255, 0);
185inline Color const Color::Magenta(255, 0, 255);
186inline Color const Color::Cyan(0, 255, 255);
187inline Color const Color::Transparent(0, 0, 0, 0);
188
189} // namespace fge
190
191#endif // _FGE_GRAPHIC_C_COLOR_HPP_INCLUDED
Definition C_color.hpp:35
static Color const Black
Black predefined color.
Definition C_color.hpp:168
static Color const Yellow
Yellow predefined color.
Definition C_color.hpp:173
static Color const Blue
Blue predefined color.
Definition C_color.hpp:172
static Color const Magenta
Magenta predefined color.
Definition C_color.hpp:174
static Color const Red
Red predefined color.
Definition C_color.hpp:170
static Color const Transparent
Transparent (black) predefined color.
Definition C_color.hpp:176
static Color const Cyan
Cyan predefined color.
Definition C_color.hpp:175
static Color const White
White predefined color.
Definition C_color.hpp:169
static Color const Green
Green predefined color.
Definition C_color.hpp:171