CSS named colors — NamedColor.tomato, fromName, closestName
Shows the three ways the engine surfaces CSS Color Module Level 4 named colors: the `NamedColor.<name>` global (every CSS L4 colour resolved as a Pixel — usable anywhere a Color is accepted), `Pixel.fromName('rebeccapurple')` for dynamic string lookup, and `pixel.closestName()` for the reverse direction (matched in CIE-Lab perceptual space, so muted oranges resolve to "darksalmon" instead of "red"). The output tints the input with the chosen named colour, appends a four-swatch strip, and prints the closest-name reading of the original image's mean colour as a badge.
INPUT
Tinted + swatches
JavaScript
// CSS Color Module Level 4 named colors — three interlocking pieces
// demo_named_colors.js
//!INPUT: INPUT
//!OUTPUT: OUTPUT
//!PARAM: TINT_NAME:string=tomato
//!PARAM: TINT_STRENGTH:number=0.35,min=0.0,max=1.0
//!PARAM: SWATCH_HEIGHT:number=80,min=20,max=200
//!PARAM: LABEL_SIZE:number=22,min=8,max=48
// What this demonstrates:
//
// 1. `NamedColor.<name>` — global namespace with every CSS L4 colour
// as a fully-resolved Pixel. Use it anywhere
// a Color is accepted (tint, fill, drawText,
// pad, …) without a fromHex round-trip.
//
// 2. `Pixel.fromName(str)` — case-insensitive dynamic lookup. Same data,
// indexed by string. Useful when the colour
// name comes from a //!PARAM or an external
// source.
//
// 3. `pixel.closestName()` — reverse lookup in CIE-Lab perceptual space
// (CIE76 ΔE). Matches how a human would
// describe the colour, which sRGB Euclidean
// doesn't quite manage for muted shades.
//
// Output: the input image tinted with the chosen named colour, with a
// footer strip showing four named-colour swatches and the closest-name
// reading of the original image's mean colour at the top-left.
const img = Engine.loadImage(INPUT);
const W = img.width;
const H = img.height;
// ── (3) closestName — what does the engine think the input "looks like"? ──
//
// A heavy box blur collapses the image to a near-uniform colour, so
// the centre pixel approximates the perceptual mean cheaply (no
// forEachPixel pass over millions of pixels).
const sample = img.clone().boxBlur(Math.max(W, H) / 2);
const mean = sample.getPixel(px(W >> 1, H >> 1));
sample.free();
const meanName = mean.closestName();
const meanHex = mean.toHex();
// ── (1) NamedColor.<name> — apply tint via the global namespace ──
//
// `tint(color, strength)` accepts Pixel | string | [r,g,b]; using the
// Pixel from NamedColor saves a fromHex hop and is statically typed.
img.tint(NamedColor[TINT_NAME] || NamedColor.tomato, TINT_STRENGTH);
// ── Footer strip with 4 named swatches plus labels ───────────────────────
//
// Pad below the image, then paint solid-colour rectangles by drawing a
// 1×1 NamedColor pixel into a temporary canvas at full strip dimensions
// — overkill for a flat fill, but shows the namespace flowing into a
// canvas API too. Below them, a label printed in the contrasting colour.
const swatches = ["aliceblue", "rebeccapurple", "darkseagreen", "goldenrod"];
const cellW = Math.floor(W / swatches.length);
const stripY = H;
img.padBottom(SWATCH_HEIGHT, NamedColor.white);
for (let i = 0; i < swatches.length; i++) {
const name = swatches[i];
// (2) Pixel.fromName — dynamic lookup from the param string.
// Same Pixel that NamedColor[name] returns, just via the static API.
const colour = Pixel.fromName(name);
// Paint the swatch by drawing a temporary same-size canvas and
// blending it. Heavier than a flat fill needs to be, but it
// proves the Pixel from NamedColor flows straight into a
// CanvasHandle.fill(Color) call without any conversion dance.
const canvas = Engine.createCanvas(cellW, SWATCH_HEIGHT);
canvas.fill(colour);
const path = Engine.createPath().rect(0, 0, cellW, SWATCH_HEIGHT);
canvas.drawPath(path);
path.free();
const block = canvas.toImage();
canvas.free();
img.blendAt(block, px(i * cellW, stripY), 1.0, BlendMode.Over);
block.free();
// Label below the swatch: white on dark backgrounds, black on light.
const labelColour = (colour.r + colour.g + colour.b) / 3 < 0.55
? NamedColor.white
: NamedColor.black;
img.drawText(name, i * cellW + 8, stripY + LABEL_SIZE + 4, {
size: LABEL_SIZE,
color: labelColour,
});
}
// ── Top-left badge: the closest-name reading of the input ──
//
// Demonstrates closestName feeding straight back into a NamedColor
// lookup — no intermediate strings or hex dancing.
const badgeColour = NamedColor[meanName];
img.drawText(
`closest: ${meanName} (${meanHex})`,
16, LABEL_SIZE + 12,
{ size: LABEL_SIZE, color: badgeColour, stroke: NamedColor.black, strokeWidth: 1 },
);
img.save(OUTPUT);
img.free();
// © 2026 Michael Lechner · mlc OpticScript · https://mlcgo.eu · Elastic License 2.0