Spielerando

Lesson 15 of 24Light with few colours8 minFree

Day and Night Shader for Pixel Art: Darker by the Hour

A day that turns to night is one rule laid over the whole screen. It looks at the hour and makes every colour darker by as many steps as the hour asks for, through the palette's own darker colours. Only what gives light is left out: the windows and the lamps stay lit, and that is what makes a night scene readable.

A pixel art village lane with two cottages, a street lamp, two beehives and a bush, under a blue sky. The beekeeper walks along the lane. The hours pass: the sky and everything under it turn darker in small dotted steps until it is night, while the window panes and the lamp's glass keep their warm honey colour. Then the morning comes back.

One day in eight seconds, from noon. The rule darkens the whole picture by the hour, through the palette's table and in sixteenths mixed by a pattern. The window panes and the lamp are in a second picture of what gives light, so they keep their colour, while the hives' honey goes dark with the rest.

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

The idea

Games with a clock, a farm, a village, a long journey, show the time of day in the colour of the whole picture. Toward evening everything gets darker and bluer, at night the world is a few dark shapes, and in the morning it comes back. The player reads the hour without looking at a number.

In pixel art the night is not a dark film over the picture. It is made of the palette's own darker colours: the cream wall of a cottage becomes sand, then wood, then dark wood, then night; the sky goes through light blue, blue and deep blue to night. Nobody draws the lane twice. It is drawn once, by day, and the rule decides how dark it is.

What makes a night picture work is what stays lit. A window with light behind it, a street lamp, a fire: the player finds the houses and the way by them. So the rule has a second picture, as large as the screen, in which only the things that give light are drawn.

How it works

The rule looks at three things: the pixel's colour, the hour ("hour of the day", 0 to 23) and how many steps darker the deepest night is ("steps darker at night", 4 at the start).

A table gives every hour its share of the night in sixteenths. From 8 to 16 o'clock the share is 0 and the picture is as drawn. From 21 to 3 o'clock it is 16, the full night. Dusk and dawn take four hours each in between. The share times the steps gives the darkness in sixteenths of a step: the whole steps go through the palette's table of darker colours, and what is left over is mixed in by the 4 by 4 pattern from the lesson on dithering. With 4 steps of night, every hour of dusk lands exactly between two steps, so half of the pixels are one step further than the other half.

Before any of that, the rule looks into the second picture. If something is drawn there at this place, the pixel keeps the colour it came with, at every hour. In the lane, that second picture holds the window panes and the lamp's glass and nothing else. Until you touch a slider, one hour passes every third of a second, starting at noon.

What goes wrong

Asked for night, an AI usually darkens the whole screen in one go and forgets the lights. The windows and the lamp go dark with everything else, and the lane at midnight becomes one dark shape with nothing to steer by. It is easy to miss: by day the two rules give the same picture, so a quick look at the game at noon shows nothing wrong.

It happens because a rule for the whole screen sees only colours. The honey of a lit window is the same number as the honey of a hive, and nothing in the colour says that one of them gives light. Switch the picture to "With the mistake" while it is night: the panes and the lamp's glass turn as dark as the walls around them.

The test "what gives light keeps its colour at every hour" catches it. A second test, "the picture of what gives light decides, not the colour", catches the quick fix an AI may try next: keeping every honey pixel bright. Then the hives would glow in the dark as well.

The mistake, line by line

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

- /* Day and night, with the mistake: the whole picture is darkened by the hour, and the rule never looks
-    at the picture of what gives light. The lit windows and the lantern's flame go dark with everything
-    else, so at night the street is one dark shape with nothing lit in it.
+ /* Day and night: the whole picture is graded by the hour. At noon every colour is its own; in the
+    evening the picture goes darker, step by step through the palette's table of darker colours, until
+    at night it is as many steps darker as `night` says. Between two steps the 4 by 4 pattern mixes
+    them, so dusk comes in sixteen small steps instead of a few jumps. What gives light, a lit window or
+    a lantern's flame, is kept out of the grade: it keeps its colour at every hour.

+    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. `other` is a second picture of the same size: what is drawn in it gives light.
+
     From the course "Shaders for pixel art" on spielerando.com.
     SPDX-License-Identifier: MIT */
…
  }

- /** The rule, wrong: everything darkened by the hour, also what gives light. */
- export function shade(x: number, y: number, _tick: number, src: Frame, p: Params = DEFAULTS, _other?: Frame): number {
+ /** The rule: the picture darkened by the hour, except what gives light. */
+ export function shade(x: number, y: number, _tick: number, src: Frame, p: Params = DEFAULTS, other?: Frame): number {
    const n = at(src, x, y);
+   // what gives light (a lit window, a lantern's flame) keeps its colour at every hour
+   if (other && at(other, x, y) !== 0) return n;
    // how dark this hour is, in sixteenths of a step: the whole steps, and what is left over
    const depth = DUSK[wrap(p.hour, 24)] * p.night;

What the test says about the mistake

✔ by day every pixel keeps its colour
✔ at midnight a colour is as many steps darker as `night` says, by the table
✖ what gives light keeps its colour at every hour
    at 0 o'clock
    got 24, expected 8
✖ the picture of what gives light decides, not the colour: the same honey on the hive goes dark
    got 20, expected 8
✔ dusk comes in sixteenths: at 17 o'clock 6 of every 16 pixels are one step darker
✔ the evening only gets darker, hour by hour, and the morning only lighter
✔ an empty pixel stays empty at every hour
7 tests: 5 passed, 2 failed

Tell your AI

Write a shader for my pixel art game that grades the whole screen by the time of day, from noon to night and back.

- Work on whole pixels and the colours of my palette only: no blending, no new colours. Each colour's next darker colour: [a table, or each ramp of my palette from light to dark].
- The hour is a whole number from 0 to 23 that the game sets. A table gives each hour its share of the night in sixteenths: 0 from 8 to 16 o'clock, 16 from 21 to 3 o'clock, and four hours of dusk and of dawn in between.
- At full night every colour is [4] steps darker through the table. Between two steps, mix them with the 4 by 4 Bayer pattern fixed to the screen, so dusk comes in small steps.
- What gives light keeps its colour at every hour: [the lit windows, lamps, fires, the player's glow]. Take it from a second picture or layer of what gives light, as large as the screen, not from its colour.

Before the shader, write the same rule as a small plain function. It takes the picture as a grid of whole numbers (0 = empty, 1 to N = my colours), the picture of what gives light, a place and the hour, and returns the number for that one pixel. Test it: at noon nothing changes; at midnight a colour is [4] steps darker; a lit window keeps its colour at every hour; a pixel of the same colour that is not in the picture of what gives light goes dark. 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.

/* Day and night: the whole picture is graded by the hour. At noon every colour is its own; in the
   evening the picture goes darker, step by step through the palette's table of darker colours, until
   at night it is as many steps darker as `night` says. Between two steps the 4 by 4 pattern mixes
   them, so dusk comes in sixteen small steps instead of a few jumps. What gives light, a lit window or
   a lantern's flame, is kept out of the grade: it keeps its colour at every hour.

   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. `other` is a second picture of the same size: what is drawn in it gives light.

   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 = {
  /** The hour of the day, 0 to 23: 12 is noon, 0 is midnight. */
  hour: 12,
  /** How many steps darker the picture is in the middle of the night. */
  night: 4,
};
export type Params = typeof DEFAULTS;

/** How much of the night's dark each hour has, in sixteenths: 0 by day, 16 in the night. */
const DUSK = [16, 16, 16, 16, 14, 10, 6, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 6, 10, 14, 16, 16, 16];
/** The 4 by 4 pattern for dithering: 0 to 15, each once.  BAYER[(y & 3) * 4 + (x & 3)] */
const BAYER = [0, 8, 2, 10, 12, 4, 14, 6, 3, 11, 1, 9, 15, 7, 13, 5];
/** The next darker colour of the palette for each number. [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];

/** 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 wrapped into 0 .. n-1, also for a below zero */
const wrap = (a: number, n: number) => a - n * Math.floor(a / n);

/** A colour made darker by the table, `steps` times. */
function darker(n: number, steps: number): number {
  let c = n;
  for (let i = 0; i < steps; i++) c = DARKER[c];
  return c;
}

/** The rule: the picture darkened by the hour, except what gives light. */
export function shade(x: number, y: number, _tick: number, src: Frame, p: Params = DEFAULTS, other?: Frame): number {
  const n = at(src, x, y);
  // what gives light (a lit window, a lantern's flame) keeps its colour at every hour
  if (other && at(other, x, y) !== 0) return n;
  // how dark this hour is, in sixteenths of a step: the whole steps, and what is left over
  const depth = DUSK[wrap(p.hour, 24)] * p.night;
  let steps = depth >> 4;
  // what is left over is mixed in by the pattern: that many of every 16 pixels go one step further
  if ((depth & 15) > BAYER[(y & 3) * 4 + (x & 3)]) steps++;
  return darker(n, 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 hour;  // the hour of the day, 0 to 23: 12 is noon, 0 is midnight
uniform int night; // how many steps darker the picture is in the middle of the night

// how much of the night's dark each hour has, in sixteenths: 0 by day, 16 in the night
const int DUSK[24] = int[24](16, 16, 16, 16, 14, 10, 6, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 6, 10, 14, 16, 16, 16);
// the 4 by 4 pattern for dithering: 0 to 15, each once.  BAYER[(y & 3) * 4 + (x & 3)]
const int BAYER[16] = int[16](0, 8, 2, 10, 12, 4, 14, 6, 3, 11, 1, 9, 15, 7, 13, 5);
// the next darker colour of the palette for each number. [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);

// a colour made darker by the table, steps times
int darker(int n, int steps) {
  int c = n;
  for (int i = 0; i < steps; i++) c = DARKER[c];
  return c;
}

// the picture darkened by the hour, except what gives light
int shade(ivec2 p) {
  int n = at(p);
  // what gives light (a lit window, a lantern's flame) keeps its colour at every hour
  if (otherAt(p) != 0) return n;
  // how dark this hour is, in sixteenths of a step: the whole steps, and what is left over
  int depth = DUSK[wrap(hour, 24)] * night;
  int steps = depth >> 4;
  // what is left over is mixed in by the pattern: that many of every 16 pixels go one step further
  if ((depth & 15) > BAYER[(p.y & 3) * 4 + (p.x & 3)]) steps++;
  return darker(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 hour;  // the hour of the day, 0 to 23: 12 is noon, 0 is midnight
uniform int night; // how many steps darker the picture is in the middle of the night

// how much of the night's dark each hour has, in sixteenths: 0 by day, 16 in the night
const int DUSK[24] = int[24](16, 16, 16, 16, 14, 10, 6, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 6, 10, 14, 16, 16, 16);
// the 4 by 4 pattern for dithering: 0 to 15, each once.  BAYER[(y & 3) * 4 + (x & 3)]
const int BAYER[16] = int[16](0, 8, 2, 10, 12, 4, 14, 6, 3, 11, 1, 9, 15, 7, 13, 5);
// the next darker colour of the palette for each number. [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);

// a colour made darker by the table, steps times
int darker(int n, int steps) {
  int c = n;
  for (int i = 0; i < steps; i++) c = DARKER[c];
  return c;
}

// the picture darkened by the hour, except what gives light
int shade(ivec2 p) {
  int n = at(p);
  // what gives light (a lit window, a lantern's flame) keeps its colour at every hour
  if (otherAt(p) != 0) return n;
  // how dark this hour is, in sixteenths of a step: the whole steps, and what is left over
  int depth = DUSK[wrap(hour, 24)] * night;
  int steps = depth >> 4;
  // what is left over is mixed in by the pattern: that many of every 16 pixels go one step further
  if ((depth & 15) > BAYER[(p.y & 3) * 4 + (p.x & 3)]) steps++;
  return darker(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 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 sand wall (7) with a lit window of honey (8) in a dark frame (1), a hive of the same honey on the
// right, and slate stones (2) below
const STREET = picture([
  "7777777",
  "7881777",
  "7881777",
  "7777887",
  "2222222",
]);
// what gives light: the window's panes only
const GLOWS = picture([
  "0000000",
  "0880000",
  "0880000",
  "0000000",
  "0000000",
]);

const BLOCK = (digit: string) => picture(["", "", "", ""].map(() => digit.repeat(4)));
const when = (hour: number, night = DEFAULTS.night) => ({ ...DEFAULTS, hour, night });

test("by day every pixel keeps its colour", () => {
  for (const hour of [8, 12, 16]) {
    for (let y = 0; y < STREET.height; y++) {
      for (let x = 0; x < STREET.width; x++) assert.equal(shade(x, y, 0, STREET, when(hour), GLOWS), STREET.index[y * STREET.width + x]);
    }
  }
});

test("at midnight a colour is as many steps darker as `night` says, by the table", () => {
  // sand goes darker as wood (20), then dark wood (21); slate as night, a shade lighter (25)
  assert.equal(shade(0, 0, 0, STREET, when(0, 1), GLOWS), 20);
  assert.equal(shade(0, 0, 0, STREET, when(0, 2), GLOWS), 21);
  assert.equal(shade(0, 4, 0, STREET, when(0, 1), GLOWS), 25);
});

test("what gives light keeps its colour at every hour", () => {
  for (let hour = 0; hour < 24; hour++) {
    assert.equal(shade(1, 1, 0, STREET, when(hour), GLOWS), 8, `at ${hour} o'clock`);
    assert.equal(shade(2, 2, 0, STREET, when(hour), GLOWS), 8, `at ${hour} o'clock`);
  }
});

test("the picture of what gives light decides, not the colour: the same honey on the hive goes dark", () => {
  assert.equal(shade(1, 1, 0, STREET, when(0, 2), GLOWS), 8);
  assert.equal(shade(4, 3, 0, STREET, when(0, 2), GLOWS), 20);
  // the window's dark frame is not in the picture of what gives light either
  assert.equal(shade(3, 1, 0, STREET, when(0, 2), GLOWS), 24);
});

test("dusk comes in sixteenths: at 17 o'clock 6 of every 16 pixels are one step darker", () => {
  const sand = BLOCK("7");
  const answers: number[] = [];
  for (let y = 0; y < 4; y++) for (let x = 0; x < 4; x++) answers.push(shade(x, y, 0, sand, when(17, 3)));
  assert.equal(answers.filter((n) => n === 20).length, 6);
  assert.equal(answers.filter((n) => n === 7).length, 10);
});

test("the evening only gets darker, hour by hour, and the morning only lighter", () => {
  // white goes darker as palest blue, sky, light blue, blue, deep blue, night: its place in that list is its steps
  const RAMP = [5, 15, 16, 14, 12, 13, 25, 24];
  const white = BLOCK("5");
  const steps = (hour: number) => {
    let sum = 0;
    for (let y = 0; y < 4; y++) for (let x = 0; x < 4; x++) sum += RAMP.indexOf(shade(x, y, 0, white, when(hour, 3)));
    return sum;
  };
  const evening = [12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 0].map(steps);
  const morning = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12].map(steps);
  for (let i = 1; i < evening.length; i++) assert.ok(evening[i] >= evening[i - 1], `the evening got lighter: ${evening.join(", ")}`);
  for (let i = 1; i < morning.length; i++) assert.ok(morning[i] <= morning[i - 1], `the morning got darker: ${morning.join(", ")}`);
  assert.ok(evening[0] === 0 && evening[12] === 16 * 3);
});

test("an empty pixel stays empty at every hour", () => {
  const holes = picture(["070", "000"]);
  for (let hour = 0; hour < 24; hour++) assert.equal(shade(0, 0, 0, holes, when(hour)), 0);
});
What we checked
  • 7 tests pass on the rule.
  • With the mistake put in, 2 of them fail: "what gives light keeps its colour at every hour", "the picture of what gives light decides, not the colour: the same honey on the hive goes dark".
  • 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.

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

The village lane is drawn once, by day. How does this lesson's rule turn it into night?

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

Next lesson Fog in a Pixel Art Shader That Leaves the Figure Clear

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