shader_type canvas_item; // Input textures for source and target palettes. uniform sampler2D source_palette : source_color; uniform sampler2D target_palette : source_color; uniform int color_count = 6; // Default to 6 colors void fragment() { // Get the current pixel color vec3 current_color = COLOR.rgb; // Find the matching color in the source palette bool found = false; for (int i = 0; i < color_count; i++) { // Sample exact pixel from the palette texture // For a 6x1 texture, the centers of the pixels are at x coordinates: 0.5/6, 1.5/6, 2.5/6, etc. float x_coord = (float(i) + 0.5) / float(color_count); vec3 palette_color = texture(source_palette, vec2(x_coord, 0.5)).rgb; // Compare colors (with a small tolerance for floating point precision) if (distance(current_color, palette_color) < 0.01) { // Get the corresponding color from the target palette at the same position vec3 target_color = texture(target_palette, vec2(x_coord, 0.5)).rgb; COLOR.rgb = target_color; } } }