1 /* Copyright (C) 2022 Wildfire Games.
2 * This file is part of 0 A.D.
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.
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.
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/>.
18 #ifndef INCLUDED_COLOR
19 #define INCLUDED_COLOR
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"
27 // Simple defines for 3 and 4 component floating point colors - just map to
28 // corresponding vector types.
29 typedef CVector3D RGBColor
;
30 typedef CVector4D RGBAColor
;
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.
35 extern SColor4ub (*ConvertRGBColorTo4ub
)(const RGBColor
& src
);
38 * Detects CPU caps and activates the best possible codepath.
40 extern void ColorActivateFastImpl();
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
) {}
48 * Returns whether this has been set to a valid color.
52 return r
>= 0 && g
>= 0 && b
>= 0 && a
>= 0;
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".
61 bool ParseString(const CStr8
& value
, int defaultAlpha
= 255);
63 bool operator==(const CColor
& color
) const;
64 bool operator!=(const CColor
& color
) const
66 return !(*this == color
);
69 // For passing to uniform as vec3/vec4.
70 PS::span
<const float> AsFloatArray() const
72 // Additional check to prevent a weird compiler has a different
73 // alignement for an array and a class members.
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);
84 // For passing to CRenderer:
85 SColor4ub
AsSColor4ub() const
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
)
98 #endif // INCLUDED_COLOR