Blender  V3.3
BLI_color.hh
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 #pragma once
4 
5 #include <iostream>
6 
7 #include "BLI_math_color.h"
8 
9 namespace blender {
10 
63 enum class eAlpha {
65  Straight,
68 };
69 std::ostream &operator<<(std::ostream &stream, const eAlpha &space);
70 
72 enum class eSpace {
74  Theme,
79 };
80 std::ostream &operator<<(std::ostream &stream, const eSpace &space);
81 
83 template<typename ChannelStorageType, eSpace Space, eAlpha Alpha> class ColorRGBA {
84  public:
85  ChannelStorageType r, g, b, a;
86  constexpr ColorRGBA() = default;
87 
88  constexpr ColorRGBA(const ChannelStorageType rgba[4])
89  : r(rgba[0]), g(rgba[1]), b(rgba[2]), a(rgba[3])
90  {
91  }
92 
93  constexpr ColorRGBA(const ChannelStorageType r,
94  const ChannelStorageType g,
95  const ChannelStorageType b,
96  const ChannelStorageType a)
97  : r(r), g(g), b(b), a(a)
98  {
99  }
100 
101  operator ChannelStorageType *()
102  {
103  return &r;
104  }
105 
106  operator const ChannelStorageType *() const
107  {
108  return &r;
109  }
110 
111  friend std::ostream &operator<<(std::ostream &stream,
113  {
114 
115  stream << Space << Alpha << "(" << c.r << ", " << c.g << ", " << c.b << ", " << c.a << ")";
116  return stream;
117  }
118 
121  {
122  return a.r == b.r && a.g == b.g && a.b == b.b && a.a == b.a;
123  }
124 
127  {
128  return !(a == b);
129  }
130 
131  uint64_t hash() const
132  {
133  uint64_t x1 = *reinterpret_cast<const uint32_t *>(&r);
134  uint64_t x2 = *reinterpret_cast<const uint32_t *>(&g);
135  uint64_t x3 = *reinterpret_cast<const uint32_t *>(&b);
136  uint64_t x4 = *reinterpret_cast<const uint32_t *>(&a);
137  return (x1 * 1283591) ^ (x2 * 850177) ^ (x3 * 735391) ^ (x4 * 442319);
138  }
139 };
140 
141 /* Forward declarations of concrete color classes. */
142 
143 template<eAlpha Alpha> class ColorSceneLinear4f;
144 template<eAlpha Alpha> class ColorSceneLinearByteEncoded4b;
145 template<typename ChannelStorageType> class ColorTheme4;
146 
147 /* Forward declaration of precision conversion methods. */
148 
151 
152 template<eAlpha Alpha>
153 class ColorSceneLinear4f final : public ColorRGBA<float, eSpace::SceneLinear, Alpha> {
154  public:
156  {
157  }
158 
159  constexpr ColorSceneLinear4f<Alpha>(const float *rgba)
161  {
162  }
163 
164  constexpr ColorSceneLinear4f<Alpha>(float r, float g, float b, float a)
166  {
167  }
168 
173  {
175  linearrgb_to_srgb_uchar4(encoded, *this);
176  return encoded;
177  }
178 
185  {
186  if constexpr (Alpha == eAlpha::Straight) {
188  straight_to_premul_v4_v4(premultiplied, *this);
189  return premultiplied;
190  }
191  else {
192  return *this;
193  }
194  }
195 
202  {
203  if constexpr (Alpha == eAlpha::Premultiplied) {
205  premul_to_straight_v4_v4(straighten, *this);
206  return straighten;
207  }
208  else {
209  return *this;
210  }
211  }
212 };
213 
214 template<eAlpha Alpha>
216  : public ColorRGBA<uint8_t, eSpace::SceneLinearByteEncoded, Alpha> {
217  public:
218  constexpr ColorSceneLinearByteEncoded4b() = default;
219 
222  {
223  }
224 
227  {
228  }
229 
234  {
236  srgb_to_linearrgb_uchar4(decoded, *this);
237  return decoded;
238  }
239 };
240 
250 template<typename ChannelStorageType>
251 class ColorTheme4 final : public ColorRGBA<ChannelStorageType, eSpace::Theme, eAlpha::Straight> {
252  public:
253  constexpr ColorTheme4() : ColorRGBA<ChannelStorageType, eSpace::Theme, eAlpha::Straight>(){};
254 
255  constexpr ColorTheme4(const ChannelStorageType *rgba)
256  : ColorRGBA<ChannelStorageType, eSpace::Theme, eAlpha::Straight>(rgba)
257  {
258  }
259 
260  constexpr ColorTheme4(ChannelStorageType r,
261  ChannelStorageType g,
262  ChannelStorageType b,
263  ChannelStorageType a)
264  : ColorRGBA<ChannelStorageType, eSpace::Theme, eAlpha::Straight>(r, g, b, a)
265  {
266  }
267 
272  {
273  if constexpr ((std::is_same_v<ChannelStorageType, uint8_t>)) {
274  return BLI_color_convert_to_theme4f(*this);
275  }
276  else {
277  return *this;
278  }
279  }
280 
285  {
286  if constexpr ((std::is_same_v<ChannelStorageType, float>)) {
287  return BLI_color_convert_to_theme4b(*this);
288  }
289  else {
290  return *this;
291  }
292  }
293 };
294 
297 
299 {
300  ColorTheme4b theme4b;
301  rgba_float_to_uchar(theme4b, theme4f);
302  return theme4b;
303 }
304 
306 {
307  ColorTheme4f theme4f;
308  rgba_uchar_to_float(theme4f, theme4b);
309  return theme4f;
310 }
311 
313  const ColorTheme4f &theme4f)
314 {
316  srgb_to_linearrgb_v4(scene_linear, theme4f);
317  return scene_linear;
318 }
319 
321  const ColorTheme4b &theme4b)
322 {
324  srgb_to_linearrgb_uchar4(scene_linear, theme4b);
325  return scene_linear;
326 }
327 
330 {
331  ColorTheme4f theme4f;
332  linearrgb_to_srgb_v4(theme4f, scene_linear);
333  return theme4f;
334 }
335 
338 {
339  ColorTheme4b theme4b;
340  linearrgb_to_srgb_uchar4(theme4b, scene_linear);
341  return theme4b;
342 }
343 
344 /* Internal roles. For convenience to shorten the type names and hide complexity. */
345 
350 
351 } // namespace blender
#define BLI_INLINE
#define final(a, b, c)
Definition: BLI_hash.h:21
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])
Definition: math_color.c:383
void rgba_float_to_uchar(unsigned char r_col[4], const float col_f[4])
Definition: math_color.c:396
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])
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble w _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat w _GL_VOID_RET _GL_VOID GLint GLint GLint w _GL_VOID_RET _GL_VOID GLshort GLshort GLshort w _GL_VOID_RET _GL_VOID GLdouble GLdouble x2
friend bool operator==(const ColorRGBA< ChannelStorageType, Space, Alpha > &a, const ColorRGBA< ChannelStorageType, Space, Alpha > &b)
Definition: BLI_color.hh:119
ChannelStorageType r
Definition: BLI_color.hh:85
ChannelStorageType g
Definition: BLI_color.hh:85
uint64_t hash() const
Definition: BLI_color.hh:131
constexpr ColorRGBA(const ChannelStorageType r, const ChannelStorageType g, const ChannelStorageType b, const ChannelStorageType a)
Definition: BLI_color.hh:93
ChannelStorageType b
Definition: BLI_color.hh:85
constexpr ColorRGBA(const ChannelStorageType rgba[4])
Definition: BLI_color.hh:88
friend bool operator!=(const ColorRGBA< ChannelStorageType, Space, Alpha > &a, const ColorRGBA< ChannelStorageType, Space, Alpha > &b)
Definition: BLI_color.hh:125
friend std::ostream & operator<<(std::ostream &stream, const ColorRGBA< ChannelStorageType, Space, Alpha > &c)
Definition: BLI_color.hh:111
ChannelStorageType a
Definition: BLI_color.hh:85
constexpr ColorRGBA()=default
ColorSceneLinear4f< eAlpha::Premultiplied > premultiply_alpha() const
Definition: BLI_color.hh:184
ColorSceneLinearByteEncoded4b< Alpha > encode() const
Definition: BLI_color.hh:172
ColorSceneLinear4f< eAlpha::Straight > unpremultiply_alpha() const
Definition: BLI_color.hh:201
constexpr ColorSceneLinearByteEncoded4b(const uint8_t *rgba)
Definition: BLI_color.hh:220
constexpr ColorSceneLinearByteEncoded4b(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
Definition: BLI_color.hh:225
constexpr ColorSceneLinearByteEncoded4b()=default
ColorSceneLinear4f< Alpha > decode() const
Definition: BLI_color.hh:233
ColorTheme4< uint8_t > to_4b() const
Definition: BLI_color.hh:284
constexpr ColorTheme4(const ChannelStorageType *rgba)
Definition: BLI_color.hh:255
constexpr ColorTheme4()
Definition: BLI_color.hh:253
constexpr ColorTheme4(ChannelStorageType r, ChannelStorageType g, ChannelStorageType b, ChannelStorageType a)
Definition: BLI_color.hh:260
ColorTheme4< float > to_4f() const
Definition: BLI_color.hh:271
static unsigned c
Definition: RandGen.cpp:83
BLI_INLINE ColorSceneLinear4f< eAlpha::Straight > BLI_color_convert_to_scene_linear(const ColorTheme4f &theme4f)
Definition: BLI_color.hh:312
std::ostream & operator<<(std::ostream &stream, const eAlpha &space)
Definition: BLI_color.cc:7
ColorTheme4< float > ColorTheme4f
Definition: BLI_color.hh:296
BLI_INLINE ColorTheme4< float > BLI_color_convert_to_theme4f(const ColorTheme4< uint8_t > &srgb4b)
ColorTheme4< uint8_t > ColorTheme4b
Definition: BLI_color.hh:295
BLI_INLINE ColorTheme4< uint8_t > BLI_color_convert_to_theme4b(const ColorTheme4< float > &srgb4f)
Definition: BLI_color.hh:298
static const pxr::TfToken rgba("rgba", pxr::TfToken::Immortal)
unsigned int uint32_t
Definition: stdint.h:80
unsigned char uint8_t
Definition: stdint.h:78
unsigned __int64 uint64_t
Definition: stdint.h:90