Replace Color

A simple replace color shader.

Based on: Green color replacement shader.

Shader code
shader_type canvas_item;

uniform float tolerance: hint_range(0.0, 1.0, 0.01) = 0.1;
uniform vec4 prev_color: source_color = vec4(1.0);
uniform vec4 new_color: source_color = vec4(0.0, 0.0, 0.0, 1.0);

void fragment() {
    vec4 color = texture(TEXTURE, UV);
    vec4 initial_color = color;

	float color_distance = distance(prev_color, color);

    if (color_distance <= tolerance) {
		color = mix(new_color, color, color_distance);
		color.a = initial_color.a;
    }

    COLOR = color;
}
Tags
Color, replace
The shader code and all code snippets in this post are under CC0 license and can be used freely without the author's permission. Images and videos, and assets depicted in those, do not fall under this license. For more info, see our License terms.

Related shaders

Color shift Color Reducer post-processing

color splash (show only one color)

Gameboy Color Palette Shader

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments