Color & palettes
Back to the reference

Color & palettes

Colour values, indexed palettes and grading tables: the indexed-colour half of the retro look.

9 symbols

consoleColorfunctionnacatamalon

consoleColor(color: TColorOption, levels: number) => TColor

A colour as a console could show it: each channel moved to the nearest of the steps that machine had, POST_EFFECTS.COLOR_LEVELS.genesis (8) for a Mega Drive, POST_EFFECTS.COLOR_LEVELS.snes (32) for a SNES.

Why it matters under dither: the effect leaves a colour that is on one of its steps flat, and puts its pattern on everything in between, because in between is what a gradient is. A tile tinted '#e8423f' sits between steps, so it comes out checkered; consoleColor('#e8423f', POST_EFFECTS.COLOR_LEVELS.genesis) is #db4949, on the steps, and comes out flat, while a sky's gradient still gets the pattern. A screen effect cannot make that choice: it sees pixels, not which ones were meant to be flat.

Takes what getColor takes, or a colour already made. Alpha is left as it is, and the colour given is never changed.

Parameters

colorTColorOption

A colour: a hex number or string, any CSS colour, or a TColor.

levelsnumber

Steps per channel, from POST_EFFECTS.COLOR_LEVELS or any number from 2 up.

Example

usePostProcess(dither({ levels: POST_EFFECTS.COLOR_LEVELS.genesis }));
createSprite({ width: 16, height: 16, tint: consoleColor('#e8423f', POST_EFFECTS.COLOR_LEVELS.genesis) });
View source · color/console_color.ts:32

fromHslfunctionnacatamalon

fromHsl(h: number, s: number, l: number, a: number) => TColor

Converts HSL values to a TColor.

Useful for animated effects that cycle hue over time (rainbow cascades, palette shifts, hit-flashes): it runs as pure arithmetic, so it is safe to call inside useUpdate every frame without performance concerns. Prefer it to fromCss('hsl(...)') per frame: that one parses a string first, which is work this does not do.

Parameters

hnumber

Hue in degrees [0, 360).

snumber

Saturation in [0, 1]. Defaults to 1 (fully saturated).

lnumber

Lightness in [0, 1]. Defaults to 0.5 (mid tone).

anumber

Alpha in [0, 1]. Defaults to 1 (fully opaque).

Example

declare const sprite: TSprite;

// Rainbow hue cycle in useUpdate:
let time = 0;
useUpdate((delta) => {
    time += delta;
    sprite.tint = fromHsl((time * 60) % 360);
});
View source · color/from_hsl.ts:34

getColorfunctionnacatamalon

getColor(input: TColorOption) => TColor

A colour as the engine keeps it, { r, g, b, a } from 0 to 1, from a hex number, a hex string, any CSS colour, or a TColor. Always a new one, a TColor given included: a copy of it.

Every option that takes a colour reads it through here, so tint: '#ff6b1a' and tint: getColor('#ff6b1a') are the same thing, and whatever is made keeps a colour of its own. One colour kept in a constant can tint many texts, and fading one of them (who.tint.a = 0.3) fades that one: not the others, nor the constant.

Parameters

inputTColorOption

A colour: a hex number, a hex string, any CSS colour, or a TColor.

View source · color/get_color.ts:46

lerpColorfunctionnacatamalon

lerpColor(a: TColor, b: TColor, t: number) => TColor

Linearly interpolates two colors channel by channel (straight RGBA, not gamma corrected): t = 0 returns a, t = 1 returns b. The built-in for color fades and tinted hit-flashes: feed it the eased value from a tween's onUpdate. GLSL calls this mix.

Parameters

aTColor

The colour at 0.

bTColor

The colour at 1.

tnumber

How far from a to b.

View source · color/lerp_color.ts:18

useColorfunctionhooknacatamalon

useColor(input: TColorInput) => TColor

Converts any color format to a normalized TColor object. Accepts hex numbers (0xff0000), hex strings ("#ff0000", "#f00"), and any valid CSS color string ("skyblue", "rgb(255,0,0)", "hsl(0,100%,50%)").

Parameters

inputTColorInput

A colour: a hex number, a hex string, or any CSS colour.

View source · hooks/color/use_color.ts:15

TColorInputtypenacatamalon

TColorInput: number | TCssNamedColor | string & { … }

Accepted input formats for useColor/getColor.

TCssNamedColor | (string & {}) is a TS trick: it gives editor autocomplete for the named CSS colors while still accepting any other string (hex, rgb(), hsl(), ...) without a type error: string & {} stops TS from widening the union to plain string, which would otherwise discard the literal suggestions.

View source · color/get_color.ts:19

TColorOptiontypenacatamalon

TColorOption: TColorInput | TColor

Any colour an option takes: what getColor reads ('#ff6b1a', 0xff6b1a, 'tomato', 'rgb(…)') or a TColor already made. What it becomes is always a TColor, which is what is read back.

View source · color/get_color.ts:29

TCssNamedColortypenacatamalon

TCssNamedColor: 'aliceblue' | 'antiquewhite' | 'aqua' | 'aquamarine' | 'azure' | 'beige' | 'bisque' | 'black' | 'blanchedalmond' | 'blue' | 'blueviolet' | 'brown' | 'burlywood' | 'cadetblue' | 'chartreuse' | 'chocolate' | 'coral' | 'cornflowerblue' | 'cornsilk' | 'crimson' | 'cyan' | 'darkblue' | 'darkcyan' | 'darkgoldenrod' | 'darkgray' | 'darkgreen' | 'darkgrey' | 'darkkhaki' | 'darkmagenta' | 'darkolivegreen' | 'darkorange' | 'darkorchid' | 'darkred' | 'darksalmon' | 'darkseagreen' | 'darkslateblue' | 'darkslategray' | 'darkslategrey' | 'darkturquoise' | 'darkviolet' | 'deeppink' | 'deepskyblue' | 'dimgray' | 'dimgrey' | 'dodgerblue' | 'firebrick' | 'floralwhite' | 'forestgreen' | 'fuchsia' | 'gainsboro' | 'ghostwhite' | 'gold' | 'goldenrod' | 'gray' | 'green' | 'greenyellow' | 'grey' | 'honeydew' | 'hotpink' | 'indianred' | 'indigo' | 'ivory' | 'khaki' | 'lavender' | 'lavenderblush' | 'lawngreen' | 'lemonchiffon' | 'lightblue' | 'lightcoral' | 'lightcyan' | 'lightgoldenrodyellow' | 'lightgray' | 'lightgreen' | 'lightgrey' | 'lightpink' | 'lightsalmon' | 'lightseagreen' | 'lightskyblue' | 'lightslategray' | 'lightslategrey' | 'lightsteelblue' | 'lightyellow' | 'lime' | 'limegreen' | 'linen' | 'magenta' | 'maroon' | 'mediumaquamarine' | 'mediumblue' | 'mediumorchid' | 'mediumpurple' | 'mediumseagreen' | 'mediumslateblue' | 'mediumspringgreen' | 'mediumturquoise' | 'mediumvioletred' | 'midnightblue' | 'mintcream' | 'mistyrose' | 'moccasin' | 'navajowhite' | 'navy' | 'oldlace' | 'olive' | 'olivedrab' | 'orange' | 'orangered' | 'orchid' | 'palegoldenrod' | 'palegreen' | 'paleturquoise' | 'palevioletred' | 'papayawhip' | 'peachpuff' | 'peru' | 'pink' | 'plum' | 'powderblue' | 'purple' | 'rebeccapurple' | 'red' | 'rosybrown' | 'royalblue' | 'saddlebrown' | 'salmon' | 'sandybrown' | 'seagreen' | 'seashell' | 'sienna' | 'silver' | 'skyblue' | 'slateblue' | 'slategray' | 'slategrey' | 'snow' | 'springgreen' | 'steelblue' | 'tan' | 'teal' | 'thistle' | 'tomato' | 'turquoise' | 'violet' | 'wheat' | 'white' | 'whitesmoke' | 'yellow' | 'yellowgreen' | 'transparent' | 'currentcolor'

Standard CSS Level 4 named colors. Listed as a string-literal union purely so editors autocomplete them as useColor/getColor arguments: fromCss (and the browser's CSS parser it relies on) accepts any of these plus the rest of the CSS color syntax (rgb(), hsl(), hex, etc.) regardless of this list.

View source · color/css_named_colors.ts:11