Extensible Color Palette

This shader allows you to turn greyscale colors (based on highest value of red, green, or blue) into defined colors in the shader parameters.

By default, it uses 9 colors, and can be programmed for more or fewer designated colors.

When adding more colors in the variables, (ie color0, color1, etc.) each variable’s name must be “color#”, where the # denotes its number. the variables must be numbered from 0 to “#-1” where “#” is the number of colors you have.

The second part of adding colors is by editing the vec4 array named “colors”. Each “color#” variable needs to be present in this list for that color to appear in the shader.
(All of the multiplication and division is based on the array’s length).

The order in which the “color#” variables are listed in the array is the same order in which they will appear on their respective greyscale value.

If you want to define 12 colors that you want to use, make sure you have variables “color0” to “color11”, and list them in the array named “colors[]”

[*Check Mark*] – I’m sure that I can make this shader even more efficient by reducing all of the unnecessary “vec3″s to floats, and process single values rather than carrying around 3 for each pixel. I’ll get around to it soon.

-update March 29, 2022:
      I’ve updated the code to use more floats, and made the entirety of the shader more readable. It should be easier (and more fun) to follow now.

 

Shader code
shader_type canvas_item; 
render_mode blend_mix, unshaded;

uniform vec4 color0: hint_color;
uniform vec4 color1: hint_color;
uniform vec4 color2: hint_color;
uniform vec4 color3: hint_color;
uniform vec4 color4: hint_color;
uniform vec4 color5: hint_color;
uniform vec4 color6: hint_color;
uniform vec4 color7: hint_color;
uniform vec4 color8: hint_color;

void fragment() {
	
	
// 1: Define the color list

	vec4 colors[] = {color0,color1,color2,color3,color4,color5,color6,color7,color8};

	

// 2: Grab the junk off of the screen

	vec3 scrn_uv = vec3(UV, 0.0);
	vec3 color_input;
	float alpa_input;
	{
		vec4 _tex_read = textureLod(TEXTURE, scrn_uv.xy, 0.0);
		color_input = _tex_read.rgb;
		alpa_input = _tex_read.a;
	}

// 3: Turn that junk into the superior "Grey"

	float grey_value;
	{
		vec3 c = color_input;
		float max1 = max(c.r, c.g);
		float max2 = max(max1, c.b);
		grey_value = max2;
	}

// 4: Multiply the superior "Grey" into the ultimate "Multiplied Grey"

	float multiplier = float(colors.length()-1);
	float multiplied_grey = grey_value * multiplier;

// 5: Round the ultimate "Multiplied Grey" to integer-like values; Achieve god-like "Rounded Multiplied Grey"

	float round_mult_grey = round(multiplied_grey);

// 6: Use the power of your new god to pick your output colors based on their color index.

	vec4 out_color = colors[int(round(multiplied_grey))];
	

// 7: Meet your new friends: the output colors.

	COLOR = out_color;
	COLOR.a = alpa_input;

// 8: Profit.

}
Tags
Color, color palette, palette
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 splash (show only one color)

Earthbound-like battle background shader w/scroll effect and palette cycling

Palette Filter For 3D and 2D

guest

0 Comments
Inline Feedbacks
View all comments