Blender  V2.93
svm_hsv.h
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2013 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef __SVM_HSV_H__
18 #define __SVM_HSV_H__
19 
21 
23  KernelGlobals *kg, ShaderData *sd, float *stack, uint4 node, int *offset)
24 {
25  uint in_color_offset, fac_offset, out_color_offset;
26  uint hue_offset, sat_offset, val_offset;
27  svm_unpack_node_uchar3(node.y, &in_color_offset, &fac_offset, &out_color_offset);
28  svm_unpack_node_uchar3(node.z, &hue_offset, &sat_offset, &val_offset);
29 
30  float fac = stack_load_float(stack, fac_offset);
31  float3 in_color = stack_load_float3(stack, in_color_offset);
32  float3 color = in_color;
33 
34  float hue = stack_load_float(stack, hue_offset);
35  float sat = stack_load_float(stack, sat_offset);
36  float val = stack_load_float(stack, val_offset);
37 
38  color = rgb_to_hsv(color);
39 
40  /* Remember: `fmodf` doesn't work for negative numbers here. */
41  color.x = fmodf(color.x + hue + 0.5f, 1.0f);
42  color.y = saturate(color.y * sat);
43  color.z *= val;
44 
45  color = hsv_to_rgb(color);
46 
47  color.x = fac * color.x + (1.0f - fac) * in_color.x;
48  color.y = fac * color.y + (1.0f - fac) * in_color.y;
49  color.z = fac * color.z + (1.0f - fac) * in_color.z;
50 
51  /* Clamp color to prevent negative values caused by over saturation. */
52  color.x = max(color.x, 0.0f);
53  color.y = max(color.y, 0.0f);
54  color.z = max(color.z, 0.0f);
55 
56  if (stack_valid(out_color_offset))
57  stack_store_float3(stack, out_color_offset, color);
58 }
59 
61 
62 #endif /* __SVM_HSV_H__ */
void rgb_to_hsv(float r, float g, float b, float *r_h, float *r_s, float *r_v)
Definition: math_color.c:229
void hsv_to_rgb(float h, float s, float v, float *r_r, float *r_g, float *r_b)
Definition: math_color.c:31
unsigned int uint
Definition: BLI_sys_types.h:83
OperationNode * node
CCL_NAMESPACE_BEGIN ccl_device_inline float3 stack_load_float3(float *stack, uint a)
ccl_device_inline float stack_load_float(float *stack, uint a)
ccl_device_forceinline void svm_unpack_node_uchar3(uint i, uint *x, uint *y, uint *z)
ccl_device_inline void stack_store_float3(float *stack, uint a, float3 f)
ccl_device_inline bool stack_valid(uint a)
#define ccl_device
#define CCL_NAMESPACE_END
#define fmodf(x, y)
ShaderData
float z
Definition: sky_float3.h:35
float y
Definition: sky_float3.h:35
float x
Definition: sky_float3.h:35
CCL_NAMESPACE_BEGIN ccl_device void svm_node_hsv(KernelGlobals *kg, ShaderData *sd, float *stack, uint4 node, int *offset)
Definition: svm_hsv.h:22
float max
ccl_device_inline float saturate(float a)
Definition: util_math.h:315