Blender  V2.93
node_texture_hueSatVal.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2006 Blender Foundation.
17  * All rights reserved.
18  */
19 
24 #include "NOD_texture.h"
25 #include "node_texture_util.h"
26 
28  {SOCK_FLOAT, N_("Hue"), 0.0f, 0.0f, 0.0f, 0.0f, -0.5f, 0.5f, PROP_NONE},
29  {SOCK_FLOAT, N_("Saturation"), 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 2.0f, PROP_NONE},
30  {SOCK_FLOAT, N_("Value"), 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 2.0f, PROP_NONE},
31  {SOCK_FLOAT, N_("Factor"), 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, PROP_NONE},
32  {SOCK_RGBA, N_("Color"), 0.8f, 0.8f, 0.8f, 1.0f},
33  {-1, ""},
34 };
36  {SOCK_RGBA, N_("Color")},
37  {-1, ""},
38 };
39 
40 static void do_hue_sat_fac(
41  bNode *UNUSED(node), float *out, float hue, float sat, float val, float *in, float fac)
42 {
43  if (fac != 0 && (hue != 0.5f || sat != 1 || val != 1)) {
44  float col[3], hsv[3], mfac = 1.0f - fac;
45 
46  rgb_to_hsv(in[0], in[1], in[2], hsv, hsv + 1, hsv + 2);
47  hsv[0] += (hue - 0.5f);
48  if (hsv[0] > 1.0f) {
49  hsv[0] -= 1.0f;
50  }
51  else if (hsv[0] < 0.0f) {
52  hsv[0] += 1.0f;
53  }
54  hsv[1] *= sat;
55  if (hsv[1] > 1.0f) {
56  hsv[1] = 1.0f;
57  }
58  else if (hsv[1] < 0.0f) {
59  hsv[1] = 0.0f;
60  }
61  hsv[2] *= val;
62  if (hsv[2] > 1.0f) {
63  hsv[2] = 1.0f;
64  }
65  else if (hsv[2] < 0.0f) {
66  hsv[2] = 0.0f;
67  }
68  hsv_to_rgb(hsv[0], hsv[1], hsv[2], col, col + 1, col + 2);
69 
70  out[0] = mfac * in[0] + fac * col[0];
71  out[1] = mfac * in[1] + fac * col[1];
72  out[2] = mfac * in[2] + fac * col[2];
73  }
74  else {
75  copy_v4_v4(out, in);
76  }
77 }
78 
79 static void colorfn(float *out, TexParams *p, bNode *node, bNodeStack **in, short thread)
80 {
81  float hue = tex_input_value(in[0], p, thread);
82  float sat = tex_input_value(in[1], p, thread);
83  float val = tex_input_value(in[2], p, thread);
84  float fac = tex_input_value(in[3], p, thread);
85 
86  float col[4];
87  tex_input_rgba(col, in[4], p, thread);
88 
89  hue += 0.5f; /* [-0.5, 0.5] -> [0, 1] */
90 
91  do_hue_sat_fac(node, out, hue, sat, val, col, fac);
92 
93  out[3] = col[3];
94 }
95 
96 static void exec(void *data,
97  int UNUSED(thread),
98  bNode *node,
99  bNodeExecData *execdata,
100  bNodeStack **in,
101  bNodeStack **out)
102 {
103  tex_output(node, execdata, in, out[0], &colorfn, data);
104 }
105 
107 {
108  static bNodeType ntype;
109 
110  tex_node_type_base(&ntype, TEX_NODE_HUE_SAT, "Hue Saturation Value", NODE_CLASS_OP_COLOR, 0);
113  node_type_exec(&ntype, NULL, NULL, exec);
114 
115  nodeRegisterType(&ntype);
116 }
void node_type_socket_templates(struct bNodeType *ntype, struct bNodeSocketTemplate *inputs, struct bNodeSocketTemplate *outputs)
Definition: node.cc:4527
void node_type_size_preset(struct bNodeType *ntype, eNodeSizePreset size)
Definition: node.cc:4577
#define NODE_CLASS_OP_COLOR
Definition: BKE_node.h:336
void node_type_exec(struct bNodeType *ntype, NodeInitExecFunction init_exec_fn, NodeFreeExecFunction free_exec_fn, NodeExecFunction exec_fn)
Definition: node.cc:4635
void nodeRegisterType(struct bNodeType *ntype)
Definition: node.cc:1298
@ NODE_SIZE_MIDDLE
Definition: BKE_node.h:372
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
MINLINE void copy_v4_v4(float r[4], const float a[4])
#define UNUSED(x)
#define N_(msgid)
@ SOCK_FLOAT
@ SOCK_RGBA
Group RGB to Bright Vector Camera Vector Combine Material Light Line Style Layer Add Ambient Diffuse Glossy Refraction Transparent Toon Principled Hair Volume Principled Light Particle Volume Image Sky Noise Wave Voronoi Brick Texture Vector Combine Vertex Separate Vector White RGB Map Separate Set Z Dilate Combine Combine Color Channel Split ID Combine Luminance Directional Alpha Distance Hue Movie Ellipse Bokeh View Corner Anti Mix RGB TEX_NODE_HUE_SAT
@ PROP_NONE
Definition: RNA_types.h:113
OperationNode * node
uint col
static bNodeSocketTemplate outputs[]
static void do_hue_sat_fac(bNode *UNUSED(node), float *out, float hue, float sat, float val, float *in, float fac)
static bNodeSocketTemplate inputs[]
static void exec(void *data, int UNUSED(thread), bNode *node, bNodeExecData *execdata, bNodeStack **in, bNodeStack **out)
static void colorfn(float *out, TexParams *p, bNode *node, bNodeStack **in, short thread)
void register_node_type_tex_hue_sat(void)
void tex_node_type_base(struct bNodeType *ntype, int type, const char *name, short nclass, short flag)
float tex_input_value(bNodeStack *in, TexParams *params, short thread)
void tex_input_rgba(float *out, bNodeStack *in, TexParams *params, short thread)
void tex_output(bNode *node, bNodeExecData *execdata, bNodeStack **in, bNodeStack *out, TexFn texfn, TexCallData *cdata)
Compact definition of a node socket.
Definition: BKE_node.h:95
Defines a node type.
Definition: BKE_node.h:221