Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
Color.h
Go to the documentation of this file.
1/* Copyright (C) 2026 Wildfire Games.
2 * This file is part of 0 A.D.
3 *
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef INCLUDED_COLOR
19#define INCLUDED_COLOR
20
21#include "graphics/SColor.h"
22#include "maths/Vector3D.h"
23#include "maths/Vector4D.h"
24
25#include <span>
26#include <cstddef>
27
28class CStr8;
29
30// Simple defines for 3 and 4 component floating point colors - just map to
31// corresponding vector types.
34
35// Convert float RGB(A) colors to unsigned byte.
36// Exposed as function pointer because it is set at init-time to
37// one of several implementations depending on CPU caps.
38extern SColor4ub (*ConvertRGBColorTo4ub)(const RGBColor& src);
39
40/**
41 * Detects CPU caps and activates the best possible codepath.
42 */
43extern void ColorActivateFastImpl();
44
45struct CColor
46{
47 constexpr CColor() : r(-1.f), g(-1.f), b(-1.f), a(1.f) {}
48 constexpr CColor(float cr, float cg, float cb, float ca) : r(cr), g(cg), b(cb), a(ca) {}
49
50 /**
51 * Returns whether this has been set to a valid color.
52 */
53 operator bool() const
54 {
55 return r >= 0 && g >= 0 && b >= 0 && a >= 0;
56 }
57
58 /**
59 * Try to parse @p Value as a color. Returns true on success, false otherwise.
60 * Leaves the color unchanged if it failed.
61 * @param value Should be "r g b" or "r g b a" where each value is an integer in [0,255].
62 * @param defaultAlpha The alpha value that is used if the format of @p Value is "r g b".
63 */
64 bool ParseString(const CStr8& value, int defaultAlpha = 255);
65
66 bool operator==(const CColor& color) const;
67 bool operator!=(const CColor& color) const
68 {
69 return !(*this == color);
70 }
71
72 // For passing to uniform as vec3/vec4.
73 std::span<const float, 4> AsFloatArray() const
74 {
75 // Additional check to prevent a weird compiler has a different
76 // alignement for an array and a class members.
77 static_assert(
78 sizeof(CColor) == sizeof(float) * 4u &&
79 offsetof(CColor, r) == 0 &&
80 offsetof(CColor, g) == sizeof(float) &&
81 offsetof(CColor, b) == sizeof(float) * 2u &&
82 offsetof(CColor, a) == sizeof(float) * 3u,
83 "CColor should be properly layouted to use AsFloatArray");
84 return std::span<const float, 4>(&r, 4);
85 }
86
87 // For passing to CRenderer:
89 {
90 return SColor4ub(
91 static_cast<std::uint8_t>(r * 255.f),
92 static_cast<std::uint8_t>(g * 255.f),
93 static_cast<std::uint8_t>(b * 255.f),
94 static_cast<std::uint8_t>(a * 255.f)
95 );
96 }
97
98 float r, g, b, a;
99};
100
101#endif // INCLUDED_COLOR
void ColorActivateFastImpl()
Detects CPU caps and activates the best possible codepath.
Definition: Color.cpp:81
SColor4ub(* ConvertRGBColorTo4ub)(const RGBColor &src)
Definition: Color.cpp:46
CVector4D RGBAColor
Definition: Color.h:33
CVector3D RGBColor
Definition: Color.h:32
Definition: Vector3D.h:32
Definition: Vector4D.h:30
Definition: Color.h:46
bool ParseString(const CStr8 &value, int defaultAlpha=255)
Try to parse Value as a color.
Definition: Color.cpp:97
constexpr CColor()
Definition: Color.h:47
constexpr CColor(float cr, float cg, float cb, float ca)
Definition: Color.h:48
bool operator==(const CColor &color) const
Definition: Color.cpp:143
float g
Definition: Color.h:98
bool operator!=(const CColor &color) const
Definition: Color.h:67
float a
Definition: Color.h:98
float b
Definition: Color.h:98
float r
Definition: Color.h:98
std::span< const float, 4 > AsFloatArray() const
Definition: Color.h:73
SColor4ub AsSColor4ub() const
Definition: Color.h:88
Definition: SColor.h:31