Blender V5.3
node_composite_color_key.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2006 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BLI_math_base.hh"
6#include "BLI_math_color_c.hh"
8
10
11#include "NOD_multi_function.hh"
12
13#include "BKE_node.hh"
14
15#include "GPU_material.hh"
16
17#include "COM_result.hh"
18
20
22
24{
25 b.use_custom_socket_order();
26 b.allow_any_socket_order();
27 b.is_function_node();
28 b.add_input<decl::Color>("Image"_ustr).default_value({1.0f, 1.0f, 1.0f, 1.0f}).hide_value();
29 b.add_output<decl::Color>("Image"_ustr).align_with_previous();
30 b.add_output<decl::Float>("Matte"_ustr);
31
32 b.add_input<decl::Color>("Key Color"_ustr).default_value({1.0f, 1.0f, 1.0f, 1.0f});
33 b.add_input<decl::Float>("Hue"_ustr)
34 .default_value(0.01f)
36 .min(0.0f)
37 .max(1.0f)
38 .description(
39 "If the difference in hue between the color and key color is less than this threshold, "
40 "it is keyed");
41 b.add_input<decl::Float>("Saturation"_ustr)
42 .default_value(0.1f)
44 .min(0.0f)
45 .max(1.0f)
46 .description(
47 "If the difference in saturation between the color and key color is less than this "
48 "threshold, it is keyed");
49 b.add_input<decl::Float>("Value"_ustr)
50 .default_value(0.1f)
52 .min(0.0f)
53 .max(1.0f)
54 .description(
55 "If the difference in value between the color and key color is less than this "
56 "threshold, it is keyed")
57 .translation_context(BLT_I18NCONTEXT_COLOR);
58}
59
60using namespace blender::compositor;
61
62static int node_gpu_material(GPUMaterial *material,
63 bNode *node,
64 bNodeExecData * /*execdata*/,
65 GPUNodeStack *inputs,
66 GPUNodeStack *outputs)
67{
68 return GPU_stack_link(material, node, "node_composite_color_matte", inputs, outputs);
69}
70
71static void color_matte(const float4 color,
72 const float4 key,
73 const float hue_threshold,
74 const float saturation_epsilon,
75 const float value_epsilon,
77 float &matte)
78{
79 float3 color_hsva;
80 rgb_to_hsv_v(color, color_hsva);
81 float3 key_hsva;
82 rgb_to_hsv_v(key, key_hsva);
83
84 /* Divide by 2 because the hue wraps around. */
85 float hue_epsilon = hue_threshold / 2.0f;
86
87 bool is_within_saturation = math::distance(color_hsva.y, key_hsva.y) < saturation_epsilon;
88 bool is_within_value = math::distance(color_hsva.z, key_hsva.z) < value_epsilon;
89 bool is_within_hue = math::distance(color_hsva.x, key_hsva.x) < hue_epsilon;
90 /* Hue wraps around, so check the distance around the boundary. */
91 float min_hue = math::min(color_hsva.x, key_hsva.x);
92 float max_hue = math::max(color_hsva.x, key_hsva.x);
93 is_within_hue = is_within_hue || ((min_hue + (1.0f - max_hue)) < hue_epsilon);
94
95 matte = (is_within_hue && is_within_saturation && is_within_value) ? 0.0f : color.w;
96 result = color * matte;
97}
98
100
102{
104 return mf::build::SI5_SO2<Color, Color, float, float, float, Color, float>(
105 "Color Key",
106 [=](const Color &color,
107 const Color &key_color,
108 const float &hue,
109 const float &saturation,
110 const float &value,
111 Color &output_color,
112 float &matte) -> void {
113 float4 out_color;
114 color_matte(float4(color), float4(key_color), hue, saturation, value, out_color, matte);
115 output_color = Color(out_color);
116 },
117 mf::build::exec_presets::SomeSpanOrSingle<0, 1>());
118 });
119}
120
121static void node_register()
122{
123 static bke::bNodeType ntype;
124
125 cmp_node_type_base(&ntype, "CompositorNodeColorMatte"_ustr, CMP_NODE_COLOR_MATTE);
126 ntype.ui_name = "Color Key";
127 ntype.ui_description = "Create matte using a given color, for green or blue screen footage";
128 ntype.enum_name_legacy = "COLOR_MATTE";
129 ntype.nclass = NODE_CLASS_MATTE;
130 ntype.declare = node_declare;
131 ntype.flag |= NODE_PREVIEW;
135
137}
139
140} // namespace blender::nodes::node_composite_color_key_cc
#define NODE_CLASS_MATTE
Definition BKE_node.hh:485
#define CMP_NODE_COLOR_MATTE
#define BLT_I18NCONTEXT_COLOR
#define NOD_REGISTER_NODE(REGISTER_FUNC)
void construct_and_set_matching_fn_cb(Fn &&create_multi_function)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
ColorSceneLinear4f< eAlpha::Premultiplied > Color
Definition COM_result.hh:96
T distance(const T &a, const T &b)
T min(const T &a, const T &b)
T max(const T &a, const T &b)
static void node_declare(NodeDeclarationBuilder &b)
static void node_build_multi_function(nodes::NodeMultiFunctionBuilder &builder)
static int node_gpu_material(GPUMaterial *material, bNode *node, bNodeExecData *, GPUNodeStack *inputs, GPUNodeStack *outputs)
static void color_matte(const float4 color, const float4 key, const float hue_threshold, const float saturation_epsilon, const float value_epsilon, float4 &result, float &matte)
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
void rgb_to_hsv_v(const float rgb[3], float r_hsv[3])
@ PROP_FACTOR
Definition RNA_types.hh:308
void cmp_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
static constexpr int _160
Definition BKE_node.hh:246
Defines a node type.
Definition BKE_node.hh:265
std::string ui_description
Definition BKE_node.hh:271
NodeGPUExecFunction gpu_fn
Definition BKE_node.hh:373
NodeMultiFunctionBuildFunction build_multi_function
Definition BKE_node.hh:382
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeDeclareFunction declare
Definition BKE_node.hh:393