pixel/js/palette.js
2026-06-04 16:40:45 +09:00

58 lines
2.5 KiB
JavaScript

(function () {
'use strict';
const root = window.PixelIslandModules ||= {};
const BASE_COLOR_CODES = '0123456789abcdefghij';
const ADVANCED_COLOR_CODES = 'klmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' + "!#$%&'()*+,-/:;<=>?@[]^_`{|}~";
const COLOR_CODES = BASE_COLOR_CODES + ADVANCED_COLOR_CODES;
const BASIC_PALETTE_COUNT = BASE_COLOR_CODES.length;
const DEFAULT_SELECTED_COLOR_CODE = BASE_COLOR_CODES.includes('a') ? 'a' : (BASE_COLOR_CODES[0] || '0');
function hslToHex(h, s, l) {
s /= 100; l /= 100;
const k = (n) => (n + h / 30) % 12;
const a = s * Math.min(l, 1 - l);
const f = (n) => l - a * Math.max(-1, Math.min(k(n) - 3, Math.min(9 - k(n), 1)));
const toHex = (value) => Math.round(255 * value).toString(16).padStart(2, '0');
return `#${toHex(f(0))}${toHex(f(8))}${toHex(f(4))}`;
}
function buildPalette() {
const basicColors = [
'#111827', '#374151', '#6b7280', '#d1d5db', '#fff7ed',
'#7f1d1d', '#dc2626', '#f97316', '#facc15', '#84cc16',
'#16a34a', '#14b8a6', '#06b6d4', '#2563eb', '#4f46e5',
'#7c3aed', '#c026d3', '#ec4899', '#f5d0a9', '#7c4a2d'
];
const advancedColors = [
'#fafafa', '#f5f5f4', '#e7e5e4', '#d6d3d1', '#a8a29e', '#78716c', '#57534e', '#292524', '#0c0a09',
'#fef3c7', '#fde68a', '#d6a25f', '#9a6a3a', '#5c3b24',
'#fee2e2', '#fca5a5', '#ef4444', '#b91c1c', '#7f1d1d',
'#ffedd5', '#fdba74', '#f97316', '#c2410c', '#7c2d12',
'#fef9c3', '#fde047', '#eab308', '#a16207', '#713f12',
'#ecfccb', '#bef264', '#84cc16', '#4d7c0f', '#365314', '#dcfce7', '#86efac', '#15803d',
'#ccfbf1', '#5eead4', '#14b8a6', '#0f766e', '#134e4a',
'#cffafe', '#67e8f9', '#22d3ee', '#06b6d4', '#0e7490', '#e0f2fe', '#38bdf8',
'#dbeafe', '#93c5fd', '#3b82f6', '#2563eb', '#1d4ed8', '#1e3a8a',
'#e0e7ff', '#a5b4fc', '#6366f1', '#4f46e5', '#3730a3', '#ede9fe', '#c4b5fd', '#8b5cf6',
'#fae8ff', '#e879f9', '#c026d3', '#86198f', '#fce7f3', '#f9a8d4', '#ec4899', '#be185d'
];
const colors = [...basicColors, ...advancedColors];
return COLOR_CODES.split('').map((code, index) => ({ code, color: colors[index] || hslToHex((index * 47) % 360, 72, 58) }));
}
const PALETTE = buildPalette();
const PALETTE_BY_CODE = Object.fromEntries(PALETTE.map((p) => [p.code, p.color]));
root.Palette = {
BASE_COLOR_CODES,
ADVANCED_COLOR_CODES,
COLOR_CODES,
BASIC_PALETTE_COUNT,
DEFAULT_SELECTED_COLOR_CODE,
PALETTE,
PALETTE_BY_CODE,
buildPalette,
hslToHex
};
})();