simulateBuffer()
Applies colorblindness simulation to an RGBA pixel buffer in-place. This is the core function for image processing.
Signature
function simulateBuffer(
pixels: Uint8Array | Uint8ClampedArray,
type: ColorblindType,
options?: SimulateOptions,
): voidMutates the input buffer. If you need to preserve the original, clone it first.
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
pixels | Uint8Array | Uint8ClampedArray | Yes | RGBA pixel buffer. Length must be a multiple of 4. |
type | ColorblindType | Yes | Deficiency type to simulate |
options.model | ColorblindModel | No | "machado" (default), "vienot", or "brettel" |
options.severity | number | No | 0–1, default 1. Clamped to valid range. |
Examples
Canvas ImageData
import { simulateBuffer } from 'chromanopia'
const ctx = canvas.getContext('2d')
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height)
simulateBuffer(imageData.data, 'protanopia')
ctx.putImageData(imageData, 0, 0)Preserving original
const original = ctx.getImageData(0, 0, canvas.width, canvas.height)
const copy = new ImageData(
new Uint8ClampedArray(original.data),
original.width,
original.height,
)
simulateBuffer(copy.data, 'deuteranopia')
ctx.putImageData(copy, 0, 0)Node.js Buffer (Sharp)
const { data, info } = await sharp('photo.jpg')
.ensureAlpha()
.raw()
.toBuffer({ resolveWithObject: true })
// Buffer extends Uint8Array — works directly
simulateBuffer(data, 'tritanopia', { model: 'brettel' })With severity
simulateBuffer(pixels, 'protanomaly', { severity: 0.5 })Behavior
- Alpha channel is preserved — only R, G, B bytes are modified (every 4th byte is skipped)
- When
typeis"none"orseverityis0, returns immediately (no processing) - For matrix models (Viénot, Machado): sRGB → linear → matrix multiply → gamut map → sRGB
- For Brettel: sRGB → linear → XYZ → xyY projection → XYZ → linear → sRGB
- For Brettel with achromatopsia/achromatomaly: falls back to Machado matrix
Performance
The function processes 4 bytes at a time with no allocations inside the hot loop. Performance scales linearly with pixel count. In a warm JavaScript engine, expect roughly:
| Resolution | Pixels | Viénot | Machado | Brettel |
|---|---|---|---|---|
| 640×480 | 0.3M | ~80ms | ~85ms | ~100ms |
| 1920×1080 | 2.1M | ~590ms | ~560ms | ~660ms |
| 3840×2160 | 8.3M | ~2.4s | ~2.2s | ~2.6s |
One reference point — Apple M1, Node 22, warm JIT. All three models run at roughly ~3.5 Mpix/s; Brettel's per-pixel CIE xyY projection is only ~15–20% slower than the matrix path, not the several× gap one might expect from its accuracy advantage.
TIP
These are CPU/JavaScript timings. For real-time image processing, a WebGL shader runs the same matrix on the GPU at 10–100× the throughput. The numbers above are fine for one-shot transforms (e.g. processing an uploaded image once).