LIBEUNOMIA
読み取り中…
検索中…
一致する文字列を見つけられません
colour.h
[詳解]
1/*
2 * Copyright 2001-2021 oZ/acy (名賀月晃嗣)
3 * Redistribution and use in source and binary forms,
4 * with or without modification,
5 * are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
26 */
42#ifndef INCLUDE_GUARD_EUNOMIA_COLOUR_H
43#define INCLUDE_GUARD_EUNOMIA_COLOUR_H
44
45#include <algorithm>
46#include <cstdint>
47
48namespace eunomia
49{
54{
55public:
56 std::uint8_t red;
57 std::uint8_t green;
58 std::uint8_t blue;
59
60public:
64 constexpr RgbColour() noexcept : red(0), green(0), blue(0) {}
65
69 constexpr
70 RgbColour(std::uint8_t r, std::uint8_t g, std::uint8_t b) noexcept
71 : red(r), green(g), blue(b)
72 {}
73
75 constexpr
76 bool operator==(const RgbColour& other) const noexcept
77 { return red == other.red && green == other.green && blue == other.blue; }
78
80 constexpr
81 bool operator!=(const RgbColour& other) const noexcept
82 { return !(*this == other); }
83};
84
85
90{
91public:
92 std::uint8_t red;
93 std::uint8_t green;
94 std::uint8_t blue;
95 std::uint8_t alpha;
96
97public:
101 constexpr RgbaColour() noexcept : red(0), green(0), blue(0), alpha(255) {}
102
106 constexpr
108 std::uint8_t r, std::uint8_t g, std::uint8_t b, std::uint8_t a) noexcept
109 : red(r), green(g), blue(b), alpha(a)
110 {}
111
113 explicit constexpr RgbaColour(const RgbColour& rgb) noexcept
114 : red(rgb.red), green(rgb.green), blue(rgb.blue), alpha(255)
115 {}
116
118 explicit operator RgbColour() const noexcept
119 { return RgbColour(red, green, blue); }
120
122 constexpr
123 bool operator==(const RgbaColour& other) const noexcept
124 {
125 return
126 red == other.red && green == other.green
127 && blue == other.blue && alpha == other.alpha;
128 }
129
131 constexpr
132 bool operator!=(const RgbaColour& other) const noexcept
133 { return !(*this == other); }
134};
135
136
137
138
140
141constexpr
142inline
143bool operator==(const RgbaColour& x, const RgbColour& y) noexcept
144 { return x.red == y.red && x.green == y.green && x.blue == y.blue; }
145
146constexpr
147inline
148bool operator!=(const RgbaColour& x, const RgbColour& y) noexcept
149 { return !(x == y); }
150
151constexpr
152inline
153bool operator==(const RgbColour& x, const RgbaColour& y) noexcept
154 { return y == x; }
155
156constexpr
157inline
158bool operator!=(const RgbColour& x, const RgbaColour& y) noexcept
159 { return !(x == y); }
160
161
162
163
165
170{
171public:
172 void operator()(const RgbaColour& src, RgbColour& dst) noexcept
173 {
174 dst.red = src.red;
175 dst.green = src.green;
176 dst.blue = src.blue;
177 }
178
179 void operator()(const RgbColour& src, RgbaColour& dst) noexcept
180 {
181 dst.red = src.red;
182 dst.green = src.green;
183 dst.blue = src.blue;
184 // dst.alphaは變更しない
185 }
186};
187
188
194{
195public:
196 void operator()(const RgbColour& src, RgbColour& dst) noexcept
197 {
198 dst.red = std::clamp(dst.red + src.red, 0, 255);
199 dst.green = std::clamp(dst.green + src.green, 0, 255);
200 dst.red = std::clamp(dst.blue + src.blue, 0, 255);
201 }
202
203 void operator()(const RgbaColour& src, RgbColour& dst) noexcept
204 {
205 dst.red = std::clamp(dst.red + src.red, 0, 255);
206 dst.green = std::clamp(dst.green + src.green, 0, 255);
207 dst.red = std::clamp(dst.blue + src.blue, 0, 255);
208 }
209
210 void operator()(const RgbColour& src, RgbaColour& dst) noexcept
211 {
212 dst.red = std::clamp(dst.red + src.red, 0, 255);
213 dst.green = std::clamp(dst.green + src.green, 0, 255);
214 dst.red = std::clamp(dst.blue + src.blue, 0, 255);
215 // dst.alphaは變更しない
216 }
217
218 void operator()(const RgbaColour& src, RgbaColour& dst) noexcept
219 {
220 dst.red = std::clamp(dst.red + src.red, 0, 255);
221 dst.green = std::clamp(dst.green + src.green, 0, 255);
222 dst.red = std::clamp(dst.blue + src.blue, 0, 255);
223 // dst.alphaは變更しない
224 }
225};
226
227
233{
234public:
235 void operator()(const RgbColour& src, RgbColour& dst) noexcept
236 {
237 dst.red = std::clamp(dst.red * src.red / 255, 0, 255);
238 dst.green = std::clamp(dst.green * src.green / 255, 0, 255);
239 dst.blue = std::clamp(dst.blue * src.blue / 255, 0, 255);
240 }
241
242 void operator()(const RgbaColour& src, RgbColour& dst) noexcept
243 {
244 dst.red = std::clamp(dst.red * src.red / 255, 0, 255);
245 dst.green = std::clamp(dst.green * src.green / 255, 0, 255);
246 dst.blue = std::clamp(dst.blue * src.blue / 255, 0, 255);
247 }
248
249 void operator()(const RgbColour& src, RgbaColour& dst) noexcept
250 {
251 dst.red = std::clamp(dst.red * src.red / 255, 0, 255);
252 dst.green = std::clamp(dst.green * src.green / 255, 0, 255);
253 dst.blue = std::clamp(dst.blue * src.blue / 255, 0, 255);
254 // dst.alphaは變更しない
255 }
256
257 void operator()(const RgbaColour& src, RgbaColour& dst) noexcept
258 {
259 dst.red = std::clamp(dst.red * src.red / 255, 0, 255);
260 dst.green = std::clamp(dst.green * src.green / 255, 0, 255);
261 dst.blue = std::clamp(dst.blue * src.blue / 255, 0, 255);
262 // dst.alphaは變更しない
263 }
264};
265
266
272{
273public:
274 void operator()(const RgbaColour& src, RgbColour& dst) noexcept
275 {
276 dst.red
277 = std::clamp(
278 src.red * src.alpha / 255 + dst.red * (255 - src.alpha) / 255,
279 0, 255);
280 dst.green
281 = std::clamp(
282 src.green * src.alpha / 255 + dst.green * (255 - src.alpha) / 255,
283 0, 255);
284 dst.blue
285 = std::clamp(
286 src.blue * src.alpha / 255 + dst.blue * (255 - src.alpha) / 255,
287 0, 255);
288 }
289
290 void operator()(const RgbaColour& src, RgbaColour& dst) noexcept
291 {
292 dst.red
293 = std::clamp(
294 src.red * src.alpha / 255 + dst.red * (255 - src.alpha) / 255,
295 0, 255);
296 dst.green
297 = std::clamp(
298 src.green * src.alpha / 255 + dst.green * (255 - src.alpha) / 255,
299 0, 255);
300 dst.blue
301 = std::clamp(
302 src.blue * src.alpha / 255 + dst.blue * (255 - src.alpha) / 255,
303 0, 255);
304 dst.alpha
305 = std::clamp(
306 src.alpha + dst.alpha * (255 - src.alpha) / 255,
307 0, 255);
308 }
309};
310
311
320{
321private:
322 std::uint8_t alpha_;
323
324public:
325 explicit CopierUsingFixedAlpha(std::uint8_t a) noexcept : alpha_(a) {}
326
327 void operator()(const RgbColour& src, RgbColour& dst) const noexcept
328 {
329 dst.red
330 = std::clamp(
331 src.red * alpha_ / 255 + dst.red * (255 - alpha_) / 255,
332 0, 255);
333 dst.green
334 = std::clamp(
335 src.green * alpha_ / 255 + dst.green * (255 - alpha_) / 255,
336 0, 255);
337 dst.blue
338 = std::clamp(
339 src.blue * alpha_ / 255 + dst.blue * (255 - alpha_) / 255,
340 0, 255);
341 }
342
343 void operator()(const RgbaColour& src, RgbColour& dst) const noexcept
344 {
345 // srcのアルファ値を、固定アルファ値alpha_で乘じた上で用ゐる。
346 int a = alpha_ * src.alpha;
347 dst.red
348 = std::clamp(
349 src.red * a / 65025 + dst.red * (65025 - a) / 65025,
350 0, 255);
351 dst.green
352 = std::clamp(
353 src.green * a / 65025 + dst.green * (65025 - a) / 65025,
354 0, 255);
355 dst.blue
356 = std::clamp(
357 src.blue * a / 65025 + dst.blue * (65025 - a) / 65025,
358 0, 255);
359 }
360
361 void operator()(const RgbColour& src, RgbaColour& dst) const noexcept
362 {
363 dst.red
364 = std::clamp(
365 src.red * alpha_ / 255 + dst.red * (255 - alpha_) / 255,
366 0, 255);
367 dst.green
368 = std::clamp(
369 src.green * alpha_ / 255 + dst.green * (255 - alpha_) / 255,
370 0, 255);
371 dst.blue
372 = std::clamp(
373 src.blue * alpha_ / 255 + dst.blue * (255 - alpha_) / 255,
374 0, 255);
375 dst.alpha
376 = std::clamp(
377 alpha_ + dst.alpha * (255 - alpha_) / 255,
378 0, 255);
379 }
380
381 void operator()(const RgbaColour& src, RgbaColour& dst) const noexcept
382 {
383 // srcのアルファ値を、固定アルファ値alpha_で乘じた上で用ゐる。
384 int a = alpha_ * src.alpha;
385 dst.red
386 = std::clamp(
387 src.red * a / 65025 + dst.red * (65025 - a) / 65025,
388 0, 255);
389 dst.green
390 = std::clamp(
391 src.green * a / 65025 + dst.green * (65025 - a) / 65025,
392 0, 255);
393 dst.blue
394 = std::clamp(
395 src.blue * a / 65025 + dst.blue * (65025 - a) / 65025,
396 0, 255);
397
398 dst.alpha
399 = std::clamp(
400 a / 255 + dst.alpha * (65025 - a) / 65025,
401 0, 255);
402 }
403};
404
405
406}//end of namespace eunomia
407
408
409
410
411#endif // INCLUDE_GUARD_EUNOMIA_COLOUR_H
ImageBuffer<>::blt()で用ゐる加算ブレンドのための函數オブジェクトクラス
Definition colour.h:194
void operator()(const RgbColour &src, RgbaColour &dst) noexcept
Definition colour.h:210
void operator()(const RgbColour &src, RgbColour &dst) noexcept
Definition colour.h:196
void operator()(const RgbaColour &src, RgbColour &dst) noexcept
Definition colour.h:203
void operator()(const RgbaColour &src, RgbaColour &dst) noexcept
Definition colour.h:218
ImageBuffer<>::blt()で用ゐるαブレンドのための函數オブジェクトクラス
Definition colour.h:272
void operator()(const RgbaColour &src, RgbaColour &dst) noexcept
Definition colour.h:290
void operator()(const RgbaColour &src, RgbColour &dst) noexcept
Definition colour.h:274
ImageBuffer<>::blt()で用ゐるαブレンドのための函數オブジェクトクラス
Definition colour.h:320
void operator()(const RgbaColour &src, RgbColour &dst) const noexcept
Definition colour.h:343
void operator()(const RgbColour &src, RgbColour &dst) const noexcept
Definition colour.h:327
void operator()(const RgbColour &src, RgbaColour &dst) const noexcept
Definition colour.h:361
CopierUsingFixedAlpha(std::uint8_t a) noexcept
Definition colour.h:325
void operator()(const RgbaColour &src, RgbaColour &dst) const noexcept
Definition colour.h:381
ImageBuffer<>::blt()で用ゐる乘算ブレンドのための函數オブジェクトクラス
Definition colour.h:233
void operator()(const RgbaColour &src, RgbColour &dst) noexcept
Definition colour.h:242
void operator()(const RgbColour &src, RgbColour &dst) noexcept
Definition colour.h:235
void operator()(const RgbColour &src, RgbaColour &dst) noexcept
Definition colour.h:249
void operator()(const RgbaColour &src, RgbaColour &dst) noexcept
Definition colour.h:257
ImageBuffer<>::blt()で用ゐる單純轉送のための函數オブジェクトクラス
Definition colour.h:170
void operator()(const RgbColour &src, RgbaColour &dst) noexcept
Definition colour.h:179
void operator()(const RgbaColour &src, RgbColour &dst) noexcept
Definition colour.h:172
RGB24bit色情報クラス
Definition colour.h:54
std::uint8_t green
緑要素
Definition colour.h:57
constexpr bool operator==(const RgbColour &other) const noexcept
等價比較演算子
Definition colour.h:76
constexpr RgbColour(std::uint8_t r, std::uint8_t g, std::uint8_t b) noexcept
構築子
Definition colour.h:70
constexpr RgbColour() noexcept
構築子
Definition colour.h:64
constexpr bool operator!=(const RgbColour &other) const noexcept
非等價比較演算子
Definition colour.h:81
std::uint8_t red
赤要素
Definition colour.h:56
std::uint8_t blue
青要素
Definition colour.h:58
RGBA32bit色情報クラス
Definition colour.h:90
constexpr bool operator==(const RgbaColour &other) const noexcept
等價比較演算子
Definition colour.h:123
constexpr bool operator!=(const RgbaColour &other) const noexcept
非等價比較演算子
Definition colour.h:132
std::uint8_t alpha
アルファ値(不透過度)
Definition colour.h:95
std::uint8_t red
赤要素
Definition colour.h:92
std::uint8_t green
緑要素
Definition colour.h:93
std::uint8_t blue
青要素
Definition colour.h:94
constexpr RgbaColour(const RgbColour &rgb) noexcept
構築子
Definition colour.h:113
constexpr RgbaColour(std::uint8_t r, std::uint8_t g, std::uint8_t b, std::uint8_t a) noexcept
構築子
Definition colour.h:107
constexpr RgbaColour() noexcept
構築子
Definition colour.h:101
Definition colour.h:49
constexpr bool operator==(const RgbaColour &x, const RgbColour &y) noexcept
Definition colour.h:143
constexpr bool operator!=(const RgbaColour &x, const RgbColour &y) noexcept
Definition colour.h:148