Line data Source code
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 : 28 : class CStr8; 29 : 30 : // Simple defines for 3 and 4 component floating point colors - just map to 31 : // corresponding vector types. 32 : typedef CVector3D RGBColor; 33 : typedef CVector4D RGBAColor; 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. 38 : extern SColor4ub (*ConvertRGBColorTo4ub)(const RGBColor& src); 39 : 40 : /** 41 : * Detects CPU caps and activates the best possible codepath. 42 : */ 43 : extern void ColorActivateFastImpl(); 44 : 45 : struct CColor 46 : { 47 2064 : constexpr CColor() : r(-1.f), g(-1.f), b(-1.f), a(1.f) {} 48 408 : 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 0 : operator bool() const 54 : { 55 0 : 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 0 : bool operator!=(const CColor& color) const 68 : { 69 0 : return !(*this == color); 70 : } 71 : 72 : // For passing to uniform as vec3/vec4. 73 0 : 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 0 : 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 0 : return std::span<const float, 4>(&r, 4); 85 : } 86 : 87 : // For passing to CRenderer: 88 0 : SColor4ub AsSColor4ub() const 89 : { 90 0 : return SColor4ub( 91 0 : static_cast<std::uint8_t>(r * 255.f), 92 0 : static_cast<std::uint8_t>(g * 255.f), 93 0 : static_cast<std::uint8_t>(b * 255.f), 94 0 : static_cast<std::uint8_t>(a * 255.f) 95 : ); 96 : } 97 : 98 : float r, g, b, a; 99 : }; 100 : 101 : #endif // INCLUDED_COLOR