31 lines
1.1 KiB
JavaScript
31 lines
1.1 KiB
JavaScript
import Phaser from '../vendor/phaser.js';
|
|
|
|
/**
|
|
* Converts a config color (number or CSS string like '#1b2540')
|
|
* into the integer format Phaser expects.
|
|
*/
|
|
export function toColor(value, fallback = 0xffffff) {
|
|
if (typeof value === 'number' && Number.isFinite(value)) {
|
|
return value >>> 0;
|
|
}
|
|
if (typeof value === 'string' && value.length > 0) {
|
|
const color = Phaser.Display.Color.ValueToColor(value.trim());
|
|
if (color) return color.color;
|
|
}
|
|
return fallback;
|
|
}
|
|
|
|
/**
|
|
* Converts a color (number or CSS string) to a '#rrggbb' CSS string.
|
|
*
|
|
* Phaser v4 (4.2.1 Giedi) quirk: Text styles are written straight to the
|
|
* canvas — `fillStyle = style.color` — so a numeric color (e.g. 0xeaf6ff)
|
|
* is an *invalid* fillStyle and the text silently renders black. Canvas-
|
|
* backed text (add.text / TextStyle) must therefore always get CSS strings;
|
|
* keep toColor for Graphics/shape APIs that want numbers.
|
|
*/
|
|
export function toCss(value, fallback = '#ffffff') {
|
|
const n = toColor(value, toColor(fallback));
|
|
return '#' + (n >>> 0).toString(16).padStart(6, '0');
|
|
}
|