Four common edits, one simple idea
Turning a photo black and white, making a negative, lightening a dark shot, or adding punch to a flat one. These are some of the most common image edits people make.
They feel like "photo editing", so it is easy to assume a server or a big app has to do them. In fact, each one is a short loop over a list of numbers. Your browser already has that list once it has opened the image.
I built four small browser tools for these edits: grayscale, invert colours, brightness and contrast. In this article I show the one idea they share, the exact formula each one uses, and where the approach stops being enough.
An image is just a long list of numbers
When you draw an image on a <canvas> and call getImageData() , you get an ImageData object. Its data property is a Uint8ClampedArray : one flat list with four numbers per pixel.
data[i] : Meaning: Red. Range: 0 to 255.
data[i + 1] : Meaning: Green. Range: 0 to 255.
data[i + 2] : Meaning: Blue.β¦