# Tint and colour

> A sprite's tint is multiplied into its picture every frame: the basis for hit flashes and fades.

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

A sprite's `tint` is a colour multiplied into what it draws. The top row colourizes one texture five ways; the middle row has no texture, so the tint paints flat squares.

The one at the bottom is a hit flash: `useTween` carries a number from 0 to 1 and back, and `lerpColor` reads that number as how much red is mixed in. The tween never learns what a colour is, which is exactly why the two compose.

## Code

`index.ts`:

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

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

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

`components/hit_flash.ts`:

```ts
import { createSprite, getColor, lerpColor, useTween } from 'nacatamalon';
import type { TTexture } from 'nacatamalon';
import { easeInOutSine } from 'nacatamalon/math';
import { FLASH, SWATCH } from '../settings';

export const HitFlash = (texture: TTexture) => {
    const tween = useTween();
    const calm = getColor(FLASH.calm);
    const hurt = getColor(FLASH.hurt);

    const sprite = createSprite({
        texture,
        width: SWATCH.size,
        height: SWATCH.size,
        transform: {
            x: FLASH.x,
            y: FLASH.y
        }
    });

    // The tween supplies a number; lerpColor turns that number into a colour.
    tween({
        from: 0,
        to: 1,
        duration: FLASH.seconds,
        ease: easeInOutSine,
        yoyo: true,
        loop: true,
        onUpdate: (mix) => {
            sprite.tint = lerpColor(calm, hurt, mix);
        }
    }).play();
};
```

`components/tint_row.ts`:

```ts
import { createSprite } from 'nacatamalon';
import type { TTexture } from 'nacatamalon';
import { COLORS, SWATCH } from '../settings';

type TTintRowProps = {
    texture?: TTexture;
    y: number;
};

export const TintRow = ({ texture, y }: TTintRowProps) => {
    // White leaves a texture unchanged. With no texture, the tint paints a flat square.
    COLORS.forEach((tint, index) => {
        createSprite({
            texture,
            tint,
            width: SWATCH.size,
            height: SWATCH.size,
            transform: {
                x: SWATCH.startX + index * SWATCH.gap,
                y
            }
        });
    });
};
```

`scenes/demo.ts`:

```ts
import { createScene, useLoadTexture, useSpawn } from 'nacatamalon';
import { TintRow } from '../components/tint_row';
import { HitFlash } from '../components/hit_flash';

export const Demo = () => {
    const texture = useLoadTexture({
        src: '/example-assets/assets/gammix.png'
    });

    const spawnRow = useSpawn(TintRow);

    spawnRow({
        texture,
        y: 44
    });
    spawnRow({
        y: 138
    });
    useSpawn(HitFlash)(texture);

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 480;
export const HEIGHT = 320;
export const COLORS = ['#ffffff', '#ff5566', '#55dd77', '#5599ff', '#ffcc33'];

export const SWATCH = {
    size: 56,
    startX: 36,
    gap: 84
};

export const FLASH = {
    x: 212,
    y: 236,
    calm: '#ffffff',
    hurt: '#ff3344',
    seconds: 0.5
};
```
