Minimal browser-first utilities for inverting RGBA pixel data, designed for the tooling behind https://invertcolor.app.
npm install @invertcolor/invertcolorProvide your own image loading and canvas setup, then pass the resulting ImageData (or raw Uint8ClampedArray) to the helpers.
import { invertImageData, invertPixels } from '@invertcolor/invertcolor';
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
ctx.drawImage(myImageElement, 0, 0);
const data = ctx.getImageData(0, 0, canvas.width, canvas.height);
const inverted = invertImageData(data); // returns new ImageData
ctx.putImageData(inverted, 0, 0);
// Or work with pixel buffers directly
const invertedBuffer = invertPixels(data.data);Both invertImageData and invertPixels accept an optional { mutate?: boolean } flag.
mutate: truewill modify the provided buffer/ImageData in place.- The default (
false) returns a cloned buffer/ImageData, leaving the original untouched.
pixels:Uint8ClampedArrayrepresenting RGBA data.options.mutate: whentrue, inversion happens on the provided array.
Returns: Uint8ClampedArray containing the inverted pixel values.
imageData:ImageDataretrieved by the caller (e.g. viaCanvasRenderingContext2D#getImageData).options.mutate: whentrue, the providedImageDatais mutated.
Returns: ImageData with inverted colors.
MIT © invertcolor.app