From 85d4f4840cb39105a38f6dfa69197714dc0e89e0 Mon Sep 17 00:00:00 2001 From: Sameh Khamis Date: Sat, 7 Nov 2026 22:21:29 -0800 Subject: [PATCH] Read CSS colour names as the default renderer reads them cssColor (src/render/color.js) now reads the colour names the default renderer knows, in any case, upper-cased as it looks them up: its table, with grey aliases, transparent and its lavendar_blush spelling, and no rebeccapurple. The local GeoJSON layers read simplestyle fill, stroke and marker colours through it, so a named colour no longer falls back to the layer colour; submarine cables read their marker colours the same way. --- docs/INFRASTRUCTURE-LAYERS.md | 10 +- src/data/localGeojsonDraw.test.mjs | 36 +++++++ src/render/color.js | 165 ++++++++++++++++++++++++++++- src/render/color.test.mjs | 32 ++++++ 4 files changed, 236 insertions(+), 7 deletions(-) diff --git a/docs/INFRASTRUCTURE-LAYERS.md b/docs/INFRASTRUCTURE-LAYERS.md index 9a5590ec6..239e1d31f 100644 --- a/docs/INFRASTRUCTURE-LAYERS.md +++ b/docs/INFRASTRUCTURE-LAYERS.md @@ -58,10 +58,12 @@ an error naming it, as it does for a dataset it cannot load. A selected feature is the context record its draw item carries as `meta`; the layer no longer sets the viewer's `selectedEntity`. Features keep their own simplestyle `fill`, `fill-opacity`, `stroke`, -`stroke-opacity`, `stroke-width`, `marker-color` and `marker-size`, given as hex, -`rgb()` or `hsl()` colours; a `marker-symbol` draws the plain pin. Polygons -with heights are drawn at them, outlined; points with heights stand their pin -on the stem's tip. Edges follow rhumb lines. +`stroke-opacity`, `stroke-width`, `marker-color` and `marker-size`, given as +hex, `rgb()` or `hsl()` colours or by the CSS colour names the default renderer +knows (it has no `rebeccapurple`, and spells `lavenderblush` `lavendar_blush`); +a `marker-symbol` draws the plain pin. Polygons with heights are drawn at them, +outlined; points with heights stand their pin on the stem's tip. Edges follow +rhumb lines. `gods-eye-view/infrastructure/geojson` exports the lower-level `createLocalGeoJsonLayer(options, services)` and existing infrastructure overlay diff --git a/src/data/localGeojsonDraw.test.mjs b/src/data/localGeojsonDraw.test.mjs index 70d208384..841d67055 100644 --- a/src/data/localGeojsonDraw.test.mjs +++ b/src/data/localGeojsonDraw.test.mjs @@ -220,7 +220,43 @@ test('features keep their own simplestyle colours, widths and marker sizes', asy { type: 'Point', coordinates: [0.002, 0.003] }, { 'marker-color': '#ffffff', 'marker-size': 'large' }, ), + // CSS colour names, as the loader reads them. + feature( + 'named', + { type: 'Polygon', coordinates: square(0.04, 0) }, + { fill: 'Red' }, + ), + feature( + 'named-line', + { + type: 'LineString', + coordinates: [ + [0, 0], + [0, 0.01], + ], + }, + { stroke: 'navy' }, + ), + feature( + 'named-pin', + { type: 'Point', coordinates: [0.004, 0.003] }, + { 'marker-color': 'white' }, + ), ]); + assert.deepEqual( + env.fake.set('fills').items.get('named').fill, + [1, 0, 0, 0.3], + ); + assert.deepEqual(env.fake.set('lines').items.get('named-line').style.color, [ + 0, + 0, + 128 / 255, + 1, + ]); + assert.match( + env.fake.set('pins').items.get('named-pin').image.svg, + /fill="rgba\(255,255,255,1\)"/, + ); assert.deepEqual(env.fake.set('fills').items.get('p').fill, [1, 0, 0, 0.5]); // `fill` alone keeps the layer's fill alpha. assert.deepEqual(env.fake.set('fills').items.get('q').fill, [0, 1, 0, 0.3]); diff --git a/src/render/color.js b/src/render/color.js index c31d313ff..12d3a0e77 100644 --- a/src/render/color.js +++ b/src/render/color.js @@ -31,16 +31,175 @@ function hueToRgb(m1, m2, h) { return m1; } +/** + * The CSS colour names the default renderer knows, upper-cased, as `rrggbb` + * (`transparent` aside). It has no `rebeccapurple`, and spells + * `lavenderblush` `lavendar_blush`. + */ +const NAMED = Object.freeze({ + ALICEBLUE: 'f0f8ff', + ANTIQUEWHITE: 'faebd7', + AQUA: '00ffff', + AQUAMARINE: '7fffd4', + AZURE: 'f0ffff', + BEIGE: 'f5f5dc', + BISQUE: 'ffe4c4', + BLACK: '000000', + BLANCHEDALMOND: 'ffebcd', + BLUE: '0000ff', + BLUEVIOLET: '8a2be2', + BROWN: 'a52a2a', + BURLYWOOD: 'deb887', + CADETBLUE: '5f9ea0', + CHARTREUSE: '7fff00', + CHOCOLATE: 'd2691e', + CORAL: 'ff7f50', + CORNFLOWERBLUE: '6495ed', + CORNSILK: 'fff8dc', + CRIMSON: 'dc143c', + CYAN: '00ffff', + DARKBLUE: '00008b', + DARKCYAN: '008b8b', + DARKGOLDENROD: 'b8860b', + DARKGRAY: 'a9a9a9', + DARKGREEN: '006400', + DARKGREY: 'a9a9a9', + DARKKHAKI: 'bdb76b', + DARKMAGENTA: '8b008b', + DARKOLIVEGREEN: '556b2f', + DARKORANGE: 'ff8c00', + DARKORCHID: '9932cc', + DARKRED: '8b0000', + DARKSALMON: 'e9967a', + DARKSEAGREEN: '8fbc8f', + DARKSLATEBLUE: '483d8b', + DARKSLATEGRAY: '2f4f4f', + DARKSLATEGREY: '2f4f4f', + DARKTURQUOISE: '00ced1', + DARKVIOLET: '9400d3', + DEEPPINK: 'ff1493', + DEEPSKYBLUE: '00bfff', + DIMGRAY: '696969', + DIMGREY: '696969', + DODGERBLUE: '1e90ff', + FIREBRICK: 'b22222', + FLORALWHITE: 'fffaf0', + FORESTGREEN: '228b22', + FUCHSIA: 'ff00ff', + GAINSBORO: 'dcdcdc', + GHOSTWHITE: 'f8f8ff', + GOLD: 'ffd700', + GOLDENROD: 'daa520', + GRAY: '808080', + GREEN: '008000', + GREENYELLOW: 'adff2f', + GREY: '808080', + HONEYDEW: 'f0fff0', + HOTPINK: 'ff69b4', + INDIANRED: 'cd5c5c', + INDIGO: '4b0082', + IVORY: 'fffff0', + KHAKI: 'f0e68c', + LAVENDER: 'e6e6fa', + LAVENDAR_BLUSH: 'fff0f5', + LAWNGREEN: '7cfc00', + LEMONCHIFFON: 'fffacd', + LIGHTBLUE: 'add8e6', + LIGHTCORAL: 'f08080', + LIGHTCYAN: 'e0ffff', + LIGHTGOLDENRODYELLOW: 'fafad2', + LIGHTGRAY: 'd3d3d3', + LIGHTGREEN: '90ee90', + LIGHTGREY: 'd3d3d3', + LIGHTPINK: 'ffb6c1', + LIGHTSEAGREEN: '20b2aa', + LIGHTSKYBLUE: '87cefa', + LIGHTSLATEGRAY: '778899', + LIGHTSLATEGREY: '778899', + LIGHTSTEELBLUE: 'b0c4de', + LIGHTYELLOW: 'ffffe0', + LIME: '00ff00', + LIMEGREEN: '32cd32', + LINEN: 'faf0e6', + MAGENTA: 'ff00ff', + MAROON: '800000', + MEDIUMAQUAMARINE: '66cdaa', + MEDIUMBLUE: '0000cd', + MEDIUMORCHID: 'ba55d3', + MEDIUMPURPLE: '9370db', + MEDIUMSEAGREEN: '3cb371', + MEDIUMSLATEBLUE: '7b68ee', + MEDIUMSPRINGGREEN: '00fa9a', + MEDIUMTURQUOISE: '48d1cc', + MEDIUMVIOLETRED: 'c71585', + MIDNIGHTBLUE: '191970', + MINTCREAM: 'f5fffa', + MISTYROSE: 'ffe4e1', + MOCCASIN: 'ffe4b5', + NAVAJOWHITE: 'ffdead', + NAVY: '000080', + OLDLACE: 'fdf5e6', + OLIVE: '808000', + OLIVEDRAB: '6b8e23', + ORANGE: 'ffa500', + ORANGERED: 'ff4500', + ORCHID: 'da70d6', + PALEGOLDENROD: 'eee8aa', + PALEGREEN: '98fb98', + PALETURQUOISE: 'afeeee', + PALEVIOLETRED: 'db7093', + PAPAYAWHIP: 'ffefd5', + PEACHPUFF: 'ffdab9', + PERU: 'cd853f', + PINK: 'ffc0cb', + PLUM: 'dda0dd', + POWDERBLUE: 'b0e0e6', + PURPLE: '800080', + RED: 'ff0000', + ROSYBROWN: 'bc8f8f', + ROYALBLUE: '4169e1', + SADDLEBROWN: '8b4513', + SALMON: 'fa8072', + SANDYBROWN: 'f4a460', + SEAGREEN: '2e8b57', + SEASHELL: 'fff5ee', + SIENNA: 'a0522d', + SILVER: 'c0c0c0', + SKYBLUE: '87ceeb', + SLATEBLUE: '6a5acd', + SLATEGRAY: '708090', + SLATEGREY: '708090', + SNOW: 'fffafa', + SPRINGGREEN: '00ff7f', + STEELBLUE: '4682b4', + TAN: 'd2b48c', + TEAL: '008080', + THISTLE: 'd8bfd8', + TOMATO: 'ff6347', + TURQUOISE: '40e0d0', + VIOLET: 'ee82ee', + WHEAT: 'f5deb3', + WHITE: 'ffffff', + WHITESMOKE: 'f5f5f5', + YELLOW: 'ffff00', + YELLOWGREEN: '9acd32', +}); + const rgbChannel = (text) => Number.parseFloat(text) / (text.endsWith('%') ? 100 : 255); /** - * A CSS colour (`#rgb`, `#rgba`, `#rrggbb`, `#rrggbbaa`, `rgb()`, `rgba()`, - * `hsl()`, `hsla()`) as a renderer colour, read as the default renderer - * reads it; null for anything else (named colours included). + * A CSS colour (a name the default renderer knows, in any case, `#rgb`, + * `#rgba`, `#rrggbb`, `#rrggbbaa`, `rgb()`, `rgba()`, `hsl()`, `hsla()`) as + * a renderer colour, read as the default renderer reads it; null for + * anything else. */ export function cssColor(text) { const css = String(text ?? '').trim(); + // Upper-cased, as the default renderer looks its names up. + const name = css.toUpperCase(); + if (name === 'TRANSPARENT') return [0, 0, 0, 0]; + if (Object.hasOwn(NAMED, name)) return hexColor(`#${NAMED[name]}`); let m = SHORT_HEX.exec(css); if (m) return [ diff --git a/src/render/color.test.mjs b/src/render/color.test.mjs index b8cf3c68e..117e13e01 100644 --- a/src/render/color.test.mjs +++ b/src/render/color.test.mjs @@ -22,11 +22,26 @@ test('CSS colours read as the default renderer reads them', async () => { 'hsl(120, 50%, 25%)', 'hsla(300, 100%, 75%, 0.4)', 'hsl(30, 0%, 40%)', + 'red', + ' Navy ', + 'DARKGREY', + 'lightSlateGray', + 'transparent', + // Upper-cased as the default renderer does: dotless i and long s + // upper-case to I and S. + 'wh\u0131te', + '\u017Fnow', ]) { const c = Cesium.Color.fromCssColorString(css); assert.deepEqual(cssColor(css), [c.red, c.green, c.blue, c.alpha], css); } assert.equal(cssColor('not a colour'), null); + assert.equal(cssColor('rebeccapurple'), null); + assert.equal(cssColor('lavenderblush'), null); + assert.equal(cssColor('constructor'), null); + assert.equal(cssColor('__proto__'), null); + // The Kelvin sign lower-cases to k, but does not upper-case to K. + assert.equal(cssColor('\u212Ahaki'), null); assert.equal(cssColor(undefined), null); }); @@ -44,3 +59,20 @@ test('HSL colours read as the default renderer computes them', async () => { assert.deepEqual(hslColor(h, s, l, a), [c.red, c.green, c.blue, c.alpha]); } }); + +test('every colour name the default renderer knows reads as it does', async () => { + const Cesium = await import('cesium'); + const { cssColor } = await import('./color.js'); + const names = Object.keys(Cesium.Color).filter( + (key) => /^[A-Z_]+$/.test(key) && Cesium.Color[key] instanceof Cesium.Color, + ); + assert.ok(names.length > 140); + for (const name of names) { + const c = Cesium.Color.fromCssColorString(name.toLowerCase()); + assert.deepEqual( + cssColor(name.toLowerCase()), + [c.red, c.green, c.blue, c.alpha], + name, + ); + } +});