Pyrogenesis trunk
Color.h
Go to the documentation of this file.
1/* Copyright (C) 2022 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#include "ps/containers/Span.h"
25#include "ps/CStrForward.h"
26
27// Simple defines for 3 and 4 component floating point colors - just map to
28// corresponding vector types.
31
32// Convert float RGB(A) colors to unsigned byte.
33// Exposed as function pointer because it is set at init-time to
34// one of several implementations depending on CPU caps.
35extern SColor4ub (*ConvertRGBColorTo4ub)(const RGBColor& src);
36
37/**
38 * Detects CPU caps and activates the best possible codepath.
39 */
40extern void ColorActivateFastImpl();
41
42struct CColor
43{
44 CColor() : r(-1.f), g(-1.f), b(-1.f), a(1.f) {}
45 CColor(float cr, float cg, float cb, float ca) : r(cr), g(cg), b(cb), a(ca) {}
46
47 /**
48 * Returns whether this has been set to a valid color.
49 */
50 operator bool() const
51 {
52 return r >= 0 && g >= 0 && b >= 0 && a >= 0;
53 }
54
55 /**
56 * Try to parse @p Value as a color. Returns true on success, false otherwise.
57 * Leaves the color unchanged if it failed.
58 * @param value Should be "r g b" or "r g b a" where each value is an integer in [0,255].
59 * @param defaultAlpha The alpha value that is used if the format of @p Value is "r g b".
60 */
61 bool ParseString(const CStr8& value, int defaultAlpha = 255);
62
63 bool operator==(const CColor& color) const;
64 bool operator!=(const CColor& color) const
65 {
66 return !(*this == color);
67 }
68
69 // For passing to uniform as vec3/vec4.
71 {
72 // Additional check to prevent a weird compiler has a different
73 // alignement for an array and a class members.
74 static_assert(
75 sizeof(CColor) == sizeof(float) * 4u &&
76 offsetof(CColor, r) == 0 &&
77 offsetof(CColor, g) == sizeof(float) &&
78 offsetof(CColor, b) == sizeof(float) * 2u &&
79 offsetof(CColor, a) == sizeof(float) * 3u,
80 "CColor should be properly layouted to use AsFloatArray");
81 return PS::span(&r, 4);
82 }
83
84 // For passing to CRenderer:
86 {
87 return SColor4ub(
88 static_cast<u8>(r * 255.f),
89 static_cast<u8>(g * 255.f),
90 static_cast<u8>(b * 255.f),
91 static_cast<u8>(a * 255.f)
92 );
93 }
94
95 float r, g, b, a;
96};
97
98#endif // INCLUDED_COLOR
void ColorActivateFastImpl()
Detects CPU caps and activates the best possible codepath.
Definition: Color.cpp:78
SColor4ub(* ConvertRGBColorTo4ub)(const RGBColor &src)
Definition: Color.cpp:43
CVector4D RGBAColor
Definition: Color.h:30
CVector3D RGBColor
Definition: Color.h:29
Definition: Vector3D.h:31
Definition: Vector4D.h:31
Simplifed version of std::span (C++20) as we don't support the original one yet.
Definition: Span.h:37
span(const std::array< T, N > &) -> span< const T >
Definition: Color.h:43
bool ParseString(const CStr8 &value, int defaultAlpha=255)
Try to parse Value as a color.
Definition: Color.cpp:94
bool operator==(const CColor &color) const
Definition: Color.cpp:139
PS::span< const float > AsFloatArray() const
Definition: Color.h:70
float g
Definition: Color.h:95
bool operator!=(const CColor &color) const
Definition: Color.h:64
float a
Definition: Color.h:95
CColor(float cr, float cg, float cb, float ca)
Definition: Color.h:45
float b
Definition: Color.h:95
float r
Definition: Color.h:95
CColor()
Definition: Color.h:44
SColor4ub AsSColor4ub() const
Definition: Color.h:85
Definition: SColor.h:31
uint8_t u8
Definition: types.h:37