Blender V5.3
color.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#pragma once
6
7#include "util/math.h"
8#include "util/types.h"
9
11
13{
14 uchar r;
15 uchar g;
16 uchar b;
17
18 r = float_to_byte(c.x);
19 g = float_to_byte(c.y);
20 b = float_to_byte(c.z);
21
22 return make_uchar4(r, g, b, 0);
23}
24
26{
27 uchar r;
28 uchar g;
29 uchar b;
30 uchar a;
31
32 r = float_to_byte(c.x);
33 g = float_to_byte(c.y);
34 b = float_to_byte(c.z);
35 a = float_to_byte(c.w);
36
37 return make_uchar4(r, g, b, a);
38}
39
41{
42 return make_float3(c.x * (1.0f / 255.0f), c.y * (1.0f / 255.0f), c.z * (1.0f / 255.0f));
43}
44
46{
47 return make_float4(
48 c.x * (1.0f / 255.0f), c.y * (1.0f / 255.0f), c.z * (1.0f / 255.0f), c.w * (1.0f / 255.0f));
49}
50
51ccl_device float color_srgb_to_linear(const float c)
52{
53 if (c < 0.04045f) {
54 return (c < 0.0f) ? 0.0f : c * (1.0f / 12.92f);
55 }
56 return powf((c + 0.055f) * (1.0f / 1.055f), 2.4f);
57}
58
59ccl_device float color_linear_to_srgb(const float c)
60{
61 if (c < 0.0031308f) {
62 return (c < 0.0f) ? 0.0f : c * 12.92f;
63 }
64 return 1.055f * powf(c, 1.0f / 2.4f) - 0.055f;
65}
66
68{
69 float cmax;
70 float cmin;
71 float h;
72 float s;
73 float v;
74 float cdelta;
75 float3 c;
76
77 cmax = fmaxf(rgb.x, fmaxf(rgb.y, rgb.z));
78 cmin = min(rgb.x, min(rgb.y, rgb.z));
79 cdelta = cmax - cmin;
80
81 v = cmax;
82
83 if (cmax != 0.0f) {
84 s = cdelta / cmax;
85 }
86 else {
87 s = 0.0f;
88 h = 0.0f;
89 }
90
91 if (s != 0.0f) {
92 const float3 cmax3 = make_float3(cmax, cmax, cmax);
93 c = (cmax3 - rgb) / cdelta;
94
95 if (rgb.x == cmax) {
96 h = c.z - c.y;
97 }
98 else if (rgb.y == cmax) {
99 h = 2.0f + c.x - c.z;
100 }
101 else {
102 h = 4.0f + c.y - c.x;
103 }
104
105 h /= 6.0f;
106
107 if (h < 0.0f) {
108 h += 1.0f;
109 }
110 }
111 else {
112 h = 0.0f;
113 }
114
115 return make_float3(h, s, v);
116}
117
119{
120 float i;
121 float f;
122 float p;
123 float q;
124 float t;
125 float h;
126 float s;
127 float v;
128 float3 rgb;
129
130 h = hsv.x;
131 s = hsv.y;
132 v = hsv.z;
133
134 if (s != 0.0f) {
135 if (h == 1.0f) {
136 h = 0.0f;
137 }
138
139 h *= 6.0f;
140 i = floorf(h);
141 f = h - i;
142 rgb = make_float3(f, f, f);
143 p = v * (1.0f - s);
144 q = v * (1.0f - (s * f));
145 t = v * (1.0f - (s * (1.0f - f)));
146
147 if (i == 0.0f) {
148 rgb = make_float3(v, t, p);
149 }
150 else if (i == 1.0f) {
151 rgb = make_float3(q, v, p);
152 }
153 else if (i == 2.0f) {
154 rgb = make_float3(p, v, t);
155 }
156 else if (i == 3.0f) {
157 rgb = make_float3(p, q, v);
158 }
159 else if (i == 4.0f) {
160 rgb = make_float3(t, p, v);
161 }
162 else {
163 rgb = make_float3(v, p, q);
164 }
165 }
166 else {
167 rgb = make_float3(v, v, v);
168 }
169
170 return rgb;
171}
172
174{
175 float cmax;
176 float cmin;
177 float h;
178 float s;
179 float l;
180
181 cmax = fmaxf(rgb.x, fmaxf(rgb.y, rgb.z));
182 cmin = min(rgb.x, min(rgb.y, rgb.z));
183 l = min(1.0f, (cmax + cmin) / 2.0f);
184
185 if (cmax == cmin) {
186 h = s = 0.0f; /* achromatic */
187 }
188 else {
189 const float cdelta = cmax - cmin;
190 s = l > 0.5f ? cdelta / (2.0f - cmax - cmin) : cdelta / (cmax + cmin);
191 if (cmax == rgb.x) {
192 h = (rgb.y - rgb.z) / cdelta + (rgb.y < rgb.z ? 6.0f : 0.0f);
193 }
194 else if (cmax == rgb.y) {
195 h = (rgb.z - rgb.x) / cdelta + 2.0f;
196 }
197 else {
198 h = (rgb.x - rgb.y) / cdelta + 4.0f;
199 }
200 }
201 h /= 6.0f;
202
203 return make_float3(h, s, l);
204}
205
207{
208 float nr;
209 float ng;
210 float nb;
211 float chroma;
212 float h;
213 float s;
214 float l;
215
216 h = hsl.x;
217 s = hsl.y;
218 l = hsl.z;
219
220 nr = fabsf(h * 6.0f - 3.0f) - 1.0f;
221 ng = 2.0f - fabsf(h * 6.0f - 2.0f);
222 nb = 2.0f - fabsf(h * 6.0f - 4.0f);
223
224 nr = clamp(nr, 0.0f, 1.0f);
225 nb = clamp(nb, 0.0f, 1.0f);
226 ng = clamp(ng, 0.0f, 1.0f);
227
228 chroma = (1.0f - fabsf(2.0f * l - 1.0f)) * s;
229
230 return make_float3((nr - 0.5f) * chroma + l, (ng - 0.5f) * chroma + l, (nb - 0.5f) * chroma + l);
231}
232
233ccl_device float3 xyY_to_xyz(const float x, const float y, float Y)
234{
235 float X;
236 float Z;
237
238 if (y != 0.0f) {
239 X = (x / y) * Y;
240 }
241 else {
242 X = 0.0f;
243 }
244
245 if (y != 0.0f && Y != 0.0f) {
246 Z = (1.0f - x - y) / y * Y;
247 }
248 else {
249 Z = 0.0f;
250 }
251
252 return make_float3(X, Y, Z);
253}
254
255#ifdef __KERNEL_SSE2__
256/*
257 * Calculate initial guess for arg^exp based on float representation
258 * This method gives a constant bias,
259 * which can be easily compensated by multiplication with bias_coeff.
260 * Gives better results for exponents near 1 (e. g. 4/5).
261 * exp = exponent, encoded as uint32_t
262 * e2coeff = 2^(127/exponent - 127) * bias_coeff^(1/exponent), encoded as uint32_t
263 */
264template<unsigned exp, unsigned e2coeff> ccl_device_inline float4 fastpow_sse2(const float4 &arg)
265{
266 float4 ret = arg * cast(make_int4(e2coeff));
267 ret = make_float4(cast(ret));
268 ret = ret * cast(make_int4(exp));
269 ret = cast(make_int4(ret));
270 return ret;
271}
272
273/* Improve x ^ 1.0f/5.0f solution with Newton-Raphson method */
274ccl_device_inline float4 improve_5throot_solution_sse2(const float4 &old_result, const float4 &x)
275{
276 const float4 approx2 = old_result * old_result;
277 const float4 approx4 = approx2 * approx2;
278 const float4 t = x / approx4;
279 const float4 summ = madd(make_float4(4.0f), old_result, t);
280 return summ * make_float4(1.0f / 5.0f);
281}
282
283/* Calculate powf(x, 2.4). Working domain: 1e-10 < x < 1e+10 */
284ccl_device_inline float4 fastpow24_sse2(const float4 &arg)
285{
286 /* `max`, `avg` and |avg| errors were calculated in GCC without FMA instructions.
287 * The final precision should be better than `powf` in GLIBC. */
288
289 /* Calculate x^4/5, coefficient 0.994 was constructed manually to minimize avg error */
290 /* 0x3F4CCCCD = 4/5 */
291 /* 0x4F55A7FB = 2^(127/(4/5) - 127) * 0.994^(1/(4/5)) */
292 float4 x = fastpow_sse2<0x3F4CCCCD, 0x4F55A7FB>(
293 arg); // error max = 0.17 avg = 0.0018 |avg| = 0.05
294 const float4 arg2 = arg * arg;
295 const float4 arg4 = arg2 * arg2;
296
297 /* error max = 0.018 avg = 0.0031 |avg| = 0.0031 */
298 x = improve_5throot_solution_sse2(x, arg4);
299 /* error max = 0.00021 avg = 1.6e-05 |avg| = 1.6e-05 */
300 x = improve_5throot_solution_sse2(x, arg4);
301 /* error max = 6.1e-07 avg = 5.2e-08 |avg| = 1.1e-07 */
302 x = improve_5throot_solution_sse2(x, arg4);
303
304 return x * (x * x);
305}
306
307ccl_device float4 color_srgb_to_linear_sse2(const float4 &c)
308{
309 const int4 cmp = c < make_float4(0.04045f);
310 const float4 lt = max(c * make_float4(1.0f / 12.92f), make_float4(0.0f));
311 const float4 gtebase = (c + make_float4(0.055f)) * make_float4(1.0f / 1.055f); /* fma */
312 const float4 gte = fastpow24_sse2(gtebase);
313 return select(cmp, lt, gte);
314}
315#endif /* __KERNEL_SSE2__ */
316
322
328
334
336{
337#ifdef __KERNEL_SSE2__
338 float4 r = c;
339 r = color_srgb_to_linear_sse2(r);
340 r.w = c.w;
341 return r;
342#else
343 return make_float4(
345#endif
346}
347
349{
350 color += one_float3();
351 if (variance) {
352 *variance *= sqr(one_float3() / color);
353 }
354 return log(color);
355}
356
358{
359 return exp(color) - one_float3();
360}
361
362/* Color division */
363
365{
367 GET_SPECTRUM_CHANNEL(a, i) = (GET_SPECTRUM_CHANNEL(a, i) != 0.0f) ?
368 1.0f / GET_SPECTRUM_CHANNEL(a, i) :
369 0.0f;
370 }
371
372 return a;
373}
374
375/* Returns `a/b`, and replace the channel value with `fallback` if `b == 0`. */
377{
379 GET_SPECTRUM_CHANNEL(a, i) = (GET_SPECTRUM_CHANNEL(b, i) != 0.0f) ?
381 fallback;
382 }
383
384 return a;
385}
386
388{
389 float x;
390 float y;
391 float z;
392
393 x = (b.x != 0.0f) ? a.x / b.x : 0.0f;
394 y = (b.y != 0.0f) ? a.y / b.y : 0.0f;
395 z = (b.z != 0.0f) ? a.z / b.z : 0.0f;
396
397 /* try to get gray even if b is zero */
398 if (b.x == 0.0f) {
399 if (b.y == 0.0f) {
400 x = z;
401 y = z;
402 }
403 else if (b.z == 0.0f) {
404 x = y;
405 z = y;
406 }
407 else {
408 x = 0.5f * (y + z);
409 }
410 }
411 else if (b.y == 0.0f) {
412 if (b.z == 0.0f) {
413 y = x;
414 z = x;
415 }
416 else {
417 y = 0.5f * (x + z);
418 }
419 }
420 else if (b.z == 0.0f) {
421 z = 0.5f * (x + y);
422 }
423
424 return make_float3(x, y, z);
425}
426
#define Z
#define Y
btScalar max
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
ccl_device_inline float4 color_uchar4_to_float4(const uchar4 c)
Definition color.h:45
ccl_device_inline Spectrum safe_divide_color(Spectrum a, Spectrum b, const float fallback=0.0f)
Definition color.h:376
CCL_NAMESPACE_BEGIN ccl_device uchar4 color_float_to_byte(const float3 c)
Definition color.h:12
ccl_device float3 color_highlight_compress(float3 color, ccl_private float3 *variance)
Definition color.h:348
ccl_device_inline Spectrum safe_invert_color(Spectrum a)
Definition color.h:364
ccl_device float3 color_linear_to_srgb_v3(const float3 c)
Definition color.h:323
ccl_device_inline float3 safe_divide_even_color(const float3 a, const float3 b)
Definition color.h:387
ccl_device float color_srgb_to_linear(const float c)
Definition color.h:51
ccl_device float4 color_srgb_to_linear_v4(const float4 c)
Definition color.h:335
ccl_device float3 color_highlight_uncompress(const float3 color)
Definition color.h:357
ccl_device float3 color_srgb_to_linear_v3(const float3 c)
Definition color.h:317
ccl_device_inline float3 color_byte_to_float(const uchar4 c)
Definition color.h:40
ccl_device uchar4 color_float4_to_uchar4(const float4 c)
Definition color.h:25
ccl_device float4 color_linear_to_srgb_v4(const float4 c)
Definition color.h:329
ccl_device float color_linear_to_srgb(const float c)
Definition color.h:59
#define ccl_private
#define ccl_device_inline
#define FOREACH_SPECTRUM_CHANNEL(counter)
#define GET_SPECTRUM_CHANNEL(v, i)
#define powf(x, y)
#define CCL_NAMESPACE_END
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
ccl_device_forceinline uchar4 make_uchar4(const uchar x, const uchar y, const uchar z, const uchar w)
ccl_device_forceinline int4 make_int4(const int x, const int y, const int z, const int w)
void hsl_to_rgb(float4 hsl, float4 &outcol)
void rgb_to_hsv(float4 rgb, float4 &outcol)
void hsv_to_rgb(float4 hsv, float4 &outcol)
void rgb_to_hsl(float4 rgb, float4 &outcol)
#define log
#define exp
#define select(A, B, C)
constexpr T clamp(T, U, U) RET
unsigned int uchar
float3 xyY_to_xyz(float x, float y, float Y)
ccl_device_inline uchar float_to_byte(const float val)
Definition math_base.h:999
CCL_NAMESPACE_BEGIN ccl_device_inline float madd(const float a, const float b, const float c)
Definition math_fast.h:35
ccl_device_inline float3 one_float3()
Definition math_float3.h:26
ccl_device_inline int4 cast(const float4 a)
Definition math_float4.h:32
#define X(TYPE)
#define sqr
#define floorf
#define fabsf
#define ccl_device
#define fmaxf
#define make_float4
#define min(a, b)
Definition sort.cc:38
float z
Definition sky_math.h:136
float y
Definition sky_math.h:136
float x
Definition sky_math.h:136
float y
Definition sky_math.h:225
float z
Definition sky_math.h:225
float x
Definition sky_math.h:225
float w
Definition sky_math.h:225
uchar z
uchar w
uchar x
uchar y
float3 Spectrum