Skip to content

simulateBuffer() ​

Applies colorblindness simulation to an RGBA pixel buffer in-place. This is the core function for image processing.

Signature ​

ts
function simulateBuffer(
  pixels: Uint8Array | Uint8ClampedArray,
  type: ColorblindType,
  options?: SimulateOptions,
): void

Mutates the input buffer. If you need to preserve the original, clone it first.

Parameters ​

ParameterTypeRequiredDescription
pixelsUint8Array | Uint8ClampedArrayYesRGBA pixel buffer. Length must be a multiple of 4.
typeColorblindTypeYesDeficiency type to simulate
options.modelColorblindModelNo"machado" (default), "vienot", or "brettel"
options.severitynumberNo0–1, default 1. Clamped to valid range.

Examples ​

Canvas ImageData ​

ts
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 ​

ts
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) ​

ts
const { data, info } = await sharp('photo.jpg')
  .ensureAlpha()
  .raw()
  .toBuffer({ resolveWithObject: true })

// Buffer extends Uint8Array — works directly
simulateBuffer(data, 'tritanopia', { model: 'brettel' })

With severity ​

ts
simulateBuffer(pixels, 'protanomaly', { severity: 0.5 })

Behavior ​

  • Alpha channel is preserved — only R, G, B bytes are modified (every 4th byte is skipped)
  • When type is "none" or severity is 0, 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:

ResolutionPixelsViénotMachadoBrettel
640×4800.3M~80ms~85ms~100ms
1920×10802.1M~590ms~560ms~660ms
3840×21608.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).

Released under the MIT License.