Nur die Palette: ein Farbton-Shader, der Pixel-Art-Farben behält
Die Regeln bisher haben mit Zahlen geantwortet, die sie im Bild oder in einer Tabelle fanden. Diese muss eine Farbe ausrechnen: Sie mischt jede Farbe einer Szene mit einem Farbton und antwortet trotzdem mit einer Farbe der Palette. Dafür sucht sie die nächste Farbe der Palette in Rot, Grün und Blau, und genau diesen letzten Schritt macht eine KI am häufigsten falsch.

Die Regel mischt jede Farbe in Rot, Grün und Blau mit Tiefblau und nimmt dann die Farbe der Palette, die am nächsten liegt. Der Strand wird kalt, und trotzdem bleibt der Baum grün und das Meer blau, bis das Tiefblau überhandnimmt.
Auch in dieser Lektion 3 Lesetipps zum Vertiefen 3 Fragen zum Selbsttest
Die Idee
Ein Farbton legt eine Farbe über eine ganze Szene: das kalte Blau eines Winterabends oder von tiefem Wasser, Rot, wenn die Spielfigur verletzt ist, Braun für eine alte Erinnerung. Auf einem gewöhnlichen Bildschirm mischt ein Farbton jede Farbe mit seiner eigenen und zeigt die Mischung, und die kann jede von Millionen Farben sein. In Pixel Art gehört die Palette zum Look. Eine Szene, die mit 25 Farben gezeichnet ist und plötzlich Hunderte Zwischenfarben zeigt, sieht nicht mehr nach Pixel Art aus.
Also mischt diese Regel und geht dann zurück zur Palette: Sie nimmt die Farbe der Palette, die der Mischung am nächsten liegt. Die Szene nimmt den Farbton an, und jeder Pixel ist weiter eine der 25.
So funktioniert es
Jede Farbe der Palette sind drei ganze Zahlen: wie viel Rot, wie viel Grün und wie viel Blau sie hat, jeweils von 0 bis 255. Das Grün des Baumes ist 15, 138, 76; Bernstein ist 224, 138, 46. Die Regel hat alle 25 Farben in einer Tabelle.
Zum Mischen nimmt sie so viele Teile von 100 vom Farbton und den Rest von der eigenen Farbe des Pixels, für Rot, Grün und Blau einzeln, und rundet ab. Ein Fünftel Bernstein auf dem Grün des Baumes ergibt 56, 138, 70: Das Rot kommt aus 80 Teilen von 15 und 20 Teilen von 224, und so weiter.
Um zur Palette zurückzukommen, misst sie, wie weit die Mischung von jeder der 25 Farben entfernt ist: der Unterschied im Rot zum Quadrat, plus der Unterschied im Grün zum Quadrat, plus der Unterschied im Blau zum Quadrat. Die kleinste Summe gewinnt. Durch die Quadrate zählt ein großer Unterschied mehr als mehrere kleine, die zusammen genauso viel ergeben, und es braucht keine Wurzel. Sind zwei Farben genau gleich weit weg, gewinnt die niedrigere Nummer, in der Regel wie im Shader, also geben beide dasselbe Bild.
Ein Regler stellt ein, wie viel vom Farbton beigemischt wird, von 0 bis 100; er beginnt bei 30. Der andere wählt den Farbton über seine Nummer in der Palette; er beginnt bei 13, Tiefblau.
Was schiefgeht
Viele Paletten-Shader vergleichen nur die Helligkeit. Sie rechnen aus, wie hell die Mischung ist, und nehmen die Farbe der Palette, deren Helligkeit am nächsten liegt. Das ist weniger Arbeit, und so arbeiten Shader für Bildschirme mit vier Abstufungen einer einzigen Farbe, wo es nur Helligkeit gibt. Bei einer Palette mit vielen Farbtönen geht das schief, denn die Helligkeit sagt nichts darüber, ob eine Farbe grün oder violett ist.
Nimm noch einmal das Fünftel Bernstein auf dem Grün. Die Mischung, 56, 138, 70, ist immer noch deutlich grün, und die Regel findet Grün. Nach Helligkeit allein ist die nächste Farbe dieser Palette Violett.
Schalte das Bild auf „Mit dem Fehler“. Während der Farbton steigt, wird der Himmel gelb, das Meer grün, der Sand erst blau und dann grün, und die Honiggläser werden grün. Jeder Pixel behält ungefähr seine Helligkeit und verliert seinen Farbton.
Drei Tests fangen das: Ein Fünftel Bernstein auf Grün muss grün bleiben; der Himmel mit 30 Teilen Violett muss Hellblau werden, nicht Rosa; und eine Mischung genau in der Mitte zwischen Grauviolett und Rosa muss die niedrigere Nummer nehmen. Der Fehler antwortet mit Violett, Rosa und Bernstein.
Der Fehler, Zeile für Zeile
- /* 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;Was der Test zum Fehler sagt
✔ 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 failedDas sagst du deiner KI
Schreib einen Shader für mein Spiel, der das ganze Bild zu einem Farbton hin färbt und jeden Pixel in meiner Palette hält.
- Meine Palette hat [Anzahl] Farben. Hier sind sie als Rot, Grün und Blau von 0 bis 255: [die Liste]. Leg sie im Shader als Tabelle aus ganzen Zahlen an.
- Für jeden Pixel: Misch seine Farbe mit dem Farbton, [30] Teile von 100 vom Farbton und den Rest von der Farbe des Pixels, für Rot, Grün und Blau einzeln, abgerundet. Such dann die Farbe meiner Palette, die der Mischung am nächsten liegt: die kleinste Summe der quadrierten Unterschiede in Rot, Grün und Blau. Bei Gleichstand nimm die Farbe, die in der Tabelle zuerst kommt. Zeichne diese Farbe.
- Vergleiche nie nur nach Helligkeit, und zeichne nie die Mischung selbst.
- Wie viel vom Farbton und welcher Farbton müssen Einstellungen sein, die ich aus dem Code des Spiels ändern kann.
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) und einen Platz und gibt die Zahl für diesen einen Pixel zurück. Teste sie: Ohne Farbton bleibt jede Farbe sie selbst; mit dem ganzen Farbton wird jede Farbe zum Farbton; ein Grün mit ein wenig einer warmen Farbe darin ist immer noch ein Grün; eine Mischung genau in der Mitte zwischen zwei meiner Farben nimmt die, die zuerst kommt. 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
/* 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;
}
Dieselbe Regel als 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);
}
Dieselbe Regel für 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);
}
Der 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 bestehen mit der Regel.
- Mit dem eingebauten Fehler schlagen 3 davon fehl: "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".
- 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 12 Bildern und ergab in 388.800 von 388.800 Pixeln das Bild der Regel.
Probier es aus
- Lass den Farbton auf 13 und schieb „wie viel vom Farbton“ langsam von 0 bis 100. Der Baum auf der Düne bleibt grün, lange nachdem die roten Streifen des linken Häuschens braun geworden sind, und der Honigstand wird kurz rosa und dann grauviolett. Die Palette entscheidet, wohin eine Farbe gehen kann: Sie hat keinen Honig, der vom Blau abgekühlt ist.
- Stell den Farbton auf 10, Korallrot, für das Rot der Gefahr, oder auf 20, Holz, für eine alte Erinnerung. Mit Holz wird der Himmel erst sandfarben und dann grau, wie auf einem alten Foto.
- Schalte auf „Mit dem Fehler“, halte das Bild an ein paar Stellen an und vergleiche es mit „Mit der Regel“. Sieh dir Meer und Sand an: Mit der Regel kühlen sie gemeinsam ab; mit dem Fehler springen sie zu anderen Farben.
Zum Nachlesen
Wo es tiefer geht: unsere Seiten, auf denen diese Lektion aufbaut, und was andere zu ihrem Punkt geschrieben haben.
Tag-und-Nacht-Shader für Pixel Art: dunkler nach der Uhrzeit
Eine ganze Szene, die ihre Farben ändert, ohne überhaupt zu mischen: Die Nacht kommt, indem jede Farbe in gerasterten Stufen zu ihren dunkleren Nachbarn der Palette geht, während Lampen und Fenster leuchten.
Ein Farbton ohne Palette, zum Vergleich: das Licht der Stunde über der ganzen Welt, gemischt in einem Farbraum, der sich nach dem Auge richtet, damit das Morgenrot klar bleibt, statt grau zu werden.
Warum die einfache Summe der quadrierten Unterschiede in Rot, Grün und Blau nicht ganz so vergleicht wie das Auge, und eine günstige gewichtete Summe für die nächste Farbe einer Palette, deren Code mit ganzen Zahlen auskommt.
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.
Ein Farbton mischt jede Farbe einer Szene mit einer Farbe. Was macht die Regel dieser Lektion mit der Mischung, bevor sie antwortet?
Nächste Lektion Stimmt das? Einen Pixel-Art-Shader prüfen, Pixel für Pixel
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.
Das ist eine Lektion aus dem Kurs „Shader für Pixel Art“ auf spielerando.com: https://spielerando.com/de/courses/pixel-shaders/only-the-palette
Schreib einen Shader für mein Spiel, der das ganze Bild zu einem Farbton hin färbt und jeden Pixel in meiner Palette hält.
- Meine Palette hat [Anzahl] Farben. Hier sind sie als Rot, Grün und Blau von 0 bis 255: [die Liste]. Leg sie im Shader als Tabelle aus ganzen Zahlen an.
- Für jeden Pixel: Misch seine Farbe mit dem Farbton, [30] Teile von 100 vom Farbton und den Rest von der Farbe des Pixels, für Rot, Grün und Blau einzeln, abgerundet. Such dann die Farbe meiner Palette, die der Mischung am nächsten liegt: die kleinste Summe der quadrierten Unterschiede in Rot, Grün und Blau. Bei Gleichstand nimm die Farbe, die in der Tabelle zuerst kommt. Zeichne diese Farbe.
- Vergleiche nie nur nach Helligkeit, und zeichne nie die Mischung selbst.
- Wie viel vom Farbton und welcher Farbton müssen Einstellungen sein, die ich aus dem Code des Spiels ändern kann.
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) und einen Platz und gibt die Zahl für diesen einen Pixel zurück. Teste sie: Ohne Farbton bleibt jede Farbe sie selbst; mit dem ganzen Farbton wird jede Farbe zum Farbton; ein Grün mit ein wenig einer warmen Farbe darin ist immer noch ein Grün; eine Mischung genau in der Mitte zwischen zwei meiner Farben nimmt die, die zuerst kommt. Zeig mir die Ausgabe des Tests. Schreib dann den Shader so, dass er genau das tut, was die Funktion tut.
Die Regel, getestet (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;
}
```
Ihr 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);
});
```
Dieselbe Regel als GLSL-Shader (WebGL 2, OpenGL ES 3.0), Pixel für Pixel gegen die Regel geprüft:
```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);
}
```
Dieselbe Regel für Godot 4 (von den Autoren nicht ausgeführt):
```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);
}
```
Wie hilfreich war diese Seite?
Diese Seite hat noch niemand bewertet.