From 15c71e28c80d70d1e18ac8124de5217c944513d5 Mon Sep 17 00:00:00 2001 From: Anthony Kim Date: Tue, 27 Jan 2026 19:51:16 -0800 Subject: [PATCH] Replace more magic numbers with readable symbols --- .../src/KittyGraphicsAddon.ts | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/addons/addon-kitty-graphics/src/KittyGraphicsAddon.ts b/addons/addon-kitty-graphics/src/KittyGraphicsAddon.ts index 1b7cb843..30e321a4 100644 --- a/addons/addon-kitty-graphics/src/KittyGraphicsAddon.ts +++ b/addons/addon-kitty-graphics/src/KittyGraphicsAddon.ts @@ -65,12 +65,17 @@ const enum KittyKey { ROWS = 'r', // More data flag (1=more chunks coming, 0=final chunk) MORE = 'm', - // Compression type (z=zlib) + // Compression type (z=zlib). This is essential for chunking larger images. COMPRESSION = 'o', // Quiet mode (1=suppress OK responses, 2=suppress error responses) QUIET = 'q' } +// Pixel format constants +const BYTES_PER_PIXEL_RGB = 3; +const BYTES_PER_PIXEL_RGBA = 4; +const ALPHA_OPAQUE = 255; + /** * Parsed Kitty graphics command. */ @@ -401,7 +406,7 @@ export class KittyGraphicsAddon implements ITerminalAddon, IKittyGraphicsApi { throw new Error('Width and height required for raw pixel data'); } - const bytesPerPixel = format === KittyFormat.RGBA ? 4 : 3; + const bytesPerPixel = format === KittyFormat.RGBA ? BYTES_PER_PIXEL_RGBA : BYTES_PER_PIXEL_RGB; const expectedBytes = width * height * bytesPerPixel; if (bytes.length < expectedBytes) { @@ -414,14 +419,14 @@ export class KittyGraphicsAddon implements ITerminalAddon, IKittyGraphicsApi { for (let i = 0; i < width * height; i++) { const srcOffset = i * bytesPerPixel; - const dstOffset = i * 4; + const dstOffset = i * BYTES_PER_PIXEL_RGBA; data[dstOffset] = bytes[srcOffset]; // R data[dstOffset + 1] = bytes[srcOffset + 1]; // G data[dstOffset + 2] = bytes[srcOffset + 2]; // B data[dstOffset + 3] = format === KittyFormat.RGBA ? bytes[srcOffset + 3] // A from source - : 255; // Fully opaque for RGB + : ALPHA_OPAQUE; // Fully opaque for RGB } return createImageBitmap(imageData); @@ -616,7 +621,7 @@ export class KittyGraphicsAddon implements ITerminalAddon, IKittyGraphicsApi { return true; } - const bytesPerPixel = format === KittyFormat.RGBA ? 4 : 3; + const bytesPerPixel = format === KittyFormat.RGBA ? BYTES_PER_PIXEL_RGBA : BYTES_PER_PIXEL_RGB; const expectedBytes = width * height * bytesPerPixel; if (bytes.length < expectedBytes) {