Blender V4.3
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
5#pragma once
6
7#include <ostream>
8
9#include "BLI_math_color.h"
11
12namespace blender {
13
66enum class eAlpha {
71};
72std::ostream &operator<<(std::ostream &stream, const eAlpha &space);
73
75enum class eSpace {
77 Theme,
82};
83std::ostream &operator<<(std::ostream &stream, const eSpace &space);
84
86template<typename ChannelStorageType, eSpace Space, eAlpha Alpha> class ColorRGBA {
87 public:
88 ChannelStorageType r, g, b, a;
89 constexpr ColorRGBA() = default;
90
91 constexpr ColorRGBA(const ChannelStorageType rgba[4])
92 : r(rgba[0]), g(rgba[1]), b(rgba[2]), a(rgba[3])
93 {
94 }
95
96 constexpr ColorRGBA(const ChannelStorageType r,
97 const ChannelStorageType g,
98 const ChannelStorageType b,
99 const ChannelStorageType a)
100 : r(r), g(g), b(b), a(a)
101 {
102 }
103
104 operator ChannelStorageType *()
105 {
106 return &r;
107 }
108
109 operator const ChannelStorageType *() const
110 {
111 return &r;
112 }
113
114 friend std::ostream &operator<<(std::ostream &stream,
116 {
117
118 stream << Space << Alpha << "(" << c.r << ", " << c.g << ", " << c.b << ", " << c.a << ")";
119 return stream;
120 }
121
123
125 {
126 uint64_t x1 = *reinterpret_cast<const uint32_t *>(&r);
127 uint64_t x2 = *reinterpret_cast<const uint32_t *>(&g);
128 uint64_t x3 = *reinterpret_cast<const uint32_t *>(&b);
129 uint64_t x4 = *reinterpret_cast<const uint32_t *>(&a);
130 return (x1 * 1283591) ^ (x2 * 850177) ^ (x3 * 735391) ^ (x4 * 442319);
131 }
132};
133
134/* Forward declarations of concrete color classes. */
135
136template<eAlpha Alpha> class ColorSceneLinear4f;
137template<eAlpha Alpha> class ColorSceneLinearByteEncoded4b;
138template<typename ChannelStorageType> class ColorTheme4;
139
140/* Forward declaration of precision conversion methods. */
141
144
145template<eAlpha Alpha>
146class ColorSceneLinear4f final : public ColorRGBA<float, eSpace::SceneLinear, Alpha> {
147 public:
148 constexpr ColorSceneLinear4f() = default;
149
150 constexpr ColorSceneLinear4f(const float *rgba)
151 : ColorRGBA<float, eSpace::SceneLinear, Alpha>(rgba)
152 {
153 }
154
155 constexpr ColorSceneLinear4f(float r, float g, float b, float a)
156 : ColorRGBA<float, eSpace::SceneLinear, Alpha>(r, g, b, a)
157 {
158 }
159
164 {
166 linearrgb_to_srgb_uchar4(encoded, *this);
167 return encoded;
168 }
169
176 {
177 if constexpr (Alpha == eAlpha::Straight) {
179 straight_to_premul_v4_v4(premultiplied, *this);
180 return premultiplied;
181 }
182 else {
183 return *this;
184 }
185 }
186
193 {
194 if constexpr (Alpha == eAlpha::Premultiplied) {
196 premul_to_straight_v4_v4(straighten, *this);
197 return straighten;
198 }
199 else {
200 return *this;
201 }
202 }
203};
204
205template<eAlpha Alpha>
207 : public ColorRGBA<uint8_t, eSpace::SceneLinearByteEncoded, Alpha> {
208 public:
209 constexpr ColorSceneLinearByteEncoded4b() = default;
210
213 {
214 }
215
220
225 {
227 srgb_to_linearrgb_uchar4(decoded, *this);
228 return decoded;
229 }
230};
231
241template<typename ChannelStorageType>
242class ColorTheme4 final : public ColorRGBA<ChannelStorageType, eSpace::Theme, eAlpha::Straight> {
243 public:
244 constexpr ColorTheme4() = default;
245
246 constexpr ColorTheme4(const ChannelStorageType *rgba)
247 : ColorRGBA<ChannelStorageType, eSpace::Theme, eAlpha::Straight>(rgba)
248 {
249 }
250
251 constexpr ColorTheme4(ChannelStorageType r,
252 ChannelStorageType g,
253 ChannelStorageType b,
254 ChannelStorageType a)
255 : ColorRGBA<ChannelStorageType, eSpace::Theme, eAlpha::Straight>(r, g, b, a)
256 {
257 }
258
263 {
264 if constexpr ((std::is_same_v<ChannelStorageType, uint8_t>)) {
265 return BLI_color_convert_to_theme4f(*this);
266 }
267 else {
268 return *this;
269 }
270 }
271
276 {
277 if constexpr ((std::is_same_v<ChannelStorageType, float>)) {
278 return BLI_color_convert_to_theme4b(*this);
279 }
280 else {
281 return *this;
282 }
283 }
284};
285
288
290{
291 ColorTheme4b theme4b;
292 rgba_float_to_uchar(theme4b, theme4f);
293 return theme4b;
294}
295
297{
298 ColorTheme4f theme4f;
299 rgba_uchar_to_float(theme4f, theme4b);
300 return theme4f;
301}
302
304 const ColorTheme4f &theme4f)
305{
307 srgb_to_linearrgb_v4(scene_linear, theme4f);
308 return scene_linear;
309}
310
312 const ColorTheme4b &theme4b)
313{
315 srgb_to_linearrgb_uchar4(scene_linear, theme4b);
316 return scene_linear;
317}
318
321{
322 ColorTheme4f theme4f;
323 linearrgb_to_srgb_v4(theme4f, scene_linear);
324 return theme4f;
325}
326
329{
330 ColorTheme4b theme4b;
331 linearrgb_to_srgb_uchar4(theme4b, scene_linear);
332 return theme4b;
333}
334
335/* Internal roles. For convenience to shorten the type names and hide complexity. */
336
341
342} // namespace blender
#define BLI_INLINE
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)
ChannelStorageType r
Definition BLI_color.hh:88
ChannelStorageType g
Definition BLI_color.hh:88
uint64_t hash() const
Definition BLI_color.hh:124
constexpr ColorRGBA(const ChannelStorageType r, const ChannelStorageType g, const ChannelStorageType b, const ChannelStorageType a)
Definition BLI_color.hh:96
ChannelStorageType b
Definition BLI_color.hh:88
constexpr ColorRGBA(const ChannelStorageType rgba[4])
Definition BLI_color.hh:91
friend std::ostream & operator<<(std::ostream &stream, const ColorRGBA< ChannelStorageType, Space, Alpha > &c)
Definition BLI_color.hh:114
ChannelStorageType a
Definition BLI_color.hh:88
constexpr ColorRGBA()=default
constexpr ColorSceneLinear4f(const float *rgba)
Definition BLI_color.hh:150
constexpr ColorSceneLinear4f()=default
ColorSceneLinearByteEncoded4b< Alpha > encode() const
Definition BLI_color.hh:163
ColorSceneLinear4f< eAlpha::Straight > unpremultiply_alpha() const
Definition BLI_color.hh:192
constexpr ColorSceneLinear4f(float r, float g, float b, float a)
Definition BLI_color.hh:155
ColorSceneLinear4f< eAlpha::Premultiplied > premultiply_alpha() const
Definition BLI_color.hh:175
ColorSceneLinear4f< Alpha > decode() const
Definition BLI_color.hh:224
constexpr ColorSceneLinearByteEncoded4b(const uint8_t *rgba)
Definition BLI_color.hh:211
constexpr ColorSceneLinearByteEncoded4b(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
Definition BLI_color.hh:216
constexpr ColorSceneLinearByteEncoded4b()=default
constexpr ColorTheme4(const ChannelStorageType *rgba)
Definition BLI_color.hh:246
constexpr ColorTheme4()=default
constexpr ColorTheme4(ChannelStorageType r, ChannelStorageType g, ChannelStorageType b, ChannelStorageType a)
Definition BLI_color.hh:251
ColorTheme4< float > to_4f() const
Definition BLI_color.hh:262
ColorTheme4< uint8_t > to_4b() const
Definition BLI_color.hh:275
draw_view in_light_buf[] float
ColorTheme4< uint8_t > ColorTheme4b
Definition BLI_color.hh:286
std::ostream & operator<<(std::ostream &stream, const eAlpha &space)
Definition BLI_color.cc:11
BLI_INLINE ColorTheme4< uint8_t > BLI_color_convert_to_theme4b(const ColorTheme4< float > &theme4f)
Definition BLI_color.hh:289
BLI_INLINE ColorTheme4< float > BLI_color_convert_to_theme4f(const ColorTheme4< uint8_t > &theme4b)
BLI_INLINE ColorSceneLinear4f< eAlpha::Straight > BLI_color_convert_to_scene_linear(const ColorTheme4f &theme4f)
Definition BLI_color.hh:303
ColorTheme4< float > ColorTheme4f
Definition BLI_color.hh:287
unsigned int uint32_t
Definition stdint.h:80
unsigned char uint8_t
Definition stdint.h:78
unsigned __int64 uint64_t
Definition stdint.h:90