Spielerando

Lektion 7 von 24Ein Sprite7 Min.Kostenlos

Palette Swap als Shader für Pixel Art: neue Kleider, gleiches Sprite

Ein Palette Swap zieht einer gezeichneten Figur andere Kleider an, indem er ihre Farben über eine Tabelle tauscht: Diese Farbe wird zu jener. Aus einer Zeichnung wird so ein zweiter Spieler, eine stärkere Sorte Gegner, eine Mannschaft oder dieselbe Figur bei Nacht. Die Regel schlägt die Farbe jedes Pixels einmal nach, und zwar die Farbe, in der er gezeichnet wurde. Dieses eine Wort „einmal“ ist die ganze Lektion.

Eine Imkerin als Pixel Art geht in einer Schneiderwerkstatt hin und her, an einem Regal mit Stoffballen und einer Stange mit Mänteln vorbei. Jedes Mal, wenn sie umkehrt, trägt sie andere Kleider: eine blaue Jacke mit cremefarbener Hose, eine rosa Jacke mit weißem Hut, dunklere Kleider für die Nacht, dann ihr eigenes Creme und Blau.

Es gibt eine einzige Zeichnung von ihr. Die Regel schlägt die Farbe jedes Pixels in der Tabelle der Paare nach, einmal, und zwar die Farbe, wie sie gezeichnet wurde; die Werkstatt hinter ihr bekommt die Regel nicht und behält ihre Farben.

Auch in dieser Lektion 2 Lesetipps zum Vertiefen 3 Fragen zum Selbsttest

Die Idee

In Pixel Art ist jede Farbe einer Figur eine Nummer der Palette. Die Jacke der Imkerin ist Creme, 6, mit Sand, 7, wo sie im Schatten liegt; ihre Hose ist Blau, 12, und Tiefblau, 13, am hinteren Bein. Andere Kleider sind andere Nummern an denselben Stellen. Die Form, die Schatten und jedes Bild ihres Gangs bleiben, wie sie gezeichnet wurden.

Das Spiel zeichnet sie also nicht für jede Kleidung neu, und auch nicht jedes Bild ihres Gangs für jede Kleidung noch einmal. Es behält eine Zeichnung und für jede Kleidung eine kurze Tabelle von Paaren. Ein Spieler sieht das als zweiten Spieler in anderen Farben, als denselben Gegner in Rot, wenn er härter zuschlägt, als Mannschaft in ihren eigenen Farben oder als Kleider, die man in einem Laden kauft.

So funktioniert es

Die Regel sieht sich die Zahl an ihrem eigenen Platz an. Ist sie 0, war dort nichts gezeichnet, und die Antwort ist 0. Sonst geht sie die Paare der gewählten Kleidung durch und vergleicht die erste Zahl jedes Paars mit der Farbe, wie sie gezeichnet wurde. Das Paar, das mit ihr beginnt, gibt die Antwort, und dort hört die Regel auf. Beginnt kein Paar mit ihr, bleibt die Farbe, wie sie gezeichnet wurde: Ihr weißes Auge und ihre dunklen Stiefel stehen in keinem Paar, also ändern sie sich nie.

Die Tabelle hier hat vier Kleidungen mit bis zu sieben Paaren:

Der eine Regler, Kleidung, wählt die Zeile der Tabelle. Im Bild rückt er jedes Mal weiter, wenn sie umkehrt. Nur sie wird der Regel gegeben; der Stoff im Regal und die Mäntel an der Stange liegen dahinter und behalten ihre Farben.

Was schiefgeht

Eine KI schreibt den Tausch oft als Reihe von Schritten: Ist die Farbe Creme, mach sie Blau; ist sie Blau, mach sie Creme; und so weiter die Liste hinunter. Jeder Schritt arbeitet mit dem, was der Schritt davor übrig gelassen hat. Zeile für Zeile liest sich das richtig, und so würdest du es auch mit „Suchen und Ersetzen“ in einem Text machen. Aber eine Farbe, die ein Paar gerade eingesetzt hat, kann ein späteres Paar, das mit ihr beginnt, noch einmal erwischen.

Schalte das Bild auf „Mit dem Fehler“, während sie Kleidung 1 trägt. Ihre cremefarbene Jacke wird beim Paar für Creme blau und beim Paar für Blau, zwei Zeilen weiter unten, wieder cremefarben; ihre blaue Hose wird beim selben Paar cremefarben. Jacke und Hose enden im selben Creme und Sand. In Kleidung 3 macht derselbe Fehler sie viel zu dunkel: Ihr hellblauer Schleier wird blau, dann tiefblau, dann nachtblau, drei Stufen statt einer, und Jacke und Hut enden in Holzbraun.

Der Fehler versteckt sich gut. In Kleidung 2 geht keine Farbe, die aus einem Paar herauskommt, in ein späteres hinein, also sieht sie genau aus wie mit der Regel. Eine KI, die nur so eine Kleidung ausprobiert, sieht nichts Falsches.

Zwei Tests fangen ihn: „two colours that trade places each change once, and not back“ verlangt, dass Creme blau wird und blau bleibt, und „a colour that one pair puts in is not swapped again by a later pair“ verlangt eine Stufe dunkler, nicht drei.

Der Fehler, Zeile für Zeile

Zeilen mit - sind die Regel mit dem Fehler, Zeilen mit + die Regel, wie sie sein soll.

- /* Palette swap, with the mistake: the rule goes through the pairs one after another and swaps the
-    colour it holds now, not the colour as it was drawn. A colour that a pair has just put in is
-    swapped again by a later pair that starts with it. Two colours that should trade places both end up
-    as one of them, and a step darker becomes two or three steps darker.
+ /* Palette swap: the same drawn picture in other clothes. A table of pairs says which colour becomes
+    which, and the rule looks up every pixel's colour in it, once, by the colour as it was drawn. One
+    drawing of a figure gives a second player, a stronger enemy, a team's colours or a night watch,
+    without drawing her again.

+    Because each pixel is looked up once, two colours can trade places: the jacket's colour goes to the
+    trousers and the trousers' colour to the jacket, and neither is swapped a second time.
+
     From the course "Shaders for pixel art" on spielerando.com.
     SPDX-License-Identifier: MIT */
…
  }

- /** The rule, wrong: the pairs are applied one after another, each to what the last one left. */
+ /** The rule: the colour as drawn, looked up once in the outfit's pairs. */
  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; // nothing stays nothing
    const which = Math.min(Math.max(p.outfit, 0), OUTFITS - 1);
-   let color = n;
    for (let i = 0; i < PAIRS; i++) {
      const k = (which * PAIRS + i) * 2;
-     // wrong: compared with the colour as it is now, so a colour just put in is swapped again
-     if (TABLE[k] === color) color = TABLE[k + 1];
+     // compared with the colour as it was drawn: the pair that starts with it gives the answer, and that is all
+     if (TABLE[k] === n) return TABLE[k + 1];
    }
-   return color;
+   return n; // in no pair: as drawn
  }

Was der Test zum Fehler sagt

✔ outfit 0 leaves every pixel as it was drawn
✖ a colour in the table becomes its partner
    outfit 1: cream jacket to blue
    got 6, expected 12
✔ a colour that is in no pair keeps its own colour
✔ an empty pixel stays empty in every outfit
✖ two colours that trade places each change once, and not back
    cream becomes blue and stays blue
    got 6, expected 12
✖ a colour that one pair puts in is not swapped again by a later pair
    cream one step darker is sand, not wood
    got 20, expected 7
✔ the shape stays, and every pixel of one colour gets the same new colour
7 tests: 4 passed, 3 failed

Das sagst du deiner KI

Schreib einen Palette Swap als Shader für mein Pixel-Art-Sprite: dieselbe Zeichnung in anderen Kleidern, über eine Tabelle von Farbpaaren.

- Arbeite mit ganzen Pixeln: kein Glätten und kein Mischen. Lies die Textur des Sprites ohne Filter (nearest) und vergleiche Farben genau.
- Jede Kleidung ist eine Liste von Paaren: eine Farbe, wie sie gezeichnet ist, und die Farbe, zu der sie wird. Meine Kleidungen: [für jede Kleidung ihre Paare, zum Beispiel: Rot wird Grün, Grün wird Rot].
- Schlag jeden Pixel einmal nach, mit der Farbe, wie sie gezeichnet wurde. Das Paar, das mit dieser Farbe beginnt, gibt die Antwort, und kein späteres Paar darf sie noch einmal tauschen. Farben in keinem Paar und leere Pixel (Alpha 0) bleiben, wie sie sind.
- Welche Kleidung getragen wird, muss eine Einstellung sein, die ich aus dem Code des Spiels ändern kann, und Kleidung 0 bedeutet die Farben, wie sie gezeichnet sind.

Bevor du den Shader schreibst, schreib dieselbe Regel als kleine, einfache Funktion. Sie bekommt ein Raster aus ganzen Zahlen (0 = leer, 1 bis N = die Farben meiner Palette), einen Platz und die Kleidung und gibt die Zahl für diesen einen Pixel zurück. Teste sie: Zwei Farben, die die Plätze tauschen, ändern sich je einmal und nicht zurück, eine Farbe, die ein Paar eingesetzt hat, wird von keinem späteren Paar noch einmal getauscht, und eine Farbe in keinem Paar bleibt. Zeig mir die Ausgabe des Tests. Schreib dann den Shader so, dass er genau das tut, was die Funktion tut.

Der Code

Dreimal dieselbe Regel. Die erste ist die, die wir testen; die beiden Shader sind diese Regel in einer Shader-Sprache, und die läuft in deinem Spiel.

Die Regel, getestet: shader.ts

TypeScript, klein, und sie braucht nichts sonst. Deine KI kann sie lesen und dieselbe Regel für das schreiben, womit dein Spiel gebaut ist.

/* Palette swap: the same drawn picture in other clothes. A table of pairs says which colour becomes
   which, and the rule looks up every pixel's colour in it, once, by the colour as it was drawn. One
   drawing of a figure gives a second player, a stronger enemy, a team's colours or a night watch,
   without drawing her again.

   Because each pixel is looked up once, two colours can trade places: the jacket's colour goes to the
   trousers and the trousers' colour to the jacket, and neither is swapped a second time.

   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 = {
  /** Which clothes: 0 is the picture as drawn, 1 to 3 are the outfits in TABLE. */
  outfit: 1,
};
export type Params = typeof DEFAULTS;

/** How many pairs an outfit has, and how many outfits there are. */
const PAIRS = 7;
const OUTFITS = 4;
/** The outfits, PAIRS pairs each: a colour as drawn, and the colour it becomes. 0, 0 is an unused pair.
    Numbers: 5 white, 6 cream, 7 sand, 8 honey, 9 amber, 10 coral, 11 pink, 12 blue, 13 deep blue,
    14 light blue, 20 wood, 25 night (a shade lighter). */
const TABLE = [
  // 0: as drawn
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  // 1: the jacket and the trousers trade colours
  6, 12, 7, 13, 12, 6, 13, 7, 0, 0, 0, 0, 0, 0,
  // 2: a pink jacket, a white hat, coral trim
  6, 11, 7, 10, 8, 5, 9, 10, 0, 0, 0, 0, 0, 0,
  // 3: the night watch: her clothes one step darker, as in the palette's DARKER table
  6, 7, 7, 20, 14, 12, 12, 13, 13, 25, 8, 9, 9, 20,
];

/** 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 as drawn, looked up once in the outfit's pairs. */
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; // nothing stays nothing
  const which = Math.min(Math.max(p.outfit, 0), OUTFITS - 1);
  for (let i = 0; i < PAIRS; i++) {
    const k = (which * PAIRS + i) * 2;
    // compared with the colour as it was drawn: the pair that starts with it gives the answer, and that is all
    if (TABLE[k] === n) return TABLE[k + 1];
  }
  return n; // in no pair: as drawn
}

Dieselbe Regel als Shader: GLSL

Für WebGL 2 und OpenGL ES 3.0. Nur der als Regel markierte Teil gehört zu dieser Lektion; der Rest ist in jeder Lektion gleich.

#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 outfit; // which clothes: 0 is the picture as drawn, 1 to 3 are the outfits in TABLE

// how many pairs an outfit has, and how many outfits there are
const int PAIRS = 7;
const int OUTFITS = 4;
// the outfits, PAIRS pairs each: a colour as drawn, and the colour it becomes. 0, 0 is an unused pair.
// Numbers: 5 white, 6 cream, 7 sand, 8 honey, 9 amber, 10 coral, 11 pink, 12 blue, 13 deep blue,
// 14 light blue, 20 wood, 25 night (a shade lighter).
const int TABLE[56] = int[56](
  // 0: as drawn
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  // 1: the jacket and the trousers trade colours
  6, 12, 7, 13, 12, 6, 13, 7, 0, 0, 0, 0, 0, 0,
  // 2: a pink jacket, a white hat, coral trim
  6, 11, 7, 10, 8, 5, 9, 10, 0, 0, 0, 0, 0, 0,
  // 3: the night watch: her clothes one step darker, as in the palette's DARKER table
  6, 7, 7, 20, 14, 12, 12, 13, 13, 25, 8, 9, 9, 20
);

// the colour as drawn, looked up once in the outfit's pairs
int shade(ivec2 p) {
  int n = at(p);
  if (n == 0) return 0; // nothing stays nothing
  int which = min(max(outfit, 0), OUTFITS - 1);
  for (int i = 0; i < PAIRS; i++) {
    int k = (which * PAIRS + i) * 2;
    // compared with the colour as it was drawn: the pair that starts with it gives the answer, and that is all
    if (TABLE[k] == n) return TABLE[k + 1];
  }
  return n; // in no pair: as drawn
}
// ---- 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);
}

Dieselbe Regel für Godot 4

Der geprüfte Shader mit Godots Namen darum herum. Diesen haben wir nicht ausgeführt: Godot gehört noch nicht zu unseren Prüfungen. Lass deine KI sein Bild mit dem auf dieser Seite vergleichen.

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 the sprite, and give it the sprite's own picture once:
//   material.set_shader_parameter("SRC", texture)
uniform sampler2D SRC : filter_nearest;
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 outfit; // which clothes: 0 is the picture as drawn, 1 to 3 are the outfits in TABLE

// how many pairs an outfit has, and how many outfits there are
const int PAIRS = 7;
const int OUTFITS = 4;
// the outfits, PAIRS pairs each: a colour as drawn, and the colour it becomes. 0, 0 is an unused pair.
// Numbers: 5 white, 6 cream, 7 sand, 8 honey, 9 amber, 10 coral, 11 pink, 12 blue, 13 deep blue,
// 14 light blue, 20 wood, 25 night (a shade lighter).
const int TABLE[56] = int[56](
  // 0: as drawn
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  // 1: the jacket and the trousers trade colours
  6, 12, 7, 13, 12, 6, 13, 7, 0, 0, 0, 0, 0, 0,
  // 2: a pink jacket, a white hat, coral trim
  6, 11, 7, 10, 8, 5, 9, 10, 0, 0, 0, 0, 0, 0,
  // 3: the night watch: her clothes one step darker, as in the palette's DARKER table
  6, 7, 7, 20, 14, 12, 12, 13, 13, 25, 8, 9, 9, 20
);

// the colour as drawn, looked up once in the outfit's pairs
int shade(ivec2 p) {
  int n = at(p);
  if (n == 0) return 0; // nothing stays nothing
  int which = min(max(outfit, 0), OUTFITS - 1);
  for (int i = 0; i < PAIRS; i++) {
    int k = (which * PAIRS + i) * 2;
    // compared with the colour as it was drawn: the pair that starts with it gives the answer, and that is all
    if (TABLE[k] == n) return TABLE[k + 1];
  }
  return n; // in no pair: as drawn
}
// ---- end of the rule ----

void fragment() {
  ivec2 p = ivec2(UV * vec2(textureSize(SRC, 0)));
  int n = shade(p);
  COLOR = n == 0 ? vec4(0.0) : vec4(PALETTE[n], 1.0);
}
Der 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((row) => row.trim().split(/\s+/).map(Number));
  return { width: cells[0].length, height: cells.length, index: Uint8Array.from(cells.flat()) };
}

// a hat (honey, amber band), a veil (light blue), a jacket (cream, sand in shade), trousers (blue, deep blue)
const SUIT = picture([
  " 0  8  8  0",
  " 9 14 14  9",
  " 6  6  6  7",
  "12  0  0 13",
]);

/** The whole picture in an outfit. */
function whole(outfit: number): number[] {
  const out: number[] = [];
  for (let y = 0; y < SUIT.height; y++) for (let x = 0; x < SUIT.width; x++) out.push(shade(x, y, 0, SUIT, { ...DEFAULTS, outfit }));
  return out;
}

/** The colour that a drawn colour becomes in an outfit, asked at a place where SUIT has it. */
function becomes(color: number, outfit: number): number {
  const i = SUIT.index.indexOf(color);
  assert.ok(i >= 0, `${color} is not in the test picture`);
  return shade(i % SUIT.width, Math.floor(i / SUIT.width), 0, SUIT, { ...DEFAULTS, outfit });
}

test("outfit 0 leaves every pixel as it was drawn", () => {
  assert.deepEqual(whole(0), [...SUIT.index]);
});

test("a colour in the table becomes its partner", () => {
  assert.equal(becomes(6, 1), 12, "outfit 1: cream jacket to blue");
  assert.equal(becomes(6, 2), 11, "outfit 2: cream jacket to pink");
  assert.equal(becomes(8, 2), 5, "outfit 2: honey hat to white");
  assert.equal(becomes(13, 3), 25, "outfit 3: deep blue to night");
});

test("a colour that is in no pair keeps its own colour", () => {
  assert.equal(becomes(8, 1), 8, "outfit 1 leaves the hat");
  assert.equal(becomes(14, 1), 14, "outfit 1 leaves the veil");
  assert.equal(becomes(14, 2), 14, "outfit 2 leaves the veil");
  assert.equal(becomes(12, 2), 12, "outfit 2 leaves the trousers");
});

test("an empty pixel stays empty in every outfit", () => {
  for (let outfit = 0; outfit < 4; outfit++) assert.equal(shade(1, 3, 0, SUIT, { ...DEFAULTS, outfit }), 0, `outfit ${outfit}`);
});

test("two colours that trade places each change once, and not back", () => {
  assert.equal(becomes(6, 1), 12, "cream becomes blue and stays blue");
  assert.equal(becomes(12, 1), 6, "blue becomes cream");
  assert.equal(becomes(7, 1), 13, "sand becomes deep blue and stays deep blue");
  assert.equal(becomes(13, 1), 7, "deep blue becomes sand");
});

test("a colour that one pair puts in is not swapped again by a later pair", () => {
  assert.equal(becomes(6, 3), 7, "cream one step darker is sand, not wood");
  assert.equal(becomes(14, 3), 12, "light blue one step darker is blue, not night");
  assert.equal(becomes(8, 3), 9, "honey one step darker is amber, not wood");
});

test("the shape stays, and every pixel of one colour gets the same new colour", () => {
  for (let outfit = 0; outfit < 4; outfit++) {
    const after = whole(outfit);
    const map = new Map<number, number>();
    after.forEach((n, i) => {
      const was = SUIT.index[i];
      assert.equal(n === 0, was === 0, `outfit ${outfit}, pixel ${i}: drawn before and after, or empty before and after`);
      if (map.has(was)) assert.equal(n, map.get(was), `outfit ${outfit}: two pixels of ${was} came out differently`);
      map.set(was, n);
    });
  }
});
Was wir geprüft haben
  • 7 Tests bestehen mit der Regel.
  • Mit dem eingebauten Fehler schlagen 3 davon fehl: "a colour in the table becomes its partner", "two colours that trade places each change once, and not back", "a colour that one pair puts in is not swapped again by a later pair".
  • Der Fehler ist im Bild oben zu sehen: Schalte auf „Mit dem Fehler“.
  • Der Shader lief in einem Browser (Chrome/158.0.8127.37) auf 10 Bildern und ergab in 324.000 von 324.000 Pixeln das Bild der Regel.

Ausgeführt von pnpm courses:check am 2026-11-27 mit Node v24.15.0.

Probier es aus

Zum Nachlesen

Wo es tiefer geht: unsere Seiten, auf denen diese Lektion aufbaut, und was andere zu ihrem Punkt geschrieben haben.

Teste dich: drei Fragen

Jede Antwort wird sofort geprüft und erklärt. Die Fragen gehören zum Quiz und zählen für deinen Game Coder IQ wie jede andere.

Frage 1 von 3Leicht

Ein Palette Swap zieht der Imkerin andere Kleider an. Was ändert die Regel an ihrem Bild?

Alle Fragen dieses Kurses · Das Quiz und der Game Coder IQ

Nächste Lektion Outline-Shader für Pixel Art: ein Pixel rund um das Sprite

Gib deiner KI die ganze Lektion

Der Knopf kopiert, was du ihr sagst, zusammen mit der getesteten Regel, ihrem Test und beiden Shadern. So hat deine KI alles, um das in dein Spiel zu bauen und dir zu zeigen, dass es funktioniert.

Wie hilfreich war diese Seite?

Diese Seite hat noch niemand bewertet.

Ein Klick, ohne Konto. So sehen wir, welche Seiten wir als Nächstes verbessern.

Stand: 2026-11-27