# Palette swap

> One drawn sprite, processed in code into other colours, an outline and a hit flash, with useLoadPixels and no extra picture drawn.

Example `palette-swap`, running at https://nacatamalon.com/en/examples/palette-swap. The whole catalogue: https://nacatamalon.com/llms/examples.md.

To just **show** a drawn picture, use `useLoadTexture`: that is the normal way, and the first gammix here is shown like that. `useLoadPixels` loads the same file **as pixels, to process it in code**: the game reads or changes the drawing before showing it.

`createPixelTexture(gammix, paint)` takes the loaded picture and a function that changes it, and hands back a texture at once that fills in when the file has arrived. The function works on **a copy**, so the drawing, shared by every scene that loads it, is never changed. In `art/variants.ts`:

- `recolor` swaps the body and hat colours with `swapColors`: a **palette swap**, the way the consoles of the era made enemies and second players from one drawing.
- `silhouette` turns every drawn pixel white with `mapPixels`: the flash of a hit.
- `outline` colours every empty pixel next to a drawn one.

**Click** to make the outlined gammix flash. The PNG is decoded in plain JavaScript, with no canvas, so this works the same on the native runtime.

## Code

`index.ts`:

```ts
import { createGame } from 'nacatamalon';

import * as Settings from './settings';
import { Game } from './scenes/demo';

const game = createGame('#app', {
    width: Settings.WIDTH,
    height: Settings.HEIGHT,
    background: '#141826',
});

// Declare Scenes
game({
    Game,
});
```

`art/variants.ts`:

```ts
import { getColor } from 'nacatamalon';
import { clonePixels, getPixel, mapPixels, swapColors } from 'nacatamalon/pixels';
import type { TColor } from 'nacatamalon';
import type { TPixelPaint } from 'nacatamalon/pixels';

// What the scene does to the drawn sprite, each as a function createPixelTexture applies to a copy of
// it once the file has arrived. The drawing itself is never changed.

const c = (hex: string) => getColor(hex);

// The two colours gammix is drawn with that the variants change: its body and its hat.
const BODY = c('#e36956');
const HAT = c('#243b61');

export const VARIANTS = [
    {
        name: 'GREEN',
        body: c('#4fb862'),
        hat: c('#1d4a2a')
    },
    {
        name: 'BLUE',
        body: c('#4a8fe0'),
        hat: c('#1a2a6b')
    },
    {
        name: 'GOLD',
        body: c('#e8c24a'),
        hat: c('#6b3a12')
    },
];

// A palette swap: the same drawing in other colours, the way the consoles of the era made enemies and
// second players.
export const recolor = (body: TColor, hat: TColor): TPixelPaint => (pixels) => swapColors(pixels, [[BODY, body], [HAT, hat]]);

// Every drawn pixel white: the flash of a hit.
export const silhouette: TPixelPaint = (pixels) =>
    mapPixels(pixels, (color) => (color.a > 0 ? c('#ffffff') : color));

// A one-pixel line around the drawing: every empty pixel next to a drawn one takes the colour.
export const outline = (color: TColor): TPixelPaint => (pixels) => {
    const shape = clonePixels(pixels);

    const touches = (x: number, y: number): boolean =>
        getPixel(shape, x + 1, y).a > 0 || getPixel(shape, x - 1, y).a > 0 || getPixel(shape, x, y + 1).a > 0 || getPixel(shape, x, y - 1).a > 0;

    return mapPixels(pixels, (current, x, y) => (current.a === 0 && touches(x, y) ? color : current));
};
```

`components/gallery.ts`:

```ts
import { createSprite, createText, getColor, useLoadTexture, usePointer, useUpdate } from 'nacatamalon';
import { createPixelTexture, useLoadPixels } from 'nacatamalon/pixels';
import * as Settings from '../settings';
import { outline, recolor, silhouette, VARIANTS } from '../art/variants';

export const Gallery = () => {
    const label = (text: string, x: number, y: number) =>
        createText({
            text,
            tint: '#8fb7ff',
            transform: {
                x,
                y
            }
        });

    // The drawing, shown as it is.
    createSprite({
        texture: useLoadTexture({
            src: Settings.SRC
        }),
        width: Settings.SIZE,
        height: Settings.SIZE,
        transform: {
            x: 44,
            y: 70
        }
    });
    label('DRAWN', 24, 108);

    // The same file, loaded to be processed.
    const gammix = useLoadPixels({
        src: Settings.SRC
    });

    VARIANTS.forEach((variant, i) => {
        const x = 124 + i * 76;

        createSprite({
            texture: createPixelTexture(gammix, recolor(variant.body, variant.hat)),
            width: Settings.SIZE,
            height: Settings.SIZE,
            transform: {
                x,
                y: 70
            }
        });
        label(variant.name, x - variant.name.length * 4, 108);
    });

    const outlined = createPixelTexture(gammix, outline(getColor('#ffd75a')));
    const flash = createPixelTexture(gammix, silhouette);

    const hero = createSprite({
        texture: outlined,
        width: Settings.SIZE,
        height: Settings.SIZE,
        transform: {
            x: 160,
            y: 160
        }
    });

    label('OUTLINE  CLICK = HIT', 80, 198);

    let flashing = 0;

    usePointer().onDown(() => {
        flashing = 0.12;
    });
    useUpdate((delta) => {
        flashing = Math.max(0, flashing - delta);
        hero.texture = flashing > 0 ? flash : outlined;
    });
};
```

`scenes/demo.ts`:

```ts
import { createScene, useSpawn } from 'nacatamalon';

import { Gallery } from '../components/gallery';

export const Game = () => {
    useSpawn(Gallery)();

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 320;
export const HEIGHT = 224;
export const SRC = '/example-assets/assets/gammix.png';
export const SIZE = 64;
```
