Only the Palette: a Colour Tint Shader That Keeps Pixel Art Colours
The rules so far answered with numbers they found in the picture or in a table. This one has to work a colour out: it mixes every colour of a scene with one tint, and still answers with a colour of the palette. It does that by finding the palette's nearest colour in red, green and blue, and that last step is the one an AI most often gets wrong.

The rule mixes every colour with deep blue in red, green and blue, then takes the colour of the palette that lies nearest. The beach grows cold, and still the tree stays green and the sea blue, until the deep blue takes over.
Also in this lesson 3 things to read up on 3 questions to check yourself
The idea
A tint lays one colour over a whole scene: the cold blue of a winter evening or of deep water, red when the player is hurt, brown for an old memory. On an ordinary screen a tint mixes each colour with the tint's colour and shows the mixture, which can be any of millions of colours. In pixel art the palette is part of the look. A scene drawn in 25 colours that suddenly shows hundreds of colours in between no longer looks like pixel art.
So this rule mixes, and then goes back to the palette: it takes the palette's colour that lies nearest to the mixture. The scene takes on the tint, and every pixel is still one of the 25.
How it works
Each colour of the palette is three whole numbers: how much red, how much green and how much blue it has, each from 0 to 255. The tree's green is 15, 138, 76; amber is 224, 138, 46. The rule keeps all 25 colours in a table.
To mix, it takes so many parts of 100 of the tint and the rest of the pixel's own colour, for red, green and blue each, and rounds down. A fifth of amber on the tree's green gives 56, 138, 70: the red comes from 80 parts of 15 and 20 parts of 224, and so on.
To go back to the palette, it measures how far the mixture is from each of the 25 colours: the difference in red, squared, plus the difference in green, squared, plus the difference in blue, squared. The smallest sum wins. Squares make one big difference count for more than several small ones that add up to the same, and nothing needs a square root. If two colours are exactly as far away, the lower number wins, in the rule and in the shader alike, so both give the same picture.
One slider sets how much of the tint is mixed in, from 0 to 100; it starts at 30. The other chooses the tint by its number in the palette; it starts at 13, deep blue.
What goes wrong
Many palette shaders compare brightness alone. They work out how bright the mixture is and take the palette's colour whose brightness is nearest. That is less work, and it is how shaders for screens with four shades of one colour work, where brightness is all there is. With a palette of many hues it fails, because brightness says nothing about whether a colour is green or violet.
Take that fifth of amber on green again. The mixture, 56, 138, 70, is still plainly green, and the rule finds green. By brightness alone the nearest colour of this palette is violet.
Switch the picture to "With the mistake". As the tint rises, the sky turns yellow, the sea green, the sand blue and then green, and the honey jars green. Every pixel keeps about its brightness and loses its hue.
Three tests catch it: a fifth of amber on green must stay green; the sky with 30 parts of violet must become light blue, not pink; and a mixture exactly halfway between grey violet and pink must take the lower number. The mistake answers violet, pink and amber.
The mistake, line by line
- /* A tint that stays in the palette, with the mistake: the mixture is put back to the palette's colour
- whose brightness is nearest, as many palette shaders do, instead of the colour nearest in red, green
- and blue. Brightness says nothing about the hue: a little amber on green finds a violet as bright as
- the green was, and a sky with some violet in it turns pink.
+ /* A tint that stays in the palette: every colour is mixed with one colour, the tint, in red, green
+ and blue, and the mixture is then put back to the palette's nearest colour. So a whole scene can take
+ on the gold of an evening, the blue of deep water or the red of danger, and every pixel is still one
+ of the palette's 25 colours. Everything is whole numbers: red, green and blue from 0 to 255, the
+ tint in parts of 100, and distances compared squared.
+ The pixel art way: a picture is a grid of whole numbers, 0 where nothing is and 1 to 25 for the
+ palette's colours. The rule is asked for every pixel and answers with a number.
+
From the course "Shaders for pixel art" on spielerando.com.
SPDX-License-Identifier: MIT */
…
];
- /** How bright a colour looks, in thousandths: the usual weights of red, green and blue. */
- const brightness = (c: number[]) => 299 * c[0] + 587 * c[1] + 114 * c[2];
-
/** The number at a place of the picture; outside the picture nothing is. */
export function at(src: Frame, x: number, y: number): number {
…
}
- /** The rule, wrong: the colour mixed with the tint, then the palette's colour whose brightness is nearest. */
+ /** The rule: the colour mixed with the tint, then the palette's colour nearest to the mixture. */
export function shade(x: number, y: number, _tick: number, src: Frame, p: Params = DEFAULTS): number {
const n = at(src, x, y);
…
const c = RGB[n];
const t = RGB[p.tint];
+ // the colour mixed with the tint: amount parts of 100 of the tint, the rest of the colour, rounded down
const mixed = [0, 1, 2].map((k) => Math.floor((c[k] * (100 - p.amount) + t[k] * p.amount) / 100));
- // the nearest colour by brightness alone
+ // the nearest colour of the palette: the smallest sum of the squared differences in red, green and
+ // blue. Only a smaller sum wins, so on a tie the lower number stays.
let best = 1;
- let least = Number.MAX_SAFE_INTEGER;
+ let least = 195076; // more than any sum can be: 3 times 255 times 255, and 1
for (let i = 1; i <= 25; i++) {
- const away = Math.abs(brightness(mixed) - brightness(RGB[i]));
+ const d = [0, 1, 2].map((k) => mixed[k] - RGB[i][k]);
+ const away = d[0] * d[0] + d[1] * d[1] + d[2] * d[2];
if (away < least) {
least = away;What the test says about the mistake
✔ with no tint every colour stays itself
✔ with the whole tint every colour becomes the tint
✔ every answer is a colour of the palette, 1 to 25, and nothing stays nothing
✖ a fifth of amber on green is still green: 56, 138, 70 is nearest to green (17), not to a violet
got 22, expected 17
✖ the sky with 30 parts of violet becomes light blue (14), not pink
got 11, expected 14
✖ half of one colour and half of another lies exactly between them: the lower number wins, from either side
got 9, expected 3
6 tests: 3 passed, 3 failedTell your AI
Write a shader for my game that tints the whole picture towards one colour and keeps every pixel in my palette.
- My palette has [number] colours. Here they are as red, green and blue from 0 to 255: [the list]. Put them into the shader as a table of whole numbers.
- For every pixel: mix its colour with the tint, [30] parts of 100 of the tint and the rest of the pixel's colour, for red, green and blue each, rounded down. Then find the colour of my palette nearest to the mixture: the smallest sum of the squared differences in red, green and blue. On a tie, take the colour that comes first in the table. Draw that colour.
- Never compare by brightness alone, and never draw the mixture itself.
- How much of the tint, and which tint, must be settings I can change from the game's code.
Before you write the shader, write the same rule as a small plain function. It takes a grid of whole numbers (0 = empty, 1 to N = the colours of my palette) and a place, and returns the number for that one pixel. Test it: with no tint every colour stays itself; with the whole tint every colour becomes the tint; a green with a little of a warm colour in it is still a green; a mixture exactly halfway between two of my colours takes the one that comes first. Show me the test's output. Then write the shader so that it does exactly what the function does.
The code
Three times the same rule. The first is the one we test; the two shaders are that rule in a shader language, which is what your game runs.
The rule, tested: shader.ts
/* A tint that stays in the palette: every colour is mixed with one colour, the tint, in red, green
and blue, and the mixture is then put back to the palette's nearest colour. So a whole scene can take
on the gold of an evening, the blue of deep water or the red of danger, and every pixel is still one
of the palette's 25 colours. Everything is whole numbers: red, green and blue from 0 to 255, the
tint in parts of 100, and distances compared squared.
The pixel art way: a picture is a grid of whole numbers, 0 where nothing is and 1 to 25 for the
palette's colours. The rule is asked for every pixel and answers with a number.
From the course "Shaders for pixel art" on spielerando.com.
SPDX-License-Identifier: MIT */
export interface Frame {
width: number;
height: number;
index: Uint8Array;
}
export const DEFAULTS = {
/** How much of the tint is mixed in, in parts of 100: 0 is none, 100 is the tint alone. */
amount: 30,
/** The number of the tint's colour in the palette: 13 is deep blue. */
tint: 13,
};
export type Params = typeof DEFAULTS;
/** Red, green and blue of each colour of the palette, from 0 to 255. [0] is nothing. */
const RGB = [
[0, 0, 0],
[31, 33, 66], // 1: ink
[75, 78, 112], // 2: slate
[120, 123, 152], // 3: grey violet
[198, 199, 227], // 4: light slate
[255, 255, 255], // 5: white
[246, 241, 233], // 6: cream
[214, 205, 189], // 7: sand
[242, 193, 78], // 8: honey
[224, 138, 46], // 9: amber
[194, 65, 45], // 10: coral
[244, 163, 160], // 11: pink
[47, 98, 240], // 12: blue
[35, 83, 214], // 13: deep blue
[157, 184, 255], // 14: light blue
[234, 240, 255], // 15: palest blue
[207, 224, 255], // 16: sky
[15, 138, 76], // 17: green
[124, 207, 154], // 18: light green
[226, 244, 234], // 19: meadow
[138, 90, 60], // 20: wood
[92, 59, 40], // 21: dark wood
[109, 76, 230], // 22: violet
[201, 187, 255], // 23: light violet
[18, 20, 43], // 24: night
[39, 43, 82], // 25: night, a shade lighter
];
/** The number at a place of the picture; outside the picture nothing is. */
export function at(src: Frame, x: number, y: number): number {
if (x < 0 || y < 0 || x >= src.width || y >= src.height) return 0;
return src.index[y * src.width + x];
}
/** The rule: the colour mixed with the tint, then the palette's colour nearest to the mixture. */
export function shade(x: number, y: number, _tick: number, src: Frame, p: Params = DEFAULTS): number {
const n = at(src, x, y);
if (n === 0) return 0;
const c = RGB[n];
const t = RGB[p.tint];
// the colour mixed with the tint: amount parts of 100 of the tint, the rest of the colour, rounded down
const mixed = [0, 1, 2].map((k) => Math.floor((c[k] * (100 - p.amount) + t[k] * p.amount) / 100));
// the nearest colour of the palette: the smallest sum of the squared differences in red, green and
// blue. Only a smaller sum wins, so on a tie the lower number stays.
let best = 1;
let least = 195076; // more than any sum can be: 3 times 255 times 255, and 1
for (let i = 1; i <= 25; i++) {
const d = [0, 1, 2].map((k) => mixed[k] - RGB[i][k]);
const away = d[0] * d[0] + d[1] * d[1] + d[2] * d[2];
if (away < least) {
least = away;
best = i;
}
}
return best;
}
The same rule as a shader: GLSL
#version 300 es
// A shader the pixel art way: a pixel is a whole number, 0 where nothing is, 1 to 25 for the
// palette's colours. The rule is shade(): it is asked for every pixel and answers with a number.
// Everything outside the marked part is the same for every rule. Put your own palette in PALETTE.
precision highp float;
precision highp int;
uniform sampler2D SRC; // the picture, sampled without smoothing (nearest)
uniform sampler2D OTHER; // a second picture, where the rule uses one
uniform int TICK; // the time in ticks, 60 a second, counted by the game
const vec3 PALETTE[26] = vec3[26](
vec3(0.0),
vec3(31.0, 33.0, 66.0) / 255.0, // 1: k
vec3(75.0, 78.0, 112.0) / 255.0, // 2: n
vec3(120.0, 123.0, 152.0) / 255.0, // 3: m
vec3(198.0, 199.0, 227.0) / 255.0, // 4: e
vec3(255.0, 255.0, 255.0) / 255.0, // 5: w
vec3(246.0, 241.0, 233.0) / 255.0, // 6: c
vec3(214.0, 205.0, 189.0) / 255.0, // 7: d
vec3(242.0, 193.0, 78.0) / 255.0, // 8: y
vec3(224.0, 138.0, 46.0) / 255.0, // 9: o
vec3(194.0, 65.0, 45.0) / 255.0, // 10: r
vec3(244.0, 163.0, 160.0) / 255.0, // 11: p
vec3(47.0, 98.0, 240.0) / 255.0, // 12: b
vec3(35.0, 83.0, 214.0) / 255.0, // 13: u
vec3(157.0, 184.0, 255.0) / 255.0, // 14: s
vec3(234.0, 240.0, 255.0) / 255.0, // 15: a
vec3(207.0, 224.0, 255.0) / 255.0, // 16: i
vec3(15.0, 138.0, 76.0) / 255.0, // 17: g
vec3(124.0, 207.0, 154.0) / 255.0, // 18: l
vec3(226.0, 244.0, 234.0) / 255.0, // 19: f
vec3(138.0, 90.0, 60.0) / 255.0, // 20: t
vec3(92.0, 59.0, 40.0) / 255.0, // 21: h
vec3(109.0, 76.0, 230.0) / 255.0, // 22: v
vec3(201.0, 187.0, 255.0) / 255.0, // 23: q
vec3(18.0, 20.0, 43.0) / 255.0, // 24: x
vec3(39.0, 43.0, 82.0) / 255.0 // 25: z
);
// Which colour of the palette is this? 0 where nothing is drawn.
int number(vec4 c) {
if (c.a < 0.5) return 0;
int best = 1;
float least = 9.0;
for (int i = 1; i <= 25; i++) {
vec3 d = c.rgb - PALETTE[i];
float away = dot(d, d);
if (away < least) {
least = away;
best = i;
}
}
return best;
}
// The number at a place of the picture; outside the picture nothing is.
int at(ivec2 p) {
ivec2 size = textureSize(SRC, 0);
if (p.x < 0 || p.y < 0 || p.x >= size.x || p.y >= size.y) return 0;
return number(texelFetch(SRC, p, 0));
}
// The same for the second picture.
int otherAt(ivec2 p) {
ivec2 size = textureSize(OTHER, 0);
if (p.x < 0 || p.y < 0 || p.x >= size.x || p.y >= size.y) return 0;
return number(texelFetch(OTHER, p, 0));
}
// a wrapped into 0 .. n-1, also for a below zero (the % sign is not safe there)
int wrap(int a, int n) {
return a - n * int(floor(float(a) / float(n)));
}
// ---- the rule ----
uniform int amount; // how much of the tint is mixed in, in parts of 100: 0 is none, 100 is the tint alone
uniform int tint; // the number of the tint's colour in the palette: 13 is deep blue
// red, green and blue of each colour of the palette, from 0 to 255; [0] is nothing
const ivec3 RGB[26] = ivec3[26](
ivec3(0, 0, 0),
ivec3(31, 33, 66), // 1: ink
ivec3(75, 78, 112), // 2: slate
ivec3(120, 123, 152), // 3: grey violet
ivec3(198, 199, 227), // 4: light slate
ivec3(255, 255, 255), // 5: white
ivec3(246, 241, 233), // 6: cream
ivec3(214, 205, 189), // 7: sand
ivec3(242, 193, 78), // 8: honey
ivec3(224, 138, 46), // 9: amber
ivec3(194, 65, 45), // 10: coral
ivec3(244, 163, 160), // 11: pink
ivec3(47, 98, 240), // 12: blue
ivec3(35, 83, 214), // 13: deep blue
ivec3(157, 184, 255), // 14: light blue
ivec3(234, 240, 255), // 15: palest blue
ivec3(207, 224, 255), // 16: sky
ivec3(15, 138, 76), // 17: green
ivec3(124, 207, 154), // 18: light green
ivec3(226, 244, 234), // 19: meadow
ivec3(138, 90, 60), // 20: wood
ivec3(92, 59, 40), // 21: dark wood
ivec3(109, 76, 230), // 22: violet
ivec3(201, 187, 255), // 23: light violet
ivec3(18, 20, 43), // 24: night
ivec3(39, 43, 82) // 25: night, a shade lighter
);
// the colour mixed with the tint, then the palette's colour nearest to the mixture
int shade(ivec2 p) {
int n = at(p);
if (n == 0) return 0;
ivec3 c = RGB[n];
ivec3 t = RGB[tint];
// the colour mixed with the tint: amount parts of 100 of the tint, the rest of the colour, rounded down
ivec3 mixed = (c * (100 - amount) + t * amount) / 100;
// the nearest colour of the palette: the smallest sum of the squared differences in red, green and
// blue. Only a smaller sum wins, so on a tie the lower number stays.
int best = 1;
int least = 195076; // more than any sum can be: 3 times 255 times 255, and 1
for (int i = 1; i <= 25; i++) {
ivec3 d = mixed - RGB[i];
int away = d.x * d.x + d.y * d.y + d.z * d.z;
if (away < least) {
least = away;
best = i;
}
}
return best;
}
// ---- end of the rule ----
out vec4 FRAGMENT;
void main() {
// the pixel's place, counted from the top left as in the picture
ivec2 p = ivec2(int(gl_FragCoord.x), textureSize(SRC, 0).y - 1 - int(gl_FragCoord.y));
int n = shade(p);
FRAGMENT = n == 0 ? vec4(0.0) : vec4(PALETTE[n], 1.0);
}
The same rule for Godot 4
shader_type canvas_item;
// A shader the pixel art way: a pixel is a whole number, 0 where nothing is, 1 to 25 for the
// palette's colours. The rule is shade(): it is asked for every pixel and answers with a number.
// Everything outside the marked part is the same for every rule. Put your own palette in PALETTE.
//
// Put this on a ColorRect that covers the screen, above everything it should change.
// The rule counts pixels as this shader sees them. Let the game draw at the size of its art and
// enlarge the finished picture (the project's stretch mode "viewport"), or it counts window pixels.
uniform sampler2D SRC : hint_screen_texture, filter_nearest; // what is on the screen behind it
uniform sampler2D OTHER : filter_nearest; // a second picture, where the rule uses one
// The time in ticks, 60 a second. The game counts it itself: add 1 in the _physics_process of a
// node that pauses with the game, and set it there, so a paused game pauses its shaders:
// tick += 1
// material.set_shader_parameter("TICK", tick)
uniform int TICK;
const vec3 PALETTE[26] = vec3[26](
vec3(0.0),
vec3(31.0, 33.0, 66.0) / 255.0, // 1: k
vec3(75.0, 78.0, 112.0) / 255.0, // 2: n
vec3(120.0, 123.0, 152.0) / 255.0, // 3: m
vec3(198.0, 199.0, 227.0) / 255.0, // 4: e
vec3(255.0, 255.0, 255.0) / 255.0, // 5: w
vec3(246.0, 241.0, 233.0) / 255.0, // 6: c
vec3(214.0, 205.0, 189.0) / 255.0, // 7: d
vec3(242.0, 193.0, 78.0) / 255.0, // 8: y
vec3(224.0, 138.0, 46.0) / 255.0, // 9: o
vec3(194.0, 65.0, 45.0) / 255.0, // 10: r
vec3(244.0, 163.0, 160.0) / 255.0, // 11: p
vec3(47.0, 98.0, 240.0) / 255.0, // 12: b
vec3(35.0, 83.0, 214.0) / 255.0, // 13: u
vec3(157.0, 184.0, 255.0) / 255.0, // 14: s
vec3(234.0, 240.0, 255.0) / 255.0, // 15: a
vec3(207.0, 224.0, 255.0) / 255.0, // 16: i
vec3(15.0, 138.0, 76.0) / 255.0, // 17: g
vec3(124.0, 207.0, 154.0) / 255.0, // 18: l
vec3(226.0, 244.0, 234.0) / 255.0, // 19: f
vec3(138.0, 90.0, 60.0) / 255.0, // 20: t
vec3(92.0, 59.0, 40.0) / 255.0, // 21: h
vec3(109.0, 76.0, 230.0) / 255.0, // 22: v
vec3(201.0, 187.0, 255.0) / 255.0, // 23: q
vec3(18.0, 20.0, 43.0) / 255.0, // 24: x
vec3(39.0, 43.0, 82.0) / 255.0 // 25: z
);
// Which colour of the palette is this? 0 where nothing is drawn.
int number(vec4 c) {
if (c.a < 0.5) return 0;
int best = 1;
float least = 9.0;
for (int i = 1; i <= 25; i++) {
vec3 d = c.rgb - PALETTE[i];
float away = dot(d, d);
if (away < least) {
least = away;
best = i;
}
}
return best;
}
// The number at a place of the picture; outside the picture nothing is.
int at(ivec2 p) {
ivec2 size = textureSize(SRC, 0);
if (p.x < 0 || p.y < 0 || p.x >= size.x || p.y >= size.y) return 0;
return number(texelFetch(SRC, p, 0));
}
// The same for the second picture.
int otherAt(ivec2 p) {
ivec2 size = textureSize(OTHER, 0);
if (p.x < 0 || p.y < 0 || p.x >= size.x || p.y >= size.y) return 0;
return number(texelFetch(OTHER, p, 0));
}
// a wrapped into 0 .. n-1, also for a below zero (the % sign is not safe there)
int wrap(int a, int n) {
return a - n * int(floor(float(a) / float(n)));
}
// ---- the rule ----
uniform int amount; // how much of the tint is mixed in, in parts of 100: 0 is none, 100 is the tint alone
uniform int tint; // the number of the tint's colour in the palette: 13 is deep blue
// red, green and blue of each colour of the palette, from 0 to 255; [0] is nothing
const ivec3 RGB[26] = ivec3[26](
ivec3(0, 0, 0),
ivec3(31, 33, 66), // 1: ink
ivec3(75, 78, 112), // 2: slate
ivec3(120, 123, 152), // 3: grey violet
ivec3(198, 199, 227), // 4: light slate
ivec3(255, 255, 255), // 5: white
ivec3(246, 241, 233), // 6: cream
ivec3(214, 205, 189), // 7: sand
ivec3(242, 193, 78), // 8: honey
ivec3(224, 138, 46), // 9: amber
ivec3(194, 65, 45), // 10: coral
ivec3(244, 163, 160), // 11: pink
ivec3(47, 98, 240), // 12: blue
ivec3(35, 83, 214), // 13: deep blue
ivec3(157, 184, 255), // 14: light blue
ivec3(234, 240, 255), // 15: palest blue
ivec3(207, 224, 255), // 16: sky
ivec3(15, 138, 76), // 17: green
ivec3(124, 207, 154), // 18: light green
ivec3(226, 244, 234), // 19: meadow
ivec3(138, 90, 60), // 20: wood
ivec3(92, 59, 40), // 21: dark wood
ivec3(109, 76, 230), // 22: violet
ivec3(201, 187, 255), // 23: light violet
ivec3(18, 20, 43), // 24: night
ivec3(39, 43, 82) // 25: night, a shade lighter
);
// the colour mixed with the tint, then the palette's colour nearest to the mixture
int shade(ivec2 p) {
int n = at(p);
if (n == 0) return 0;
ivec3 c = RGB[n];
ivec3 t = RGB[tint];
// the colour mixed with the tint: amount parts of 100 of the tint, the rest of the colour, rounded down
ivec3 mixed = (c * (100 - amount) + t * amount) / 100;
// the nearest colour of the palette: the smallest sum of the squared differences in red, green and
// blue. Only a smaller sum wins, so on a tie the lower number stays.
int best = 1;
int least = 195076; // more than any sum can be: 3 times 255 times 255, and 1
for (int i = 1; i <= 25; i++) {
ivec3 d = mixed - RGB[i];
int away = d.x * d.x + d.y * d.y + d.z * d.z;
if (away < least) {
least = away;
best = i;
}
}
return best;
}
// ---- end of the rule ----
void fragment() {
ivec2 p = ivec2(SCREEN_UV * vec2(textureSize(SRC, 0)));
int n = shade(p);
COLOR = n == 0 ? vec4(0.0) : vec4(PALETTE[n], 1.0);
}
The test: shader.test.ts
import assert from "node:assert/strict";
import { test } from "node:test";
import { type Frame, shade } from "./shader.ts";
/** A tiny picture from rows of digits: 0 is nothing, 1 to 9 are colours. */
function picture(rows: string[]): Frame {
return { width: rows[0].length, height: rows.length, index: Uint8Array.from(rows.join(""), Number) };
}
/** A picture of one pixel, for the colours from 10 to 25 that a digit cannot write. */
const one = (n: number): Frame => ({ width: 1, height: 1, index: Uint8Array.of(n) });
/** What colour `n` becomes with `amount` parts of 100 of the tint mixed in. */
const tinted = (n: number, amount: number, tint: number) => shade(0, 0, 0, one(n), { amount, tint });
const EVERY = Array.from({ length: 25 }, (_, i) => i + 1);
test("with no tint every colour stays itself", () => {
for (const n of EVERY) assert.equal(tinted(n, 0, 9), n, `colour ${n}`);
});
test("with the whole tint every colour becomes the tint", () => {
for (const n of EVERY) assert.equal(tinted(n, 100, 22), 22, `colour ${n}`);
});
test("every answer is a colour of the palette, 1 to 25, and nothing stays nothing", () => {
for (const tint of EVERY) {
for (let amount = 0; amount <= 100; amount += 10) {
for (const n of EVERY) {
const answer = tinted(n, amount, tint);
assert.ok(Number.isInteger(answer) && answer >= 1 && answer <= 25, `colour ${n} with ${amount} of tint ${tint} gave ${answer}`);
}
}
}
const hive = picture(["0880", "8998"]);
assert.equal(shade(0, 0, 0, hive, { amount: 50, tint: 9 }), 0);
});
test("a fifth of amber on green is still green: 56, 138, 70 is nearest to green (17), not to a violet", () => {
// green is 15, 138, 76 and amber 224, 138, 46; 80 parts of the one and 20 of the other give 56, 138, 70
assert.equal(tinted(17, 20, 9), 17);
});
test("the sky with 30 parts of violet becomes light blue (14), not pink", () => {
// sky is 207, 224, 255 and violet 109, 76, 230: the mixture 177, 179, 247 lies nearest to light blue
assert.equal(tinted(16, 30, 22), 14);
});
test("half of one colour and half of another lies exactly between them: the lower number wins, from either side", () => {
// grey violet (3) is 120, 123, 152 and pink (11) 244, 163, 160; half and half is 182, 143, 156,
// which is 4260 away from both, squared
assert.equal(tinted(3, 50, 11), 3);
assert.equal(tinted(11, 50, 3), 3);
});
- 6 tests pass on the rule.
- With the mistake put in, 3 of them fail: "a fifth of amber on green is still green: 56, 138, 70 is nearest to green (17), not to a violet", "the sky with 30 parts of violet becomes light blue (14), not pink", "half of one colour and half of another lies exactly between them: the lower number wins, from either side".
- The mistake shows in the picture above: switch to "With the mistake".
- The shader ran in a browser (Chrome/158.0.8127.37) on 12 pictures and gave the rule's picture in 388,800 of 388,800 pixels.
Try it
- Keep the tint at 13 and move "how much of the tint" slowly from 0 to 100. The tree on the dune stays green long after the red stripes of the hut on the left have turned brown, and the honey kiosk turns pink for a moment and then grey violet. The palette decides where a colour can go: it has no honey cooled by blue.
- Set the tint to 10, coral, for the red of danger, or to 20, wood, for an old memory. With wood, the sky turns to sand and then grey, like an old photograph.
- Switch to "With the mistake", stop the picture at a few moments and compare it with "With the rule". Look at the sea and the sand: with the rule they cool down together; with the mistake they jump to other colours.
Read up
Where to go deeper: the pages of ours this lesson builds on, and what others have written about its point.
Day and Night Shader for Pixel Art: Darker by the Hour
A whole scene that changes its colours without mixing at all: night comes by stepping every colour down to the palette's darker neighbours, in dithered steps, while lamps and windows stay lit.
A tint without a palette, for comparison: the light of the hour laid over the whole world, with colours mixed in a space built around the eye, so that dawn stays clean instead of turning grey.
Why the plain sum of squared differences in red, green and blue is not quite how the eye compares colours, and a cheap weighted sum, written for picking a palette's nearest colour, whose code stays in whole numbers.
Check yourself: three questions
Each answer is checked and explained at once. The questions are part of the quiz, so they count for your Game Coder IQ like any other.
A tint mixes every colour of a scene with one colour. What does this lesson's rule do with the mixture before it answers?
Next lesson Is It Right? Testing a Pixel Art Shader, Pixel for Pixel
Give the whole lesson to your AI
The button copies what to tell it together with the tested rule, its test and both shaders, so your AI has everything to build this in your game and to show you that it works.
This is a lesson from the course "Shaders for Pixel Art" on spielerando.com: https://spielerando.com/en/courses/pixel-shaders/only-the-palette
Write a shader for my game that tints the whole picture towards one colour and keeps every pixel in my palette.
- My palette has [number] colours. Here they are as red, green and blue from 0 to 255: [the list]. Put them into the shader as a table of whole numbers.
- For every pixel: mix its colour with the tint, [30] parts of 100 of the tint and the rest of the pixel's colour, for red, green and blue each, rounded down. Then find the colour of my palette nearest to the mixture: the smallest sum of the squared differences in red, green and blue. On a tie, take the colour that comes first in the table. Draw that colour.
- Never compare by brightness alone, and never draw the mixture itself.
- How much of the tint, and which tint, must be settings I can change from the game's code.
Before you write the shader, write the same rule as a small plain function. It takes a grid of whole numbers (0 = empty, 1 to N = the colours of my palette) and a place, and returns the number for that one pixel. Test it: with no tint every colour stays itself; with the whole tint every colour becomes the tint; a green with a little of a warm colour in it is still a green; a mixture exactly halfway between two of my colours takes the one that comes first. Show me the test's output. Then write the shader so that it does exactly what the function does.
The rule, tested (shader.ts):
```ts
/* A tint that stays in the palette: every colour is mixed with one colour, the tint, in red, green
and blue, and the mixture is then put back to the palette's nearest colour. So a whole scene can take
on the gold of an evening, the blue of deep water or the red of danger, and every pixel is still one
of the palette's 25 colours. Everything is whole numbers: red, green and blue from 0 to 255, the
tint in parts of 100, and distances compared squared.
The pixel art way: a picture is a grid of whole numbers, 0 where nothing is and 1 to 25 for the
palette's colours. The rule is asked for every pixel and answers with a number.
From the course "Shaders for pixel art" on spielerando.com.
SPDX-License-Identifier: MIT */
export interface Frame {
width: number;
height: number;
index: Uint8Array;
}
export const DEFAULTS = {
/** How much of the tint is mixed in, in parts of 100: 0 is none, 100 is the tint alone. */
amount: 30,
/** The number of the tint's colour in the palette: 13 is deep blue. */
tint: 13,
};
export type Params = typeof DEFAULTS;
/** Red, green and blue of each colour of the palette, from 0 to 255. [0] is nothing. */
const RGB = [
[0, 0, 0],
[31, 33, 66], // 1: ink
[75, 78, 112], // 2: slate
[120, 123, 152], // 3: grey violet
[198, 199, 227], // 4: light slate
[255, 255, 255], // 5: white
[246, 241, 233], // 6: cream
[214, 205, 189], // 7: sand
[242, 193, 78], // 8: honey
[224, 138, 46], // 9: amber
[194, 65, 45], // 10: coral
[244, 163, 160], // 11: pink
[47, 98, 240], // 12: blue
[35, 83, 214], // 13: deep blue
[157, 184, 255], // 14: light blue
[234, 240, 255], // 15: palest blue
[207, 224, 255], // 16: sky
[15, 138, 76], // 17: green
[124, 207, 154], // 18: light green
[226, 244, 234], // 19: meadow
[138, 90, 60], // 20: wood
[92, 59, 40], // 21: dark wood
[109, 76, 230], // 22: violet
[201, 187, 255], // 23: light violet
[18, 20, 43], // 24: night
[39, 43, 82], // 25: night, a shade lighter
];
/** The number at a place of the picture; outside the picture nothing is. */
export function at(src: Frame, x: number, y: number): number {
if (x < 0 || y < 0 || x >= src.width || y >= src.height) return 0;
return src.index[y * src.width + x];
}
/** The rule: the colour mixed with the tint, then the palette's colour nearest to the mixture. */
export function shade(x: number, y: number, _tick: number, src: Frame, p: Params = DEFAULTS): number {
const n = at(src, x, y);
if (n === 0) return 0;
const c = RGB[n];
const t = RGB[p.tint];
// the colour mixed with the tint: amount parts of 100 of the tint, the rest of the colour, rounded down
const mixed = [0, 1, 2].map((k) => Math.floor((c[k] * (100 - p.amount) + t[k] * p.amount) / 100));
// the nearest colour of the palette: the smallest sum of the squared differences in red, green and
// blue. Only a smaller sum wins, so on a tie the lower number stays.
let best = 1;
let least = 195076; // more than any sum can be: 3 times 255 times 255, and 1
for (let i = 1; i <= 25; i++) {
const d = [0, 1, 2].map((k) => mixed[k] - RGB[i][k]);
const away = d[0] * d[0] + d[1] * d[1] + d[2] * d[2];
if (away < least) {
least = away;
best = i;
}
}
return best;
}
```
Its test (shader.test.ts):
```ts
import assert from "node:assert/strict";
import { test } from "node:test";
import { type Frame, shade } from "./shader.ts";
/** A tiny picture from rows of digits: 0 is nothing, 1 to 9 are colours. */
function picture(rows: string[]): Frame {
return { width: rows[0].length, height: rows.length, index: Uint8Array.from(rows.join(""), Number) };
}
/** A picture of one pixel, for the colours from 10 to 25 that a digit cannot write. */
const one = (n: number): Frame => ({ width: 1, height: 1, index: Uint8Array.of(n) });
/** What colour `n` becomes with `amount` parts of 100 of the tint mixed in. */
const tinted = (n: number, amount: number, tint: number) => shade(0, 0, 0, one(n), { amount, tint });
const EVERY = Array.from({ length: 25 }, (_, i) => i + 1);
test("with no tint every colour stays itself", () => {
for (const n of EVERY) assert.equal(tinted(n, 0, 9), n, `colour ${n}`);
});
test("with the whole tint every colour becomes the tint", () => {
for (const n of EVERY) assert.equal(tinted(n, 100, 22), 22, `colour ${n}`);
});
test("every answer is a colour of the palette, 1 to 25, and nothing stays nothing", () => {
for (const tint of EVERY) {
for (let amount = 0; amount <= 100; amount += 10) {
for (const n of EVERY) {
const answer = tinted(n, amount, tint);
assert.ok(Number.isInteger(answer) && answer >= 1 && answer <= 25, `colour ${n} with ${amount} of tint ${tint} gave ${answer}`);
}
}
}
const hive = picture(["0880", "8998"]);
assert.equal(shade(0, 0, 0, hive, { amount: 50, tint: 9 }), 0);
});
test("a fifth of amber on green is still green: 56, 138, 70 is nearest to green (17), not to a violet", () => {
// green is 15, 138, 76 and amber 224, 138, 46; 80 parts of the one and 20 of the other give 56, 138, 70
assert.equal(tinted(17, 20, 9), 17);
});
test("the sky with 30 parts of violet becomes light blue (14), not pink", () => {
// sky is 207, 224, 255 and violet 109, 76, 230: the mixture 177, 179, 247 lies nearest to light blue
assert.equal(tinted(16, 30, 22), 14);
});
test("half of one colour and half of another lies exactly between them: the lower number wins, from either side", () => {
// grey violet (3) is 120, 123, 152 and pink (11) 244, 163, 160; half and half is 182, 143, 156,
// which is 4260 away from both, squared
assert.equal(tinted(3, 50, 11), 3);
assert.equal(tinted(11, 50, 3), 3);
});
```
The same rule as a GLSL shader (WebGL 2, OpenGL ES 3.0), checked against the rule pixel for pixel:
```glsl
#version 300 es
// A shader the pixel art way: a pixel is a whole number, 0 where nothing is, 1 to 25 for the
// palette's colours. The rule is shade(): it is asked for every pixel and answers with a number.
// Everything outside the marked part is the same for every rule. Put your own palette in PALETTE.
precision highp float;
precision highp int;
uniform sampler2D SRC; // the picture, sampled without smoothing (nearest)
uniform sampler2D OTHER; // a second picture, where the rule uses one
uniform int TICK; // the time in ticks, 60 a second, counted by the game
const vec3 PALETTE[26] = vec3[26](
vec3(0.0),
vec3(31.0, 33.0, 66.0) / 255.0, // 1: k
vec3(75.0, 78.0, 112.0) / 255.0, // 2: n
vec3(120.0, 123.0, 152.0) / 255.0, // 3: m
vec3(198.0, 199.0, 227.0) / 255.0, // 4: e
vec3(255.0, 255.0, 255.0) / 255.0, // 5: w
vec3(246.0, 241.0, 233.0) / 255.0, // 6: c
vec3(214.0, 205.0, 189.0) / 255.0, // 7: d
vec3(242.0, 193.0, 78.0) / 255.0, // 8: y
vec3(224.0, 138.0, 46.0) / 255.0, // 9: o
vec3(194.0, 65.0, 45.0) / 255.0, // 10: r
vec3(244.0, 163.0, 160.0) / 255.0, // 11: p
vec3(47.0, 98.0, 240.0) / 255.0, // 12: b
vec3(35.0, 83.0, 214.0) / 255.0, // 13: u
vec3(157.0, 184.0, 255.0) / 255.0, // 14: s
vec3(234.0, 240.0, 255.0) / 255.0, // 15: a
vec3(207.0, 224.0, 255.0) / 255.0, // 16: i
vec3(15.0, 138.0, 76.0) / 255.0, // 17: g
vec3(124.0, 207.0, 154.0) / 255.0, // 18: l
vec3(226.0, 244.0, 234.0) / 255.0, // 19: f
vec3(138.0, 90.0, 60.0) / 255.0, // 20: t
vec3(92.0, 59.0, 40.0) / 255.0, // 21: h
vec3(109.0, 76.0, 230.0) / 255.0, // 22: v
vec3(201.0, 187.0, 255.0) / 255.0, // 23: q
vec3(18.0, 20.0, 43.0) / 255.0, // 24: x
vec3(39.0, 43.0, 82.0) / 255.0 // 25: z
);
// Which colour of the palette is this? 0 where nothing is drawn.
int number(vec4 c) {
if (c.a < 0.5) return 0;
int best = 1;
float least = 9.0;
for (int i = 1; i <= 25; i++) {
vec3 d = c.rgb - PALETTE[i];
float away = dot(d, d);
if (away < least) {
least = away;
best = i;
}
}
return best;
}
// The number at a place of the picture; outside the picture nothing is.
int at(ivec2 p) {
ivec2 size = textureSize(SRC, 0);
if (p.x < 0 || p.y < 0 || p.x >= size.x || p.y >= size.y) return 0;
return number(texelFetch(SRC, p, 0));
}
// The same for the second picture.
int otherAt(ivec2 p) {
ivec2 size = textureSize(OTHER, 0);
if (p.x < 0 || p.y < 0 || p.x >= size.x || p.y >= size.y) return 0;
return number(texelFetch(OTHER, p, 0));
}
// a wrapped into 0 .. n-1, also for a below zero (the % sign is not safe there)
int wrap(int a, int n) {
return a - n * int(floor(float(a) / float(n)));
}
// ---- the rule ----
uniform int amount; // how much of the tint is mixed in, in parts of 100: 0 is none, 100 is the tint alone
uniform int tint; // the number of the tint's colour in the palette: 13 is deep blue
// red, green and blue of each colour of the palette, from 0 to 255; [0] is nothing
const ivec3 RGB[26] = ivec3[26](
ivec3(0, 0, 0),
ivec3(31, 33, 66), // 1: ink
ivec3(75, 78, 112), // 2: slate
ivec3(120, 123, 152), // 3: grey violet
ivec3(198, 199, 227), // 4: light slate
ivec3(255, 255, 255), // 5: white
ivec3(246, 241, 233), // 6: cream
ivec3(214, 205, 189), // 7: sand
ivec3(242, 193, 78), // 8: honey
ivec3(224, 138, 46), // 9: amber
ivec3(194, 65, 45), // 10: coral
ivec3(244, 163, 160), // 11: pink
ivec3(47, 98, 240), // 12: blue
ivec3(35, 83, 214), // 13: deep blue
ivec3(157, 184, 255), // 14: light blue
ivec3(234, 240, 255), // 15: palest blue
ivec3(207, 224, 255), // 16: sky
ivec3(15, 138, 76), // 17: green
ivec3(124, 207, 154), // 18: light green
ivec3(226, 244, 234), // 19: meadow
ivec3(138, 90, 60), // 20: wood
ivec3(92, 59, 40), // 21: dark wood
ivec3(109, 76, 230), // 22: violet
ivec3(201, 187, 255), // 23: light violet
ivec3(18, 20, 43), // 24: night
ivec3(39, 43, 82) // 25: night, a shade lighter
);
// the colour mixed with the tint, then the palette's colour nearest to the mixture
int shade(ivec2 p) {
int n = at(p);
if (n == 0) return 0;
ivec3 c = RGB[n];
ivec3 t = RGB[tint];
// the colour mixed with the tint: amount parts of 100 of the tint, the rest of the colour, rounded down
ivec3 mixed = (c * (100 - amount) + t * amount) / 100;
// the nearest colour of the palette: the smallest sum of the squared differences in red, green and
// blue. Only a smaller sum wins, so on a tie the lower number stays.
int best = 1;
int least = 195076; // more than any sum can be: 3 times 255 times 255, and 1
for (int i = 1; i <= 25; i++) {
ivec3 d = mixed - RGB[i];
int away = d.x * d.x + d.y * d.y + d.z * d.z;
if (away < least) {
least = away;
best = i;
}
}
return best;
}
// ---- end of the rule ----
out vec4 FRAGMENT;
void main() {
// the pixel's place, counted from the top left as in the picture
ivec2 p = ivec2(int(gl_FragCoord.x), textureSize(SRC, 0).y - 1 - int(gl_FragCoord.y));
int n = shade(p);
FRAGMENT = n == 0 ? vec4(0.0) : vec4(PALETTE[n], 1.0);
}
```
The same rule for Godot 4 (not run by the authors):
```gdshader
shader_type canvas_item;
// A shader the pixel art way: a pixel is a whole number, 0 where nothing is, 1 to 25 for the
// palette's colours. The rule is shade(): it is asked for every pixel and answers with a number.
// Everything outside the marked part is the same for every rule. Put your own palette in PALETTE.
//
// Put this on a ColorRect that covers the screen, above everything it should change.
// The rule counts pixels as this shader sees them. Let the game draw at the size of its art and
// enlarge the finished picture (the project's stretch mode "viewport"), or it counts window pixels.
uniform sampler2D SRC : hint_screen_texture, filter_nearest; // what is on the screen behind it
uniform sampler2D OTHER : filter_nearest; // a second picture, where the rule uses one
// The time in ticks, 60 a second. The game counts it itself: add 1 in the _physics_process of a
// node that pauses with the game, and set it there, so a paused game pauses its shaders:
// tick += 1
// material.set_shader_parameter("TICK", tick)
uniform int TICK;
const vec3 PALETTE[26] = vec3[26](
vec3(0.0),
vec3(31.0, 33.0, 66.0) / 255.0, // 1: k
vec3(75.0, 78.0, 112.0) / 255.0, // 2: n
vec3(120.0, 123.0, 152.0) / 255.0, // 3: m
vec3(198.0, 199.0, 227.0) / 255.0, // 4: e
vec3(255.0, 255.0, 255.0) / 255.0, // 5: w
vec3(246.0, 241.0, 233.0) / 255.0, // 6: c
vec3(214.0, 205.0, 189.0) / 255.0, // 7: d
vec3(242.0, 193.0, 78.0) / 255.0, // 8: y
vec3(224.0, 138.0, 46.0) / 255.0, // 9: o
vec3(194.0, 65.0, 45.0) / 255.0, // 10: r
vec3(244.0, 163.0, 160.0) / 255.0, // 11: p
vec3(47.0, 98.0, 240.0) / 255.0, // 12: b
vec3(35.0, 83.0, 214.0) / 255.0, // 13: u
vec3(157.0, 184.0, 255.0) / 255.0, // 14: s
vec3(234.0, 240.0, 255.0) / 255.0, // 15: a
vec3(207.0, 224.0, 255.0) / 255.0, // 16: i
vec3(15.0, 138.0, 76.0) / 255.0, // 17: g
vec3(124.0, 207.0, 154.0) / 255.0, // 18: l
vec3(226.0, 244.0, 234.0) / 255.0, // 19: f
vec3(138.0, 90.0, 60.0) / 255.0, // 20: t
vec3(92.0, 59.0, 40.0) / 255.0, // 21: h
vec3(109.0, 76.0, 230.0) / 255.0, // 22: v
vec3(201.0, 187.0, 255.0) / 255.0, // 23: q
vec3(18.0, 20.0, 43.0) / 255.0, // 24: x
vec3(39.0, 43.0, 82.0) / 255.0 // 25: z
);
// Which colour of the palette is this? 0 where nothing is drawn.
int number(vec4 c) {
if (c.a < 0.5) return 0;
int best = 1;
float least = 9.0;
for (int i = 1; i <= 25; i++) {
vec3 d = c.rgb - PALETTE[i];
float away = dot(d, d);
if (away < least) {
least = away;
best = i;
}
}
return best;
}
// The number at a place of the picture; outside the picture nothing is.
int at(ivec2 p) {
ivec2 size = textureSize(SRC, 0);
if (p.x < 0 || p.y < 0 || p.x >= size.x || p.y >= size.y) return 0;
return number(texelFetch(SRC, p, 0));
}
// The same for the second picture.
int otherAt(ivec2 p) {
ivec2 size = textureSize(OTHER, 0);
if (p.x < 0 || p.y < 0 || p.x >= size.x || p.y >= size.y) return 0;
return number(texelFetch(OTHER, p, 0));
}
// a wrapped into 0 .. n-1, also for a below zero (the % sign is not safe there)
int wrap(int a, int n) {
return a - n * int(floor(float(a) / float(n)));
}
// ---- the rule ----
uniform int amount; // how much of the tint is mixed in, in parts of 100: 0 is none, 100 is the tint alone
uniform int tint; // the number of the tint's colour in the palette: 13 is deep blue
// red, green and blue of each colour of the palette, from 0 to 255; [0] is nothing
const ivec3 RGB[26] = ivec3[26](
ivec3(0, 0, 0),
ivec3(31, 33, 66), // 1: ink
ivec3(75, 78, 112), // 2: slate
ivec3(120, 123, 152), // 3: grey violet
ivec3(198, 199, 227), // 4: light slate
ivec3(255, 255, 255), // 5: white
ivec3(246, 241, 233), // 6: cream
ivec3(214, 205, 189), // 7: sand
ivec3(242, 193, 78), // 8: honey
ivec3(224, 138, 46), // 9: amber
ivec3(194, 65, 45), // 10: coral
ivec3(244, 163, 160), // 11: pink
ivec3(47, 98, 240), // 12: blue
ivec3(35, 83, 214), // 13: deep blue
ivec3(157, 184, 255), // 14: light blue
ivec3(234, 240, 255), // 15: palest blue
ivec3(207, 224, 255), // 16: sky
ivec3(15, 138, 76), // 17: green
ivec3(124, 207, 154), // 18: light green
ivec3(226, 244, 234), // 19: meadow
ivec3(138, 90, 60), // 20: wood
ivec3(92, 59, 40), // 21: dark wood
ivec3(109, 76, 230), // 22: violet
ivec3(201, 187, 255), // 23: light violet
ivec3(18, 20, 43), // 24: night
ivec3(39, 43, 82) // 25: night, a shade lighter
);
// the colour mixed with the tint, then the palette's colour nearest to the mixture
int shade(ivec2 p) {
int n = at(p);
if (n == 0) return 0;
ivec3 c = RGB[n];
ivec3 t = RGB[tint];
// the colour mixed with the tint: amount parts of 100 of the tint, the rest of the colour, rounded down
ivec3 mixed = (c * (100 - amount) + t * amount) / 100;
// the nearest colour of the palette: the smallest sum of the squared differences in red, green and
// blue. Only a smaller sum wins, so on a tie the lower number stays.
int best = 1;
int least = 195076; // more than any sum can be: 3 times 255 times 255, and 1
for (int i = 1; i <= 25; i++) {
ivec3 d = mixed - RGB[i];
int away = d.x * d.x + d.y * d.y + d.z * d.z;
if (away < least) {
least = away;
best = i;
}
}
return best;
}
// ---- end of the rule ----
void fragment() {
ivec2 p = ivec2(SCREEN_UV * vec2(textureSize(SRC, 0)));
int n = shade(p);
COLOR = n == 0 ? vec4(0.0) : vec4(PALETTE[n], 1.0);
}
```
How useful was this page?
Nobody has rated this page yet.