Skip to content

Commit

Permalink
Support both 8 and 16 color palettes in live streams
Browse files Browse the repository at this point in the history
  • Loading branch information
ku1ik committed Jan 11, 2025
1 parent 4258b00 commit 4e79944
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/driver/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,6 @@ function websocket(
}
}

const THEME_LEN = 54; // (2 + 16) * 3

function handleStreamMessage(event) {
const buffer = event.data;
const view = new DataView(buffer);
Expand All @@ -117,9 +115,18 @@ function websocket(
offset += 1;
let theme;

if (themeFormat === 1) {
theme = parseTheme(new Uint8Array(buffer, offset, THEME_LEN));
offset += THEME_LEN;
if (themeFormat === 8) {
const len = (2 + 8) * 3;
theme = parseTheme(new Uint8Array(buffer, offset, len));
offset += len;
} else if (themeFormat === 16) {
const len = (2 + 16) * 3;
theme = parseTheme(new Uint8Array(buffer, offset, len));
offset += len;
} else if (themeFormat !== 0) {
logger.warn(`unsupported theme format (${themeFormat})`);
socket.close();
return;
}

const initLen = view.getUint32(offset, true);
Expand Down Expand Up @@ -154,12 +161,13 @@ function websocket(
}

function parseTheme(arr) {
const colorCount = arr.length / 3;
const foreground = hexColor(arr[0], arr[1], arr[2]);
const background = hexColor(arr[3], arr[4], arr[5]);
const palette = [];

for (let i = 0; i < 16; i++) {
palette.push(hexColor(arr[i * 3 + 6], arr[i * 3 + 7], arr[i * 3 + 8]));
for (let i = 2; i < colorCount; i++) {
palette.push(hexColor(arr[i * 3], arr[i * 3 + 1], arr[i * 3 + 2]));
}

return { foreground, background, palette };
Expand Down

0 comments on commit 4e79944

Please sign in to comment.