Blender V4.5
BLI_color.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#pragma once
10
11#include <ostream>
12
13#include "BLI_compiler_compat.h"
14#include "BLI_math_color.h"
16
17namespace blender {
18
69
77std::ostream &operator<<(std::ostream &stream, const eAlpha &space);
78
88std::ostream &operator<<(std::ostream &stream, const eSpace &space);
89
91template<typename ChannelStorageType, eSpace Space, eAlpha Alpha> class ColorRGBA {
92 public:
93 ChannelStorageType r, g, b, a;
94 constexpr ColorRGBA() = default;
95
96 constexpr ColorRGBA(const ChannelStorageType rgba[4])
97 : r(rgba[0]), g(rgba[1]), b(rgba[2]), a(rgba[3])
98 {
99 }
100
101 constexpr ColorRGBA(const ChannelStorageType r,
102 const ChannelStorageType g,
103 const ChannelStorageType b,
104 const ChannelStorageType a)
105 : r(r), g(g), b(b), a(a)
106 {
107 }
108
109 operator ChannelStorageType *()
110 {
111 return &r;
112 }
113
114 operator const ChannelStorageType *() const
115 {
116 return &r;
117 }
118
119 friend std::ostream &operator<<(std::ostream &stream,
121 {
122
123 stream << Space << Alpha << "(" << c.r << ", " << c.g << ", " << c.b << ", " << c.a << ")";
124 return stream;
125 }
126
128
130 {
131 uint64_t x1 = *reinterpret_cast<const uint32_t *>(&r);
132 uint64_t x2 = *reinterpret_cast<const uint32_t *>(&g);
133 uint64_t x3 = *reinterpret_cast<const uint32_t *>(&b);
134 uint64_t x4 = *reinterpret_cast<const uint32_t *>(&a);
135 return (x1 * 1283591) ^ (x2 * 850177) ^ (x3 * 735391) ^ (x4 * 442319);
136 }
137};
138
139/* Forward declarations of concrete color classes. */
140
141template<eAlpha Alpha> class ColorSceneLinear4f;
142template<eAlpha Alpha> class ColorSceneLinearByteEncoded4b;
143template<typename ChannelStorageType> class ColorTheme4;
144
145/* Forward declaration of precision conversion methods. */
146
147BLI_INLINE ColorTheme4<float> BLI_color_convert_to_theme4f(const ColorTheme4<uint8_t> &theme4b);
148BLI_INLINE ColorTheme4<uint8_t> BLI_color_convert_to_theme4b(const ColorTheme4<float> &theme4f);
149
150template<eAlpha Alpha>
151class ColorSceneLinear4f final : public ColorRGBA<float, eSpace::SceneLinear, Alpha> {
152 public:
153 constexpr ColorSceneLinear4f() = default;
154
155 constexpr ColorSceneLinear4f(const float *rgba)
156 : ColorRGBA<float, eSpace::SceneLinear, Alpha>(rgba)
157 {
158 }
159
160 constexpr ColorSceneLinear4f(float r, float g, float b, float a)
161 : ColorRGBA<float, eSpace::SceneLinear, Alpha>(r, g, b, a)
162 {
163 }
164
169 {
171 linearrgb_to_srgb_uchar4(encoded, *this);
172 return encoded;
173 }
174
181 {
182 if constexpr (Alpha == eAlpha::Straight) {
184 straight_to_premul_v4_v4(premultiplied, *this);
185 return premultiplied;
186 }
187 else {
188 return *this;
189 }
190 }
191
198 {
199 if constexpr (Alpha == eAlpha::Premultiplied) {
201 premul_to_straight_v4_v4(straighten, *this);
202 return straighten;
203 }
204 else {
205 return *this;
206 }
207 }
208};
209
210template<eAlpha Alpha>
212 : public ColorRGBA<uint8_t, eSpace::SceneLinearByteEncoded, Alpha> {
213 public:
214 constexpr ColorSceneLinearByteEncoded4b() = default;
215
216 constexpr ColorSceneLinearByteEncoded4b(const uint8_t *rgba)
217 : ColorRGBA<uint8_t, eSpace::SceneLinearByteEncoded, Alpha>(rgba)
218 {
219 }
220
221 constexpr ColorSceneLinearByteEncoded4b(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
222 : ColorRGBA<uint8_t, eSpace::SceneLinearByteEncoded, Alpha>(r, g, b, a)
223 {
224 }
225
230 {
232 srgb_to_linearrgb_uchar4(decoded, *this);
233 return decoded;
234 }
235};
236
246template<typename ChannelStorageType>
247class ColorTheme4 final : public ColorRGBA<ChannelStorageType, eSpace::Theme, eAlpha::Straight> {
248 public:
249 constexpr ColorTheme4() = default;
250
251 constexpr ColorTheme4(const ChannelStorageType *rgba)
252 : ColorRGBA<ChannelStorageType, eSpace::Theme, eAlpha::Straight>(rgba)
253 {
254 }
255
256 constexpr ColorTheme4(ChannelStorageType r,
257 ChannelStorageType g,
258 ChannelStorageType b,
259 ChannelStorageType a)
260 : ColorRGBA<ChannelStorageType, eSpace::Theme, eAlpha::Straight>(r, g, b, a)
261 {
262 }
263
268 {
269 if constexpr ((std::is_same_v<ChannelStorageType, uint8_t>)) {
270 return BLI_color_convert_to_theme4f(*this);
271 }
272 else {
273 return *this;
274 }
275 }
276
281 {
282 if constexpr ((std::is_same_v<ChannelStorageType, float>)) {
283 return BLI_color_convert_to_theme4b(*this);
284 }
285 else {
286 return *this;
287 }
288 }
289};
290
293
295{
296 ColorTheme4b theme4b;
297 rgba_float_to_uchar(theme4b, theme4f);
298 return theme4b;
299}
300
302{
303 ColorTheme4f theme4f;
304 rgba_uchar_to_float(theme4f, theme4b);
305 return theme4f;
306}
307
309 const ColorTheme4f &theme4f)
310{
312 srgb_to_linearrgb_v4(scene_linear, theme4f);
313 return scene_linear;
314}
315
317 const ColorTheme4b &theme4b)
318{
320 srgb_to_linearrgb_uchar4(scene_linear, theme4b);
321 return scene_linear;
322}
323
326{
327 ColorTheme4f theme4f;
328 linearrgb_to_srgb_v4(theme4f, scene_linear);
329 return theme4f;
330}
331
334{
335 ColorTheme4b theme4b;
336 linearrgb_to_srgb_uchar4(theme4b, scene_linear);
337 return theme4b;
338}
339
340/* Internal roles. For convenience to shorten the type names and hide complexity. */
341
346
347} // namespace blender
#define BLI_INLINE
#define final(a, b, c)
Definition BLI_hash.h:19
MINLINE void straight_to_premul_v4_v4(float premul[4], const float straight[4])
MINLINE void srgb_to_linearrgb_uchar4(float linear[4], const unsigned char srgb[4])
MINLINE void srgb_to_linearrgb_v4(float linear[4], const float srgb[4])
void rgba_uchar_to_float(float r_col[4], const unsigned char col_ub[4])
void rgba_float_to_uchar(unsigned char r_col[4], const float col_f[4])
MINLINE void premul_to_straight_v4_v4(float straight[4], const float premul[4])
MINLINE void linearrgb_to_srgb_uchar4(unsigned char srgb[4], const float linear[4])
MINLINE void linearrgb_to_srgb_v4(float srgb[4], const float linear[4])
#define BLI_STRUCT_EQUALITY_OPERATORS_4(Type, m1, m2, m3, m4)
unsigned long long int uint64_t
ChannelStorageType r
Definition BLI_color.hh:93
ChannelStorageType g
Definition BLI_color.hh:93
uint64_t hash() const
Definition BLI_color.hh:129
constexpr ColorRGBA(const ChannelStorageType r, const ChannelStorageType g, const ChannelStorageType b, const ChannelStorageType a)
Definition BLI_color.hh:101
ChannelStorageType b
Definition BLI_color.hh:93
constexpr ColorRGBA(const ChannelStorageType rgba[4])
Definition BLI_color.hh:96
friend std::ostream & operator<<(std::ostream &stream, const ColorRGBA< ChannelStorageType, Space, Alpha > &c)
Definition BLI_color.hh:119
ChannelStorageType a
Definition BLI_color.hh:93
constexpr ColorRGBA()=default
constexpr ColorSceneLinear4f(const float *rgba)
Definition BLI_color.hh:155
constexpr ColorSceneLinear4f()=default
ColorSceneLinearByteEncoded4b< Alpha > encode() const
Definition BLI_color.hh:168
ColorSceneLinear4f< eAlpha::Straight > unpremultiply_alpha() const
Definition BLI_color.hh:197
constexpr ColorSceneLinear4f(float r, float g, float b, float a)
Definition BLI_color.hh:160
ColorSceneLinear4f< eAlpha::Premultiplied > premultiply_alpha() const
Definition BLI_color.hh:180
ColorSceneLinear4f< Alpha > decode() const
Definition BLI_color.hh:229
constexpr ColorSceneLinearByteEncoded4b(const uint8_t *rgba)
Definition BLI_color.hh:216
constexpr ColorSceneLinearByteEncoded4b(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
Definition BLI_color.hh:221
constexpr ColorSceneLinearByteEncoded4b()=default
constexpr ColorTheme4(const ChannelStorageType *rgba)
Definition BLI_color.hh:251
constexpr ColorTheme4()=default
constexpr ColorTheme4(ChannelStorageType r, ChannelStorageType g, ChannelStorageType b, ChannelStorageType a)
Definition BLI_color.hh:256
ColorTheme4< float > to_4f() const
Definition BLI_color.hh:267
ColorTheme4< uint8_t > to_4b() const
Definition BLI_color.hh:280
ColorTheme4< uint8_t > ColorTheme4b
Definition BLI_color.hh:291
std::ostream & operator<<(std::ostream &stream, const eAlpha &space)
Definition BLI_color.cc:15
ColorSceneLinearByteEncoded4b< eAlpha::Straight > ColorPaint4b
Definition BLI_color.hh:345
BLI_INLINE ColorTheme4< uint8_t > BLI_color_convert_to_theme4b(const ColorTheme4< float > &theme4f)
Definition BLI_color.hh:294
ColorSceneLinear4f< eAlpha::Straight > ColorPaint4f
Definition BLI_color.hh:344
BLI_INLINE ColorTheme4< float > BLI_color_convert_to_theme4f(const ColorTheme4< uint8_t > &theme4b)
Definition BLI_color.hh:301
BLI_INLINE ColorSceneLinear4f< eAlpha::Straight > BLI_color_convert_to_scene_linear(const ColorTheme4f &theme4f)
Definition BLI_color.hh:308
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
Definition BLI_color.hh:342
ColorTheme4< float > ColorTheme4f
Definition BLI_color.hh:292
ColorSceneLinearByteEncoded4b< eAlpha::Premultiplied > ColorGeometry4b
Definition BLI_color.hh:343