File: Color.h

package info (click to toggle)
0ad 0.0.26-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 130,460 kB
  • sloc: cpp: 261,824; ansic: 198,392; javascript: 19,067; python: 14,557; sh: 7,629; perl: 4,072; xml: 849; makefile: 741; java: 533; ruby: 229; php: 190; pascal: 30; sql: 21; tcl: 4
file content (98 lines) | stat: -rw-r--r-- 2,987 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* Copyright (C) 2022 Wildfire Games.
 * This file is part of 0 A.D.
 *
 * 0 A.D. is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * 0 A.D. is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef INCLUDED_COLOR
#define INCLUDED_COLOR

#include "graphics/SColor.h"
#include "maths/Vector3D.h"
#include "maths/Vector4D.h"
#include "ps/containers/Span.h"
#include "ps/CStrForward.h"

// Simple defines for 3 and 4 component floating point colors - just map to
// corresponding vector types.
typedef CVector3D RGBColor;
typedef CVector4D RGBAColor;

// Convert float RGB(A) colors to unsigned byte.
// Exposed as function pointer because it is set at init-time to
// one of several implementations depending on CPU caps.
extern SColor4ub (*ConvertRGBColorTo4ub)(const RGBColor& src);

/**
 * Detects CPU caps and activates the best possible codepath.
 */
extern void ColorActivateFastImpl();

struct CColor
{
	CColor() : r(-1.f), g(-1.f), b(-1.f), a(1.f) {}
	CColor(float cr, float cg, float cb, float ca) : r(cr), g(cg), b(cb), a(ca) {}

	/**
	 * Returns whether this has been set to a valid color.
	 */
	operator bool() const
	{
		return r >= 0 && g >= 0 && b >= 0 && a >= 0;
	}

	/**
	 * Try to parse @p Value as a color. Returns true on success, false otherwise.
	 * Leaves the color unchanged if it failed.
	 * @param value Should be "r g b" or "r g b a" where each value is an integer in [0,255].
	 * @param defaultAlpha The alpha value that is used if the format of @p Value is "r g b".
	 */
	bool ParseString(const CStr8& value, int defaultAlpha = 255);

	bool operator==(const CColor& color) const;
	bool operator!=(const CColor& color) const
	{
		return !(*this == color);
	}

	// For passing to uniform as vec3/vec4.
	PS::span<const float> AsFloatArray() const
	{
		// Additional check to prevent a weird compiler has a different
		// alignement for an array and a class members.
		static_assert(
			sizeof(CColor) == sizeof(float) * 4u &&
			offsetof(CColor, r) == 0 &&
			offsetof(CColor, g) == sizeof(float) &&
			offsetof(CColor, b) == sizeof(float) * 2u &&
			offsetof(CColor, a) == sizeof(float) * 3u,
			"CColor should be properly layouted to use AsFloatArray");
		return PS::span(&r, 4);
	}

	// For passing to CRenderer:
	SColor4ub AsSColor4ub() const
	{
		return SColor4ub(
			static_cast<u8>(r * 255.f),
			static_cast<u8>(g * 255.f),
			static_cast<u8>(b * 255.f),
			static_cast<u8>(a * 255.f)
		);
	}

	float r, g, b, a;
};

#endif // INCLUDED_COLOR