-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(actions): implement get/save colors
- Loading branch information
1 parent
598e099
commit 51ac6d1
Showing
5 changed files
with
135 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { fileOpen } from '../web_modules/browser-nativefs.js'; | ||
import { updateContext } from '../helpers/update-context.js'; | ||
|
||
export class GetColorsAction { | ||
async execute(drawingContext) { | ||
const file = await fileOpen({ extensions: ['pal'] }); | ||
const buffer = await file.arrayBuffer(); | ||
const dataView = new DataView(buffer); | ||
const textDecoder = new TextDecoder(); | ||
|
||
// RIFF header | ||
const header = textDecoder.decode(buffer.slice(0, 4)); | ||
if (header !== 'RIFF') { | ||
alert('Non-RIFF palettes are not supported.'); | ||
return; | ||
} | ||
|
||
// PAL form type | ||
const formType = textDecoder.decode(buffer.slice(8, 12)); | ||
if (formType !== 'PAL ') { | ||
alert('Only PAL form types are supported.'); | ||
return; | ||
} | ||
|
||
// Data chunk | ||
const chunkType = textDecoder.decode(buffer.slice(12, 16)); | ||
if (chunkType !== 'data') { | ||
alert('Expected a data chunk.'); | ||
return; | ||
} | ||
|
||
// LOGPALETTE | ||
const palette = []; | ||
const count = dataView.getUint16(22, true); | ||
for (let i = 0; i < count; i++) { | ||
const offset = 24 + i * 4; | ||
|
||
// PALETTEENTRY | ||
const r = dataView.getUint8(offset); // peRed | ||
const g = dataView.getUint8(offset + 1); // peGreen | ||
const b = dataView.getUint8(offset + 2); // peBlue | ||
// peFlags skipped | ||
|
||
palette.push(`rgb(${r} ${g} ${b})`); | ||
} | ||
|
||
palette.slice(0, 26) | ||
.forEach((color, index) => drawingContext.palette[index] = color); | ||
updateContext(drawingContext.element); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { fileSave } from '../web_modules/browser-nativefs.js'; | ||
|
||
export class SaveColorsAction { | ||
async execute({ palette }) { | ||
const count = palette.length; | ||
const chunkSize = 4 + count * 4; | ||
const size = 24 + count * 4; | ||
const buffer = new ArrayBuffer(size); | ||
const uint8View = new Uint8Array(buffer); | ||
const dataView = new DataView(buffer); | ||
const textEncoder = new TextEncoder(); | ||
|
||
// RIFF header | ||
uint8View.set(textEncoder.encode('RIFF')); | ||
dataView.setUint32(4, size - 8, true); | ||
|
||
// PAL form type | ||
uint8View.set(textEncoder.encode('PAL '), 8); | ||
|
||
// Data chunk | ||
uint8View.set(textEncoder.encode('data'), 12); // ckID | ||
dataView.setUint32(16, chunkSize, true); // ckSize | ||
|
||
// LOGPALETTE | ||
dataView.setUint16(20, 0x300, true); // palVersion | ||
dataView.setUint16(22, count, true); // palNumEntries | ||
|
||
const canvas = document.createElement('canvas'); | ||
const context = canvas.getContext('2d'); | ||
for (let i = 0; i < count; i++) { | ||
context.fillStyle = palette[i]; | ||
context.fillRect(0, 0, 1, 1); | ||
const [r, g, b] = context.getImageData(0, 0, 1, 1).data; | ||
|
||
const offset = 24 + i * 4; | ||
// PALETTEENTRY | ||
dataView.setUint8(offset, r); // peRed | ||
dataView.setUint8(offset + 1, g); // peGreen | ||
dataView.setUint8(offset + 2, b); // peBlue | ||
dataView.setUint8(offset + 3, 0); // peFlags | ||
} | ||
|
||
const blob = new Blob([buffer]); | ||
await fileSave(blob, { fileName: 'untitled.pal' }); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters