feat: item color map, fix regex matching, fix sort order, fix resize handle
This commit is contained in:
35
web/lib/colors.ts
Normal file
35
web/lib/colors.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
export type ColorMap = Map<string, string>;
|
||||
|
||||
declare global { var __colorMapCache: ColorMap | undefined; }
|
||||
|
||||
export function parseColorCsv(text: string): ColorMap {
|
||||
const map: ColorMap = new Map();
|
||||
const lines = text.split(/\r?\n/).slice(1);
|
||||
for (const line of lines) {
|
||||
if (!line.trim()) continue;
|
||||
const [key, color] = line.split(',');
|
||||
if (key && color) map.set(key.trim(), color.trim());
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
export async function getColorMap(): Promise<ColorMap> {
|
||||
if (globalThis.__colorMapCache) return globalThis.__colorMapCache;
|
||||
try {
|
||||
const res = await fetch('/factorio_item_colors.csv');
|
||||
globalThis.__colorMapCache = res.ok ? parseColorCsv(await res.text()) : new Map();
|
||||
} catch {
|
||||
globalThis.__colorMapCache = new Map();
|
||||
}
|
||||
return globalThis.__colorMapCache;
|
||||
}
|
||||
|
||||
function djb2(s: string): number {
|
||||
let h = 5381;
|
||||
for (let i = 0; i < s.length; i++) h = (h * 33) ^ s.charCodeAt(i);
|
||||
return h >>> 0;
|
||||
}
|
||||
|
||||
export function getItemColor(key: string, colorMap: ColorMap): string {
|
||||
return colorMap.get(key) ?? `hsl(${djb2(key) % 360},70%,60%)`;
|
||||
}
|
||||
Reference in New Issue
Block a user