-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpalettes.js
More file actions
55 lines (49 loc) · 1.73 KB
/
Copy pathpalettes.js
File metadata and controls
55 lines (49 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Cache for bundled palettes (loaded once)
let bundledPalettes = null;
const loadBundledPalettes = async () => {
if (bundledPalettes) return bundledPalettes;
try {
const response = await fetch(import.meta.env.BASE_URL + 'palettes/bundled.json');
if (!response.ok) throw new Error('Bundled palettes not found');
bundledPalettes = await response.json();
return bundledPalettes;
} catch {
bundledPalettes = {};
return bundledPalettes;
}
};
export const fetchLospecPalette = async (name) => {
try {
// Check bundled palettes first
const bundled = await loadBundledPalettes();
if (bundled[name]) return bundled[name];
// Fall back to fetching from Lospec
const baseUrl = import.meta.env.DEV
? '/lospec-api'
: 'https://corsproxy.io/?https://lospec.com';
const response = await fetch(`${baseUrl}/palette-list/${name}.hex`);
if (!response.ok) throw new Error('Palette not found');
const text = await response.text();
return parseHexFile(text);
} catch (error) {
console.error('Error fetching palette:', error);
return null;
}
};
export const parseHexFile = (text) => {
return text
.split('\n')
.map(line => line.trim())
.filter(line => line && !line.startsWith(';')) // Lospec .hex files sometimes have comments
.map(hex => hex.startsWith('#') ? hex : `#${hex}`);
};
export const getPopularPaletteNames = async () => {
try {
const response = await fetch(import.meta.env.BASE_URL + 'lospec-palettes.json');
if (!response.ok) throw new Error('Local palette list not found');
return await response.json();
} catch (error) {
console.error('Error loading palette list:', error);
return ['pico-8', 'sweetie-16', 'aap-64', 'dawnbringer-32'];
}
};