MCPcopy Create free account
hub / github.com/32comic/image2pixel-web / parseGplPalette

Function parseGplPalette

web/js/main.js:648–694  ·  view source on GitHub ↗
(text, name)

Source from the content-addressed store, hash-verified

646}
647
648function parseGplPalette(text, name) {
649 const lines = text.split(/\r?\n/);
650 const colors = [];
651 const meta = { brand: null, codeMap: {} };
652 let lastColorKey = null;
653
654 for (const line of lines) {
655 const trimmed = line.trim();
656
657 if (trimmed.startsWith('# META:')) {
658 const content = trimmed.substring(7).trim();
659 const eqIndex = content.indexOf('=');
660 if (eqIndex > 0) {
661 const key = content.substring(0, eqIndex).trim();
662 const value = content.substring(eqIndex + 1).trim();
663 if (key === 'brand') meta.brand = value;
664 }
665 continue;
666 }
667
668 if (trimmed.startsWith('# CODE:')) {
669 const code = trimmed.substring(7).trim();
670 if (lastColorKey) meta.codeMap[lastColorKey] = code;
671 continue;
672 }
673
674 if (!trimmed || trimmed.startsWith('#') || trimmed.toLowerCase().startsWith('gimp palette') || trimmed.toLowerCase().startsWith('name:') || trimmed.toLowerCase().startsWith('columns:')) continue;
675
676 const parts = trimmed.split(/\s+/);
677 if (parts.length >= 3) {
678 const r = Math.min(255, Math.max(0, parseInt(parts[0], 10) || 0));
679 const g = Math.min(255, Math.max(0, parseInt(parts[1], 10) || 0));
680 const b = Math.min(255, Math.max(0, parseInt(parts[2], 10) || 0));
681 colors.push([r, g, b]);
682 lastColorKey = `${r},${g},${b}`;
683 }
684 }
685 if (colors.length < 2) throw new Error('Palette must contain at least 2 colors');
686
687 const hasMeta = meta.brand || Object.keys(meta.codeMap).length > 0;
688 return {
689 id: name.replace(/\.[^.]+$/, ''),
690 displayName: name.replace(/\.[^.]+$/, ''),
691 colors,
692 ...(hasMeta ? { meta } : {}),
693 };
694}
695
696function parseJsonPalette(text, name) {
697 let data;

Callers 1

parsePaletteFileFunction · 0.85

Calls 1

splitMethod · 0.45

Tested by

no test coverage detected