LCOV - code coverage report
Current view: top level - source/graphics - Color.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 2 15 13.3 %
Date: 2023-01-19 00:18:29 Functions: 2 6 33.3 %

          Line data    Source code
       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.
      29             : typedef CVector3D RGBColor;
      30             : typedef CVector4D RGBAColor;
      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.
      35             : extern SColor4ub (*ConvertRGBColorTo4ub)(const RGBColor& src);
      36             : 
      37             : /**
      38             :  * Detects CPU caps and activates the best possible codepath.
      39             :  */
      40             : extern void ColorActivateFastImpl();
      41             : 
      42             : struct CColor
      43             : {
      44        1975 :     CColor() : r(-1.f), g(-1.f), b(-1.f), a(1.f) {}
      45          84 :     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           0 :     operator bool() const
      51             :     {
      52           0 :         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           0 :     bool operator!=(const CColor& color) const
      65             :     {
      66           0 :         return !(*this == color);
      67             :     }
      68             : 
      69             :     // For passing to uniform as vec3/vec4.
      70           0 :     PS::span<const float> AsFloatArray() const
      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           0 :         return PS::span(&r, 4);
      82             :     }
      83             : 
      84             :     // For passing to CRenderer:
      85           0 :     SColor4ub AsSColor4ub() const
      86             :     {
      87           0 :         return SColor4ub(
      88           0 :             static_cast<u8>(r * 255.f),
      89           0 :             static_cast<u8>(g * 255.f),
      90           0 :             static_cast<u8>(b * 255.f),
      91           0 :             static_cast<u8>(a * 255.f)
      92           0 :         );
      93             :     }
      94             : 
      95             :     float r, g, b, a;
      96             : };
      97             : 
      98             : #endif // INCLUDED_COLOR

Generated by: LCOV version 1.13