FastEngine 0.9.3
A multiplayer oriented 2D engine made with Vulkan.
Loading...
Searching...
No Matches
C_color.hpp
1/*
2 * Copyright 2024 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 Color() noexcept :
38 _r(0),
39 _g(0),
40 _b(0),
41 _a(255)
42 {}
43 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 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 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 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 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 operator SDL_Color() const { return SDL_Color{.r = this->_r, .g = this->_g, .b = this->_b, .a = this->_a}; }
75 operator VkClearColorValue() const
76 {
77 return {{static_cast<float>(this->_r) / 255.0f, static_cast<float>(this->_g) / 255.0f,
78 static_cast<float>(this->_b) / 255.0f, static_cast<float>(this->_a) / 255.0f}};
79 }
80 operator glm::vec4() const
81 {
82 return {static_cast<float>(this->_r) / 255.0f, static_cast<float>(this->_g) / 255.0f,
83 static_cast<float>(this->_b) / 255.0f, static_cast<float>(this->_a) / 255.0f};
84 }
85 operator glm::uvec4() const { return {this->_r, this->_g, this->_b, this->_a}; }
86 operator uint32_t() const { return this->toInteger(); }
87
88 [[nodiscard]] uint32_t toInteger() const
89 {
90 return (static_cast<uint32_t>(this->_r) << 24) | (static_cast<uint32_t>(this->_g) << 16) |
91 (static_cast<uint32_t>(this->_b) << 8) | static_cast<uint32_t>(this->_a);
92 }
93
94 bool operator==(Color const& right) const
95 {
96 return this->_r == right._r && this->_g == right._g && this->_b == right._b && this->_a == right._a;
97 }
98 bool operator!=(Color const& right) const { return !this->operator==(right); }
99
100 Color operator+(Color const& right) const
101 {
102 uint16_t const red = static_cast<uint16_t>(this->_r) + right._r;
103 uint16_t const green = static_cast<uint16_t>(this->_g) + right._g;
104 uint16_t const blue = static_cast<uint16_t>(this->_b) + right._b;
105 uint16_t const alpha = static_cast<uint16_t>(this->_a) + right._a;
106 return {red > 255 ? uint8_t(255) : static_cast<uint8_t>(red),
107 green > 255 ? uint8_t(255) : static_cast<uint8_t>(green),
108 blue > 255 ? uint8_t(255) : static_cast<uint8_t>(blue),
109 alpha > 255 ? uint8_t(255) : static_cast<uint8_t>(alpha)};
110 }
111 Color operator-(Color const& right) const
112 {
113 auto const red = static_cast<int16_t>(this->_r - right._r);
114 auto const green = static_cast<int16_t>(this->_g - right._g);
115 auto const blue = static_cast<int16_t>(this->_b - right._b);
116 auto const alpha = static_cast<int16_t>(this->_a - right._a);
117 return {red < 0 ? uint8_t(0) : static_cast<uint8_t>(red), green < 0 ? uint8_t(0) : static_cast<uint8_t>(green),
118 blue < 0 ? uint8_t(0) : static_cast<uint8_t>(blue),
119 alpha < 0 ? uint8_t(0) : static_cast<uint8_t>(alpha)};
120 }
121 Color operator*(Color const& right) const
122 {
123 uint16_t const red = static_cast<uint16_t>(this->_r) * right._r;
124 uint16_t const green = static_cast<uint16_t>(this->_g) * right._g;
125 uint16_t const blue = static_cast<uint16_t>(this->_b) * right._b;
126 uint16_t const alpha = static_cast<uint16_t>(this->_a) * right._a;
127 return {static_cast<uint8_t>(red / 255), static_cast<uint8_t>(green / 255), static_cast<uint8_t>(blue / 255),
128 static_cast<uint8_t>(alpha / 255)};
129 }
130
131 Color& operator+=(Color const& right) { return this->operator=(this->operator+(right)); }
132 Color& operator-=(Color const& right) { return this->operator=(this->operator-(right)); }
133 Color& operator*=(Color const& right) { return this->operator=(this->operator*(right)); }
134
135 uint8_t _r;
136 uint8_t _g;
137 uint8_t _b;
138 uint8_t _a;
139
140 static Color const Black;
141 static Color const White;
142 static Color const Red;
143 static Color const Green;
144 static Color const Blue;
145 static Color const Yellow;
146 static Color const Magenta;
147 static Color const Cyan;
148 static Color const Transparent;
149};
150
151inline Color const Color::Black(0, 0, 0);
152inline Color const Color::White(255, 255, 255);
153inline Color const Color::Red(255, 0, 0);
154inline Color const Color::Green(0, 255, 0);
155inline Color const Color::Blue(0, 0, 255);
156inline Color const Color::Yellow(255, 255, 0);
157inline Color const Color::Magenta(255, 0, 255);
158inline Color const Color::Cyan(0, 255, 255);
159inline Color const Color::Transparent(0, 0, 0, 0);
160
161} // namespace fge
162
163#endif // _FGE_GRAPHIC_C_COLOR_HPP_INCLUDED
Definition C_color.hpp:35
static Color const Black
Black predefined color.
Definition C_color.hpp:140
static Color const Yellow
Yellow predefined color.
Definition C_color.hpp:145
static Color const Blue
Blue predefined color.
Definition C_color.hpp:144
static Color const Magenta
Magenta predefined color.
Definition C_color.hpp:146
static Color const Red
Red predefined color.
Definition C_color.hpp:142
static Color const Transparent
Transparent (black) predefined color.
Definition C_color.hpp:148
static Color const Cyan
Cyan predefined color.
Definition C_color.hpp:147
static Color const White
White predefined color.
Definition C_color.hpp:141
static Color const Green
Green predefined color.
Definition C_color.hpp:143