File: Color.cpp

package info (click to toggle)
jazz2-native 3.5.0-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 16,912 kB
  • sloc: cpp: 172,557; xml: 113; python: 36; makefile: 5; sh: 2
file content (205 lines) | stat: -rw-r--r-- 4,923 bytes parent folder | download
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include "Color.h"
#include "Colorf.h"

#include <algorithm>

namespace nCine
{
	inline namespace Primitives
	{
		const Color Color::Black(0, 0, 0, 255);
		const Color Color::White(255, 255, 255, 255);
		const Color Color::Red(255, 0, 0, 255);
		const Color Color::Green(0, 255, 0, 255);
		const Color Color::Blue(0, 0, 255, 255);
		const Color Color::Yellow(255, 255, 0, 255);
		const Color Color::Magenta(255, 0, 255, 255);
		const Color Color::Cyan(0, 255, 255, 255);

		Color::Color(std::uint32_t hex)
		{
			SetAlpha(255);
			// The following method might set the alpha channel
			Set(hex);
		}

		Color::Color(const std::uint8_t channels[NumChannels])
		{
			SetVec(channels);
		}

		Color::Color(const Colorf& color)
		{
			R = std::uint8_t(color.R * 255);
			G = std::uint8_t(color.G * 255);
			B = std::uint8_t(color.B * 255);
			A = std::uint8_t(color.A * 255);
		}

		std::uint32_t Color::Rgba() const
		{
			return (R << 24) | (G << 16) | (B << 8) | A;
		}

		std::uint32_t Color::Argb() const
		{
			return (A << 24) | (R << 16) | (G << 8) | B;
		}

		std::uint32_t Color::Abgr() const
		{
			return (A << 24) | (B << 16) | (G << 8) | R;
		}

		std::uint32_t Color::Bgra() const
		{
			return (B << 24) | (G << 16) | (R << 8) | A;
		}

		void Color::Set(std::uint8_t red, std::uint8_t green, std::uint8_t blue)
		{
			R = red;
			G = green;
			B = blue;
		}

		void Color::Set(std::uint32_t hex)
		{
			R = static_cast<std::uint8_t>((hex & 0xFF0000) >> 16);
			G = static_cast<std::uint8_t>((hex & 0xFF00) >> 8);
			B = static_cast<std::uint8_t>(hex & 0xFF);

			if (hex > 0xFFFFFF) {
				A = static_cast<std::uint8_t>((hex & 0xFF000000) >> 24);
			}
		}

		void Color::SetVec(const std::uint8_t channels[NumChannels])
		{
			Set(channels[0], channels[1], channels[2], channels[3]);
		}

		void Color::SetAlpha(std::uint8_t alpha)
		{
			A = alpha;
		}

		Color& Color::operator=(const Colorf& color)
		{
			R = std::uint8_t(color.R * 255.0f);
			G = std::uint8_t(color.G * 255.0f);
			B = std::uint8_t(color.B * 255.0f);
			A = std::uint8_t(color.A * 255.0f);

			return *this;
		}

		bool Color::operator==(const Color& color) const
		{
			return (R == color.R && G == color.G &&
					B == color.B && A == color.A);
		}

		bool Color::operator!=(const Color& color) const
		{
			return (R != color.R || G != color.G ||
					B != color.B || A != color.A);
		}

		Color& Color::operator+=(const Color& color)
		{
			for (std::uint32_t i = 0; i < NumChannels; i++) {
				std::uint32_t channelValue = Data()[i] + color.Data()[i];
				channelValue = std::clamp(channelValue, 0U, 255U);
				Data()[i] = static_cast<std::uint8_t>(channelValue);
			}

			return *this;
		}

		Color& Color::operator-=(const Color& color)
		{
			for (std::uint32_t i = 0; i < NumChannels; i++) {
				std::uint32_t channelValue = Data()[i] - color.Data()[i];
				channelValue = std::clamp(channelValue, 0U, 255U);
				Data()[i] = static_cast<std::uint8_t>(channelValue);
			}

			return *this;
		}

		Color& Color::operator*=(const Color& color)
		{
			for (std::uint32_t i = 0; i < NumChannels; i++) {
				float channelValue = Data()[i] * (color.Data()[i] / 255.0f);
				channelValue = std::clamp(channelValue, 0.0f, 255.0f);
				Data()[i] = static_cast<std::uint8_t>(channelValue);
			}

			return *this;
		}

		Color& Color::operator*=(float scalar)
		{
			for (std::uint32_t i = 0; i < NumChannels; i++) {
				float channelValue = Data()[i] * scalar;
				channelValue = std::clamp(channelValue, 0.0f, 255.0f);
				Data()[i] = static_cast<std::uint8_t>(channelValue);
			}

			return *this;
		}

		Color Color::operator+(const Color& color) const
		{
			Color result;

			for (std::uint32_t i = 0; i < NumChannels; i++) {
				std::uint32_t channelValue = Data()[i] + color.Data()[i];
				channelValue = std::clamp(channelValue, 0U, 255U);
				result.Data()[i] = static_cast<std::uint8_t>(channelValue);
			}

			return result;
		}

		Color Color::operator-(const Color& color) const
		{
			Color result;

			for (std::uint32_t i = 0; i < NumChannels; i++) {
				std::uint32_t channelValue = Data()[i] - color.Data()[i];
				channelValue = std::clamp(channelValue, 0U, 255U);
				result.Data()[i] = static_cast<std::uint8_t>(channelValue);
			}

			return result;
		}

		Color Color::operator*(const Color& color) const
		{
			Color result;

			for (std::uint32_t i = 0; i < NumChannels; i++) {
				float channelValue = Data()[i] * (color.Data()[i] / 255.0f);
				channelValue = std::clamp(channelValue, 0.0f, 255.0f);
				result.Data()[i] = static_cast<std::uint8_t>(channelValue);
			}

			return result;
		}

		Color Color::operator*(float scalar) const
		{
			Color result;

			for (std::uint32_t i = 0; i < NumChannels; i++) {
				float channelValue = Data()[i] * scalar;
				channelValue = std::clamp(channelValue, 0.0f, 255.0f);
				result.Data()[i] = static_cast<std::uint8_t>(channelValue);
			}

			return result;
		}
	}
}