Blender  V3.3
color_util.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright 2011-2022 Blender Foundation */
3 
4 #pragma once
5 
7 
9 {
10  return interp(col1, col2, t);
11 }
12 
14 {
15  return interp(col1, col1 + col2, t);
16 }
17 
19 {
20  return interp(col1, col1 * col2, t);
21 }
22 
24 {
25  float tm = 1.0f - t;
26  float3 one = make_float3(1.0f, 1.0f, 1.0f);
27  float3 tm3 = make_float3(tm, tm, tm);
28 
29  return one - (tm3 + t * (one - col2)) * (one - col1);
30 }
31 
33 {
34  float tm = 1.0f - t;
35 
36  float3 outcol = col1;
37 
38  if (outcol.x < 0.5f)
39  outcol.x *= tm + 2.0f * t * col2.x;
40  else
41  outcol.x = 1.0f - (tm + 2.0f * t * (1.0f - col2.x)) * (1.0f - outcol.x);
42 
43  if (outcol.y < 0.5f)
44  outcol.y *= tm + 2.0f * t * col2.y;
45  else
46  outcol.y = 1.0f - (tm + 2.0f * t * (1.0f - col2.y)) * (1.0f - outcol.y);
47 
48  if (outcol.z < 0.5f)
49  outcol.z *= tm + 2.0f * t * col2.z;
50  else
51  outcol.z = 1.0f - (tm + 2.0f * t * (1.0f - col2.z)) * (1.0f - outcol.z);
52 
53  return outcol;
54 }
55 
57 {
58  return interp(col1, col1 - col2, t);
59 }
60 
62 {
63  float tm = 1.0f - t;
64 
65  float3 outcol = col1;
66 
67  if (col2.x != 0.0f)
68  outcol.x = tm * outcol.x + t * outcol.x / col2.x;
69  if (col2.y != 0.0f)
70  outcol.y = tm * outcol.y + t * outcol.y / col2.y;
71  if (col2.z != 0.0f)
72  outcol.z = tm * outcol.z + t * outcol.z / col2.z;
73 
74  return outcol;
75 }
76 
78 {
79  return interp(col1, fabs(col1 - col2), t);
80 }
81 
83 {
84  return interp(col1, min(col1, col2), t);
85 }
86 
88 {
89  return interp(col1, max(col1, col2), t);
90 }
91 
93 {
94  float3 outcol = col1;
95 
96  if (outcol.x != 0.0f) {
97  float tmp = 1.0f - t * col2.x;
98  if (tmp <= 0.0f)
99  outcol.x = 1.0f;
100  else if ((tmp = outcol.x / tmp) > 1.0f)
101  outcol.x = 1.0f;
102  else
103  outcol.x = tmp;
104  }
105  if (outcol.y != 0.0f) {
106  float tmp = 1.0f - t * col2.y;
107  if (tmp <= 0.0f)
108  outcol.y = 1.0f;
109  else if ((tmp = outcol.y / tmp) > 1.0f)
110  outcol.y = 1.0f;
111  else
112  outcol.y = tmp;
113  }
114  if (outcol.z != 0.0f) {
115  float tmp = 1.0f - t * col2.z;
116  if (tmp <= 0.0f)
117  outcol.z = 1.0f;
118  else if ((tmp = outcol.z / tmp) > 1.0f)
119  outcol.z = 1.0f;
120  else
121  outcol.z = tmp;
122  }
123 
124  return outcol;
125 }
126 
128 {
129  float tmp, tm = 1.0f - t;
130 
131  float3 outcol = col1;
132 
133  tmp = tm + t * col2.x;
134  if (tmp <= 0.0f)
135  outcol.x = 0.0f;
136  else if ((tmp = (1.0f - (1.0f - outcol.x) / tmp)) < 0.0f)
137  outcol.x = 0.0f;
138  else if (tmp > 1.0f)
139  outcol.x = 1.0f;
140  else
141  outcol.x = tmp;
142 
143  tmp = tm + t * col2.y;
144  if (tmp <= 0.0f)
145  outcol.y = 0.0f;
146  else if ((tmp = (1.0f - (1.0f - outcol.y) / tmp)) < 0.0f)
147  outcol.y = 0.0f;
148  else if (tmp > 1.0f)
149  outcol.y = 1.0f;
150  else
151  outcol.y = tmp;
152 
153  tmp = tm + t * col2.z;
154  if (tmp <= 0.0f)
155  outcol.z = 0.0f;
156  else if ((tmp = (1.0f - (1.0f - outcol.z) / tmp)) < 0.0f)
157  outcol.z = 0.0f;
158  else if (tmp > 1.0f)
159  outcol.z = 1.0f;
160  else
161  outcol.z = tmp;
162 
163  return outcol;
164 }
165 
167 {
168  float3 outcol = col1;
169 
170  float3 hsv2 = rgb_to_hsv(col2);
171 
172  if (hsv2.y != 0.0f) {
173  float3 hsv = rgb_to_hsv(outcol);
174  hsv.x = hsv2.x;
175  float3 tmp = hsv_to_rgb(hsv);
176 
177  outcol = interp(outcol, tmp, t);
178  }
179 
180  return outcol;
181 }
182 
184 {
185  float tm = 1.0f - t;
186 
187  float3 outcol = col1;
188 
189  float3 hsv = rgb_to_hsv(outcol);
190 
191  if (hsv.y != 0.0f) {
192  float3 hsv2 = rgb_to_hsv(col2);
193 
194  hsv.y = tm * hsv.y + t * hsv2.y;
195  outcol = hsv_to_rgb(hsv);
196  }
197 
198  return outcol;
199 }
200 
202 {
203  float tm = 1.0f - t;
204 
205  float3 hsv = rgb_to_hsv(col1);
206  float3 hsv2 = rgb_to_hsv(col2);
207 
208  hsv.z = tm * hsv.z + t * hsv2.z;
209 
210  return hsv_to_rgb(hsv);
211 }
212 
214 {
215  float3 outcol = col1;
216  float3 hsv2 = rgb_to_hsv(col2);
217 
218  if (hsv2.y != 0.0f) {
219  float3 hsv = rgb_to_hsv(outcol);
220  hsv.x = hsv2.x;
221  hsv.y = hsv2.y;
222  float3 tmp = hsv_to_rgb(hsv);
223 
224  outcol = interp(outcol, tmp, t);
225  }
226 
227  return outcol;
228 }
229 
231 {
232  float tm = 1.0f - t;
233 
234  float3 one = make_float3(1.0f, 1.0f, 1.0f);
235  float3 scr = one - (one - col2) * (one - col1);
236 
237  return tm * col1 + t * ((one - col1) * col2 * col1 + col1 * scr);
238 }
239 
241 {
242  return col1 + t * (2.0f * col2 + make_float3(-1.0f, -1.0f, -1.0f));
243 }
244 
246 {
247  return saturate(col);
248 }
249 
251 {
252  float t = saturatef(fac);
253 
254  switch (type) {
255  case NODE_MIX_BLEND:
256  return svm_mix_blend(t, c1, c2);
257  case NODE_MIX_ADD:
258  return svm_mix_add(t, c1, c2);
259  case NODE_MIX_MUL:
260  return svm_mix_mul(t, c1, c2);
261  case NODE_MIX_SCREEN:
262  return svm_mix_screen(t, c1, c2);
263  case NODE_MIX_OVERLAY:
264  return svm_mix_overlay(t, c1, c2);
265  case NODE_MIX_SUB:
266  return svm_mix_sub(t, c1, c2);
267  case NODE_MIX_DIV:
268  return svm_mix_div(t, c1, c2);
269  case NODE_MIX_DIFF:
270  return svm_mix_diff(t, c1, c2);
271  case NODE_MIX_DARK:
272  return svm_mix_dark(t, c1, c2);
273  case NODE_MIX_LIGHT:
274  return svm_mix_light(t, c1, c2);
275  case NODE_MIX_DODGE:
276  return svm_mix_dodge(t, c1, c2);
277  case NODE_MIX_BURN:
278  return svm_mix_burn(t, c1, c2);
279  case NODE_MIX_HUE:
280  return svm_mix_hue(t, c1, c2);
281  case NODE_MIX_SAT:
282  return svm_mix_sat(t, c1, c2);
283  case NODE_MIX_VAL:
284  return svm_mix_val(t, c1, c2);
285  case NODE_MIX_COLOR:
286  return svm_mix_color(t, c1, c2);
287  case NODE_MIX_SOFT:
288  return svm_mix_soft(t, c1, c2);
289  case NODE_MIX_LINEAR:
290  return svm_mix_linear(t, c1, c2);
291  case NODE_MIX_CLAMP:
292  return svm_mix_clamp(c1);
293  }
294 
295  return make_float3(0.0f, 0.0f, 0.0f);
296 }
297 
298 ccl_device_inline float3 svm_brightness_contrast(float3 color, float brightness, float contrast)
299 {
300  float a = 1.0f + contrast;
301  float b = brightness - contrast * 0.5f;
302 
303  color.x = max(a * color.x + b, 0.0f);
304  color.y = max(a * color.y + b, 0.0f);
305  color.z = max(a * color.z + b, 0.0f);
306 
307  return color;
308 }
309 
311 {
312  switch (type) {
314  return hsv_to_rgb(color);
316  return hsl_to_rgb(color);
318  default:
319  return color;
320  }
321 }
322 
324 {
325  switch (type) {
327  return rgb_to_hsv(color);
329  return rgb_to_hsl(color);
331  default:
332  return color;
333  }
334 }
335 
void rgb_to_hsl(float r, float g, float b, float *r_h, float *r_s, float *r_l)
Definition: math_color.c:237
void rgb_to_hsv(float r, float g, float b, float *r_h, float *r_s, float *r_v)
Definition: math_color.c:208
void hsv_to_rgb(float h, float s, float v, float *r_r, float *r_g, float *r_b)
Definition: math_color.c:13
void hsl_to_rgb(float h, float s, float l, float *r_r, float *r_g, float *r_b)
Definition: math_color.c:30
@ NODE_COMBSEP_COLOR_RGB
@ NODE_COMBSEP_COLOR_HSV
@ NODE_COMBSEP_COLOR_HSL
_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 type
_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 GLdouble y2 _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat y2 _GL_VOID_RET _GL_VOID GLint GLint GLint y2 _GL_VOID_RET _GL_VOID GLshort GLshort GLshort y2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLuint *buffer _GL_VOID_RET _GL_VOID GLdouble t _GL_VOID_RET _GL_VOID GLfloat t _GL_VOID_RET _GL_VOID GLint t _GL_VOID_RET _GL_VOID GLshort t _GL_VOID_RET _GL_VOID GLdouble t
Group Output data from inside of a node group A color picker Mix two input colors RGB to Convert a color s luminance to a grayscale value Generate a normal vector and a dot product Bright Control the brightness and contrast of the input color Vector Map an input vectors to used to fine tune the interpolation of the input Camera Retrieve information about the camera and how it relates to the current shading point s position Clamp a value between a minimum and a maximum Vector Perform vector math operation Invert a color
ccl_device float3 svm_mix_dodge(float t, float3 col1, float3 col2)
Definition: color_util.h:92
ccl_device_noinline_cpu float3 svm_mix(NodeMix type, float fac, float3 c1, float3 c2)
Definition: color_util.h:250
ccl_device float3 svm_mix_overlay(float t, float3 col1, float3 col2)
Definition: color_util.h:32
ccl_device float3 svm_mix_clamp(float3 col)
Definition: color_util.h:245
ccl_device float3 svm_combine_color(NodeCombSepColorType type, float3 color)
Definition: color_util.h:310
ccl_device float3 svm_mix_val(float t, float3 col1, float3 col2)
Definition: color_util.h:201
ccl_device float3 svm_mix_screen(float t, float3 col1, float3 col2)
Definition: color_util.h:23
ccl_device float3 svm_mix_soft(float t, float3 col1, float3 col2)
Definition: color_util.h:230
ccl_device float3 svm_mix_add(float t, float3 col1, float3 col2)
Definition: color_util.h:13
ccl_device float3 svm_separate_color(NodeCombSepColorType type, float3 color)
Definition: color_util.h:323
ccl_device float3 svm_mix_dark(float t, float3 col1, float3 col2)
Definition: color_util.h:82
ccl_device float3 svm_mix_diff(float t, float3 col1, float3 col2)
Definition: color_util.h:77
ccl_device float3 svm_mix_mul(float t, float3 col1, float3 col2)
Definition: color_util.h:18
ccl_device float3 svm_mix_sub(float t, float3 col1, float3 col2)
Definition: color_util.h:56
ccl_device float3 svm_mix_light(float t, float3 col1, float3 col2)
Definition: color_util.h:87
ccl_device float3 svm_mix_sat(float t, float3 col1, float3 col2)
Definition: color_util.h:183
ccl_device float3 svm_mix_color(float t, float3 col1, float3 col2)
Definition: color_util.h:213
ccl_device float3 svm_mix_burn(float t, float3 col1, float3 col2)
Definition: color_util.h:127
ccl_device float3 svm_mix_div(float t, float3 col1, float3 col2)
Definition: color_util.h:61
CCL_NAMESPACE_BEGIN ccl_device float3 svm_mix_blend(float t, float3 col1, float3 col2)
Definition: color_util.h:8
ccl_device float3 svm_mix_hue(float t, float3 col1, float3 col2)
Definition: color_util.h:166
ccl_device_inline float3 svm_brightness_contrast(float3 color, float brightness, float contrast)
Definition: color_util.h:298
ccl_device float3 svm_mix_linear(float t, float3 col1, float3 col2)
Definition: color_util.h:240
#define ccl_device
Definition: cuda/compat.h:32
#define ccl_device_noinline_cpu
Definition: cuda/compat.h:41
#define ccl_device_inline
Definition: cuda/compat.h:34
#define CCL_NAMESPACE_END
Definition: cuda/compat.h:9
uint col
@ NODE_MIX_DIV
@ NODE_MIX_SOFT
@ NODE_MIX_CLAMP
@ NODE_MIX_LIGHT
@ NODE_MIX_MUL
@ NODE_MIX_DIFF
@ NODE_MIX_BURN
@ NODE_MIX_SUB
@ NODE_MIX_LINEAR
@ NODE_MIX_DARK
@ NODE_MIX_SAT
@ NODE_MIX_COLOR
@ NODE_MIX_SCREEN
@ NODE_MIX_HUE
@ NODE_MIX_BLEND
@ NODE_MIX_OVERLAY
@ NODE_MIX_DODGE
@ NODE_MIX_VAL
@ NODE_MIX_ADD
NodeCombSepColorType
ccl_device_inline float2 interp(const float2 &a, const float2 &b, float t)
Definition: math_float2.h:232
ccl_device_inline float2 fabs(const float2 &a)
Definition: math_float2.h:222
ccl_device_inline float3 saturate(float3 a)
Definition: math_float3.h:387
#define make_float3(x, y, z)
Definition: metal/compat.h:204
static unsigned a[3]
Definition: RandGen.cpp:78
static const pxr::TfToken b("b", pxr::TfToken::Immortal)
#define min(a, b)
Definition: sort.c:35
float z
float y
float x
float max
ccl_device_inline float saturatef(float a)
Definition: util/math.h:404