# Colour adjust

> colorAdjust() is brightness, contrast, saturation, gamma, hue and tint in one effect, each starting where it changes nothing.

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

`colorAdjust()` is six uniforms over the finished frame: `brightness`, `contrast`, `saturation`, `gamma`, `hue` (in degrees) and `tint` (red, green and blue multiplied separately). Each starts where it changes nothing, so it is safe to put in a chain and turn one number at a time.

A **look** is only a set of those numbers. This example keeps one effect and writes a different set into its `uniforms` on each click, so changing the look installs nothing and compiles nothing.

**Click** to change look and **Space** to switch it off. It goes early in a chain: grade first, and let a palette or a dither decide afterwards which of the graded colours the machine could show.

## Code

`index.ts`:

```ts
import { createGame } from 'nacatamalon';
import { Game } from './scenes/demo';
import { WIDTH, HEIGHT } from './settings';

const game = createGame('#app', {
    width: WIDTH,
    height: HEIGHT,
    background: '#0b0710'
});

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

`components/effect_controls.ts`:

```ts
import { createText, useKeyboard, useLoadBitmapFont, usePointer, useUpdate, useScreenSpace } from 'nacatamalon';
import { colorAdjust, usePostProcess } from 'nacatamalon/effects';

import { LOOKS, EFFECT_SETTINGS } from '../settings';

export const EffectControls = () => {
    useScreenSpace();

    const font = useLoadBitmapFont({
        json: '/example-assets/fonts/bitmaps/nacatamal-arcade/nacatamal-arcade.json',
        atlas: '/example-assets/fonts/bitmaps/nacatamal-arcade/nacatamal-arcade.png',
    });

    const label = createText({
        text: '',
        font,
        style: {
            fontSize: 8,
        },
        tint: '#ffffff',
        transform: {
            x: 12,
            y: 14,
        },
    });

    createText({
        text: 'CLICK LOOK   SPACE OFF',
        font,
        style: {
            fontSize: 8,
        },
        tint: '#8c82a6',
        transform: {
            x: 12,
            y: 30,
        },
    });

    const effect = usePostProcess(colorAdjust(EFFECT_SETTINGS));

    let look = 0;

    const show = (name = LOOKS[look].name) => {
        if (!effect.enabled) {
            label.text = 'OFF  THE FRAME AS DRAWN';

            return;
        }

        label.text = name;
    };

    show();

    usePointer().onDown(() => {
        look = (look + 1) % LOOKS.length;

        const { name, ...values } = LOOKS[look];

        Object.assign(effect.uniforms, values);
        show(name);
    });

    const keys = useKeyboard();

    useUpdate(() => {
        // Switched off, not removed: it keeps its place in the chain for when it comes back.
        if (keys.justPressed(' ')) {
            effect.enabled = !effect.enabled;
            show();
        }
    });
};
```

`components/stage.ts`:

```ts
import { WIDTH, HEIGHT } from '../settings';
import { createMaterial, createMesh, createSprite, getColor, useCamera3d, useDirectionalLight, useLoadTexture, useUpdate, useSphereGeometry } from 'nacatamalon';

// The test card every post-processing example draws, so the only thing that changes between them
// is the effect. Each part is here because it shows something a screen effect does:
//
//   lit balls   smooth gradients that nobody drew, made by the light as it turns. Banding shows
//               there first, because a gradient is exactly what a few steps cannot hold.
//   hue ramp    every colour at once, which is where a palette has to make its hardest choices.
//   grey ramp   sixteen even steps: whether an effect keeps them even is visible at a glance.
//   gammix      pixel art, already limited, so it shows what an effect does to a sprite that was
//               drawn for a small palette in the first place.

export const Stage = () => {
    useCamera3d({
        projection: 'perspective',
        fov: 50,
        transform: {
            z: 5,
            y: 0.4
        },
    });

    const light = useDirectionalLight({
        color: '#ffffff',
        intensity: 1.2,
        ambient: 0.25,
        transform: {
            rotationX: -0.5
        },
    });

    const geometry = useSphereGeometry();

    ['#ff7a3d', '#3dd6c6', '#9b5cff'].forEach((hex, i) => {
        const material = createMaterial({
            name: 'post-card-' + i,
            shader: 'mesh3d',
            tint: getColor(hex),
            specular: '#ffffff',
            shininess: 24,
        });

        createMesh({
            geometry,
            material,
            transform: {
                x: (i - 1) * 2.1,
                y: 0.6,
            },
        });
    });

    // The light swings from side to side in front of the balls, so the gradients move and a
    // static effect cannot hide in one lucky frame.
    useUpdate((_, time) => {
        light.transform.rotationY = Math.sin(time * 0.8) * 0.9;
    });

    for (let i = 0; i < 48; i++) {
        createSprite({
            width: WIDTH / 48,
            height: 18,
            tint: getColor('hsl(' + Math.round((i / 48) * 360) + ', 85%, 55%)'),
            transform: {
                x: (i + 0.5) * WIDTH / 48,
                y: HEIGHT - 38,
            },
        });
    }

    for (let i = 0; i < 16; i++) {
        const v = Math.round((i / 15) * 255);

        createSprite({
            width: WIDTH / 16,
            height: 18,
            tint: getColor('rgb(' + v + ',' + v + ',' + v + ')'),
            transform: {
                x: (i + 0.5) * WIDTH / 16,
                y: HEIGHT - 18,
            },
        });
    }

    const gammix = useLoadTexture({
        src: '/example-assets/assets/gammix.png',
        key: 'gammix',
    });

    createSprite({
        texture: gammix,
        width: 48,
        height: 48,
        transform: {
            x: WIDTH - 34,
            y: 34,
        },
    });
};
```

`scenes/demo.ts`:

```ts
import { createScene, useSpawn } from 'nacatamalon';
import { Stage } from '../components/stage';
import { EffectControls } from '../components/effect_controls';

export const Game = () => {
    useSpawn(Stage)();
    useSpawn(EffectControls)();

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 480;

export const HEIGHT = 300;

// colorAdjust() is six uniforms over the finished frame, each starting where it changes nothing:
//
//   brightness  multiplies every colour
//   contrast    pushes colours away from the middle grey, or pulls them towards it
//   saturation  0 is black and white; above 1 is louder than the art
//   gamma       above 1 lifts the shadows, below 1 sinks them
//   hue         turns every colour round the colour wheel, in degrees
//   tint        multiplies red, green and blue separately
//
// A look is only a set of those numbers. Each one here is written into the same effect, so
// changing the look installs nothing.

export const LOOKS = [
    {
        name: 'AS DRAWN',
        brightness: 1,
        contrast: 1,
        saturation: 1,
        gamma: 1,
        hue: 0,
        tint: [1, 1, 1]
    },
    {
        name: 'BLACK AND WHITE',
        brightness: 1,
        contrast: 1.1,
        saturation: 0,
        gamma: 1,
        hue: 0,
        tint: [1, 1, 1]
    },
    {
        name: 'OLD PHOTO',
        brightness: 1.05,
        contrast: 0.9,
        saturation: 0.25,
        gamma: 1,
        hue: 0,
        tint: [1, 0.86, 0.66]
    },
    {
        name: 'NIGHT',
        brightness: 0.6,
        contrast: 1,
        saturation: 0.5,
        gamma: 0.9,
        hue: 0,
        tint: [0.7, 0.85, 1.2]
    },
    {
        name: 'HEAT',
        brightness: 1,
        contrast: 1.2,
        saturation: 1.3,
        gamma: 1,
        hue: 0,
        tint: [1.15, 0.95, 0.75]
    },
    {
        name: 'OPPOSITE HUES',
        brightness: 1,
        contrast: 1,
        saturation: 1,
        gamma: 1,
        hue: 180,
        tint: [1, 1, 1]
    },
];
export const EFFECT_SETTINGS = {
};
```
