Spielerando

Lesson 12 of 24Light with few colours8 minFree

Darker and Lighter in Pixel Art: Colour Ramps in a Shader

A colour ramp is a row of colours from dark to light, and in pixel art you shade by stepping along it: honey in the shade is amber, not a dimmer honey. This lesson writes every colour's darker and lighter neighbour into two small tables, shows how to make them for your own palette, and steps through them in a shader.

A bee garden in pixel art seen from above: five painted hives along a fence, a sand path, a bed of flowers, rows of lavender, a wooden deck with crates and a blanket, stone slabs and a pond. Bees fly over it. An oval patch drifts across the garden; every colour inside it is a few shades darker, and later a few shades lighter.

The whole garden is given to the rule. Inside the patch every colour steps to the neighbour its table names: the pale lawn to light green, honey to amber, the pond's blue to deep blue. Below 0 steps the patch is a cloud's shadow, above 0 a patch of sun.

Also in this lesson 3 things to read up on 3 questions to check yourself

The idea

A palette has nothing between its colours. Darken by turning red, green and blue down, and you get muddy colours that are not in the palette. A pixel artist shades by hand instead: for each colour they pick the colour of the palette that looks like it in shade. Write those picks down, follow them from one to the next, and you have ramps: honey, amber, wood, dark wood, night. White, palest blue, sky, light blue, blue.

Players see ramps everywhere: the shadow of a cloud passing over a field, the inside of a barn, a tile that lights up under the cursor. The lanterns and the night of the next lessons use the same two tables.

In the picture a patch drifts over a bee garden seen from above: a cloud's shadow below 0 steps, a patch of sun above 0.

How it works

The rule has two tables with one entry for every colour number. DARKER names the darker neighbour, LIGHTER the lighter one. For honey (8), DARKER says amber (9); for amber, wood (20); for wood, dark wood (21); for dark wood, night (24). Night's darker neighbour is night itself, and white's lighter neighbour is white: those are the two ends.

For every pixel the rule asks whether it lies inside the patch, an oval twice as wide as it is tall, measured with squares. Outside, the colour stays; inside, the rule looks it up in a table once for every step.

The sliders: "steps along the ramp" goes from -4 to 4, and starts at -2. The other two move the patch across and down the garden.

Making the tables for your own palette. Lay your colours out side by side. For each one ask: if this stood in shade, which of my colours would it be? Pick by eye, from the same family where you can; where a family has no darker colour, take a cooler one, as shade is. Here green goes to the dark night blue. Then do the same towards the light. Two checks keep the tables honest, and the tests run both: every step down must really be darker, by the brightness the eye sees (red, green and blue weighed 2126, 7152 and 722), and following the table must end at the darkest colour, never in a circle. With these tables, seven steps down make every colour night, and five steps up make every colour white.

What goes wrong

Asked for "one shade darker" in a palette shader, an AI often takes one off the colour's number, as if the palette ran from dark to light. Palettes are rarely sorted that way. This one runs by family: greys, warm colours, blues, greens, wood, violets, night. Honey is 8, and 7 is sand, which is lighter. The test that weighs every colour's brightness lists 13 of the 25 colours whose step down is not darker.

Switch the picture to "With the mistake". In the cloud's shadow the path turns cream and then white, the pond's blue pink and then coral, and the honey hives sand and then cream. In the patch of sun the lawn turns brown. The lawn in the shade even looks right at first, because its three greens happen to stand in the palette's order (19, 18, 17). At three steps it turns sky blue. That is why the mistake slips past a quick look.

Three tests catch it: "one step darker is the colour the table names, not the next lower number", "every step darker looks darker, and every step lighter looks lighter", and "seven steps down make every colour night, and five steps up make every colour white".

The mistake, line by line

Lines with - are the rule with the mistake, lines with + the rule as it should be.

- /* Colour ramps, with the mistake: the rule counts the palette's number down for darker and up for
-    lighter, as if the numbers ran from dark to light. They do not: they are the palette's order, and
-    the number below honey is sand, which is lighter. So the cloud's shadow comes out in wrong colours,
-    some of them lighter than what was there.
+ /* Colour ramps: a colour made darker or lighter by stepping to its neighbour in the palette, the way
+    a pixel artist shades by hand. Every colour has one darker neighbour and one lighter one, chosen by
+    eye and written down in two tables. Following a table step by step is a ramp: from white down to
+    night, from honey down through amber and wood. Here the steps are taken inside a patch: below 0 it
+    is the shadow of a cloud, above 0 a patch of sun.

+    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 type Params = typeof DEFAULTS;

+ /** The next darker colour of the palette for each number, and the next lighter one. [0] is nothing. */
+ const DARKER = [0, 24, 25, 2, 3, 15, 7, 20, 9, 20, 21, 10, 13, 25, 12, 16, 14, 25, 17, 18, 21, 24, 25, 22, 24, 24];
+ const LIGHTER = [0, 2, 3, 4, 5, 5, 5, 6, 6, 8, 11, 6, 14, 12, 16, 5, 15, 18, 19, 5, 9, 20, 23, 15, 25, 2];
+
  /** The number at a place of the picture; outside the picture nothing is. */
  export function at(src: Frame, x: number, y: number): number {
…
  }

- /** A colour moved along its ramp, wrong: the steps are added to the number, kept between 1 and 25. */
+ /** A colour moved along its ramp: one table look-up for every step, never a sum on the number. */
  export function ramp(n: number, steps: number): number {
-   if (n === 0) return 0;
-   return Math.min(25, Math.max(1, n + steps));
+   for (let i = 0; i < steps; i++) n = LIGHTER[n];
+   for (let i = 0; i < -steps; i++) n = DARKER[n];
+   return n;
  }

What the test says about the mistake

✖ one step darker is the colour the table names, not the next lower number
    got [ 7, 9, 15 ], expected [ 9, 21, 14 ]
✖ every step darker looks darker, and every step lighter looks lighter
    these steps down are not darker
    got [
  '1 to 1',   '6 to 5',
  '7 to 6',   '8 to 7',
  '9 to 8',   '10 to 9',
  '12 to 11', '13 to 12',
  '16 to 15', '17 to 16',
  '20 to 19', '21 to 20',
  '24 to 23'
], expected []
✖ seven steps down make every colour night, and five steps up make every colour white
    got [
   1,  1,  1,  1,  1,  1,  1,  1,
   2,  3,  4,  5,  6,  7,  8,  9,
  10, 11, 12, 13, 14, 15, 16, 17,
  18
], expected [
  24, 24, 24, 24, 24, 24, 24, 24,
  24, 24, 24, 24, 24, 24, 24, 24,
  24, 24, 24, 24, 24, 24, 24, 24,
  24
]
✖ the patch is twice as wide as it is tall, and outside it nothing changes
    four pixels to the left is still inside
    got 7, expected 9
✔ no steps leave every colour as it was
✔ every answer is a colour of the palette, for every step from -4 to 4
6 tests: 2 passed, 4 failed

Tell your AI

Add colour ramps to my pixel art game: a shader that makes colours darker or lighter by stepping to a neighbour colour of my palette, never by multiplying, dimming or mixing colours.

- My palette: [the colours in their order, or the file they are in].
- Make two tables with one entry for every colour: its darker neighbour and its lighter neighbour, both colours of my palette. Propose them as a list I can read, like "honey -> amber", and let me change any entry by eye. The darkest colour's darker neighbour is itself; the lightest colour's lighter neighbour is itself.
- Never count a colour's number up or down: the order of my palette is not its brightness.
- The shader looks a colour up in the table once for every step. A setting says how many steps, below 0 darker and above 0 lighter, and where it applies: [the whole screen, or a shape such as a cloud's shadow].
- Whole pixels only: nearest filtering, no smoothing.

Before you write the shader, write the same rule as a small plain function on a grid of whole numbers (0 = empty, 1 to N = my palette's colours) and test it: one step darker gives the colour the table names; every step down is darker and every step up is lighter by brightness (red, green and blue weighed 2126, 7152 and 722); following the table down from any colour reaches the darkest colour without going round in a circle. 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

TypeScript, small, and it needs nothing else. Your AI can read it and write the same rule for whatever your game is built with.

/* Colour ramps: a colour made darker or lighter by stepping to its neighbour in the palette, the way
   a pixel artist shades by hand. Every colour has one darker neighbour and one lighter one, chosen by
   eye and written down in two tables. Following a table step by step is a ramp: from white down to
   night, from honey down through amber and wood. Here the steps are taken inside a patch: below 0 it
   is the shadow of a cloud, above 0 a patch of sun.

   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 many steps along the ramp: below 0 darker, above 0 lighter, 0 leaves the colours. */
  steps: -2,
  /** The middle of the patch, in pixels from the left and from the top. */
  patchX: 120,
  patchY: 64,
  /** Half the patch's width in pixels. It is half as tall as it is wide. */
  reach: 44,
};
export type Params = typeof DEFAULTS;

/** The next darker colour of the palette for each number, and the next lighter one. [0] is nothing. */
const DARKER = [0, 24, 25, 2, 3, 15, 7, 20, 9, 20, 21, 10, 13, 25, 12, 16, 14, 25, 17, 18, 21, 24, 25, 22, 24, 24];
const LIGHTER = [0, 2, 3, 4, 5, 5, 5, 6, 6, 8, 11, 6, 14, 12, 16, 5, 15, 18, 19, 5, 9, 20, 23, 15, 25, 2];

/** 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];
}

/** A colour moved along its ramp: one table look-up for every step, never a sum on the number. */
export function ramp(n: number, steps: number): number {
  for (let i = 0; i < steps; i++) n = LIGHTER[n];
  for (let i = 0; i < -steps; i++) n = DARKER[n];
  return n;
}

/** The rule: inside the patch every colour goes its steps along the ramp, outside it stays. */
export function shade(x: number, y: number, _tick: number, src: Frame, p: Params = DEFAULTS): number {
  const n = at(src, x, y);
  // the patch is twice as wide as it is tall; its edge is measured with squares, in whole pixels
  const dx = x - p.patchX;
  const dy = y - p.patchY;
  if (dx * dx + 4 * dy * dy > p.reach * p.reach) return n;
  return ramp(n, p.steps);
}

The same rule as a shader: GLSL

For WebGL 2 and OpenGL ES 3.0. Only the part marked as the rule belongs to this lesson; the rest is the same in every lesson.

#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 steps;  // how many steps along the ramp: below 0 darker, above 0 lighter, 0 leaves the colours
uniform int patchX; // the middle of the patch, in pixels from the left
uniform int patchY; // and from the top
uniform int reach;  // half the patch's width in pixels; it is half as tall as it is wide

// the next darker colour of the palette for each number, and the next lighter one; [0] is nothing
const int DARKER[26] = int[26](0, 24, 25, 2, 3, 15, 7, 20, 9, 20, 21, 10, 13, 25, 12, 16, 14, 25, 17, 18, 21, 24, 25, 22, 24, 24);
const int LIGHTER[26] = int[26](0, 2, 3, 4, 5, 5, 5, 6, 6, 8, 11, 6, 14, 12, 16, 5, 15, 18, 19, 5, 9, 20, 23, 15, 25, 2);

// a colour moved along its ramp: one table look-up for every step, never a sum on the number
int ramp(int n, int k) {
  for (int i = 0; i < k; i++) n = LIGHTER[n];
  for (int i = 0; i < -k; i++) n = DARKER[n];
  return n;
}

// inside the patch every colour goes its steps along the ramp, outside it stays
int shade(ivec2 p) {
  int n = at(p);
  // the patch is twice as wide as it is tall; its edge is measured with squares, in whole pixels
  int dx = p.x - patchX;
  int dy = p.y - patchY;
  if (dx * dx + 4 * dy * dy > reach * reach) return n;
  return ramp(n, steps);
}
// ---- 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

The checked shader with Godot's names around it. We have not run this one: Godot is not part of our checks yet, so let your AI compare its picture with the one on this page.

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 steps;  // how many steps along the ramp: below 0 darker, above 0 lighter, 0 leaves the colours
uniform int patchX; // the middle of the patch, in pixels from the left
uniform int patchY; // and from the top
uniform int reach;  // half the patch's width in pixels; it is half as tall as it is wide

// the next darker colour of the palette for each number, and the next lighter one; [0] is nothing
const int DARKER[26] = int[26](0, 24, 25, 2, 3, 15, 7, 20, 9, 20, 21, 10, 13, 25, 12, 16, 14, 25, 17, 18, 21, 24, 25, 22, 24, 24);
const int LIGHTER[26] = int[26](0, 2, 3, 4, 5, 5, 5, 6, 6, 8, 11, 6, 14, 12, 16, 5, 15, 18, 19, 5, 9, 20, 23, 15, 25, 2);

// a colour moved along its ramp: one table look-up for every step, never a sum on the number
int ramp(int n, int k) {
  for (int i = 0; i < k; i++) n = LIGHTER[n];
  for (int i = 0; i < -k; i++) n = DARKER[n];
  return n;
}

// inside the patch every colour goes its steps along the ramp, outside it stays
int shade(ivec2 p) {
  int n = at(p);
  // the patch is twice as wide as it is tall; its edge is measured with squares, in whole pixels
  int dx = p.x - patchX;
  int dy = p.y - patchY;
  if (dx * dx + 4 * dy * dy > reach * reach) return n;
  return ramp(n, steps);
}
// ---- 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 { DEFAULTS, type Frame, shade } from "./shader.ts";

/** A tiny picture from rows of numbers: 0 is nothing, 1 to 25 are the palette's colours. */
function picture(rows: string[]): Frame {
  const cells = rows.map((r) => r.trim().split(/\s+/).map(Number));
  return { width: cells[0].length, height: cells.length, index: Uint8Array.from(cells.flat()) };
}

function whole(src: Frame, params = DEFAULTS): number[] {
  const out: number[] = [];
  for (let y = 0; y < src.height; y++) for (let x = 0; x < src.width; x++) out.push(shade(x, y, 0, src, params));
  return out;
}

/** The palette's colours in its order, to see how light each number really is. */
const PALETTE = [
  "#1f2142", "#4b4e70", "#787b98", "#c6c7e3", "#ffffff", "#f6f1e9", "#d6cdbd", "#f2c14e", "#e08a2e", "#c2412d",
  "#f4a3a0", "#2f62f0", "#2353d6", "#9db8ff", "#eaf0ff", "#cfe0ff", "#0f8a4c", "#7ccf9a", "#e2f4ea", "#8a5a3c",
  "#5c3b28", "#6d4ce6", "#c9bbff", "#12142b", "#272b52",
];
/** How light a colour looks: red, green and blue weighed the way the eye weighs them (2126, 7152, 722). */
function lightness(n: number): number {
  const hex = PALETTE[n - 1];
  return 2126 * parseInt(hex.slice(1, 3), 16) + 7152 * parseInt(hex.slice(3, 5), 16) + 722 * parseInt(hex.slice(5, 7), 16);
}

/** Every colour of the palette once, in one row, and a patch that covers all of it. */
const ALL = picture([Array.from({ length: 25 }, (_, i) => i + 1).join(" ")]);
const OVER_ALL = { ...DEFAULTS, patchX: 12, patchY: 0, reach: 13 };

test("one step darker is the colour the table names, not the next lower number", () => {
  // honey, coral, sky
  const src = picture(["8 10 16"]);
  const over = { ...DEFAULTS, patchX: 1, patchY: 0, reach: 4 };
  assert.deepEqual(whole(src, { ...over, steps: -1 }), [9, 21, 14]); // amber, dark wood, light blue
  assert.deepEqual(whole(src, { ...over, steps: 1 }), [6, 11, 15]); // cream, pink, palest blue
});

test("every step darker looks darker, and every step lighter looks lighter", () => {
  const down = whole(ALL, { ...OVER_ALL, steps: -1 });
  const up = whole(ALL, { ...OVER_ALL, steps: 1 });
  // night (24) is the darkest colour and stays night; white (5) is the lightest and stays white
  const notDarker = down.map((to, i) => [i + 1, to]).filter(([n, to]) => (n === 24 ? to !== 24 : lightness(to) >= lightness(n)));
  const notLighter = up.map((to, i) => [i + 1, to]).filter(([n, to]) => (n === 5 ? to !== 5 : lightness(to) <= lightness(n)));
  assert.deepEqual(notDarker.map(([n, to]) => `${n} to ${to}`), [], "these steps down are not darker");
  assert.deepEqual(notLighter.map(([n, to]) => `${n} to ${to}`), [], "these steps up are not lighter");
});

test("seven steps down make every colour night, and five steps up make every colour white", () => {
  // the longest ramp down starts at white: palest blue, sky, light blue, blue, deep blue, the lighter night, night
  assert.deepEqual(whole(ALL, { ...OVER_ALL, steps: -7 }), Array(25).fill(24));
  assert.deepEqual(whole(ALL, { ...OVER_ALL, steps: 5 }), Array(25).fill(5));
});

test("the patch is twice as wide as it is tall, and outside it nothing changes", () => {
  const src = picture(["8 8 8 8 8 8 8 8 8", "8 8 8 8 8 8 8 8 8", "8 8 8 8 8 8 8 8 8", "8 8 8 8 8 8 8 8 8", "8 8 8 8 8 8 8 8 8"]);
  const p = { ...DEFAULTS, steps: -1, patchX: 4, patchY: 2, reach: 4 };
  assert.equal(shade(0, 2, 0, src, p), 9, "four pixels to the left is still inside");
  assert.equal(shade(4, 0, 0, src, p), 9, "two pixels up is still inside");
  assert.equal(shade(0, 0, 0, src, p), 8, "the corner is outside");
  assert.equal(shade(8, 4, 0, src, p), 8, "the other corner is outside");
});

test("no steps leave every colour as it was", () => {
  assert.deepEqual(whole(ALL, { ...OVER_ALL, steps: 0 }), [...ALL.index]);
});

test("every answer is a colour of the palette, for every step from -4 to 4", () => {
  for (let steps = -4; steps <= 4; steps++) {
    for (const n of whole(ALL, { ...OVER_ALL, steps })) assert.ok(n >= 1 && n <= 25, `${n} at ${steps} steps`);
  }
});
What we checked
  • 6 tests pass on the rule.
  • With the mistake put in, 4 of them fail: "one step darker is the colour the table names, not the next lower number", "every step darker looks darker, and every step lighter looks lighter", "seven steps down make every colour night, and five steps up make every colour white", "the patch is twice as wide as it is tall, and outside it nothing changes".
  • The mistake shows in the picture above: switch to "With the mistake".
  • The shader ran in a browser (Chrome/158.0.8127.37) on 14 pictures and gave the rule's picture in 453,600 of 453,600 pixels.

Run by pnpm courses:check on 2026-11-27 with Node v24.15.0.

Try it

Read up

Where to go deeper: the pages of ours this lesson builds on, and what others have written about its point.

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.

Question 1 of 3Easy

In pixel art, how does a colour get darker when it lies in shade?

All questions of this course · The quiz and the Game Coder IQ

Next lesson Dithering in a Pixel Art Shader: Two Colours in 16 Steps

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.

How useful was this page?

Nobody has rated this page yet.

One click, no account. It tells us which pages to make better next.

Last updated: 2026-11-27