DEV Community

Nathan Cole
Nathan Cole

Posted on

Cutting out a background colour with a canvas pixel loop: what I learned building Color to Transparent

A few months ago I needed one boring thing: a logo on a white background that had to sit on a transparent one. Every online tool I found wanted an account, a watermark, or an upload of a file I would rather not send anywhere. So I wrote the loop myself, and it turned into Color to Transparent, a free browser tool that removes one chosen colour from an image and makes it transparent.

The core is shorter than you think

Every pixel is four numbers in a Uint8ClampedArray. Once you have the background colour you want gone, the rest is a distance check:

function keyOut(ctx, width, height, target, tolerance) {
  const img = ctx.getImageData(0, 0, width, height);
  const d = img.data;
  const [tr, tg, tb] = target;
  const limit = tolerance * tolerance * 3;

  for (let i = 0; i < d.length; i += 4) {
    const dr = d[i] - tr, dg = d[i + 1] - tg, db = d[i + 2] - tb;
    if (dr * dr + dg * dg + db * db <= limit) d[i + 3] = 0;
  }
  ctx.putImageData(img, 0, 0);
}
Enter fullscreen mode Exit fullscreen mode

That is the whole idea. Everything else in the tool exists because the naive version looks bad.

Four details that decide whether it looks finished

Picking the colour from the edge. Asking people to type a HEX value is friction. A click on the image is faster, so the tool reads the pixel under the pointer and uses it as the target — and it can sample from the border by default, because the background is usually what touches the frame.

Tolerance needs to be a small number and a big one. A pure white background is not uniform once a JPEG has touched it: compression leaves bands and ringing. Too little tolerance leaves a soft grey halo; too much eats into the subject. A 0–100 slider that maps to a squared RGB distance is easy to reason about for both the user and the code.

Halo cleanup is separate from keying. Pixels blended between subject and background keep a hint of the old colour. Removing the old colour from them is a blending problem, not a threshold problem, and it is worth doing as a second pass.

The connected-area option saves a lot of retries. If a logo also happens to contain the background colour — white letters on a white page, for instance — a global key destroys them. Restricting the removal to the region connected to the click keeps the interior intact.

Per-pixel work belongs on the main thread only briefly

A 6,000 px image is 36 million pixels. For loops over typed arrays on that scale are fast enough in practice, but they are still long enough to drop frames. What helped most was keeping the preview at screen resolution and applying the same settings to the full-size image only when exporting, so nothing feels laggy while you drag a slider.

That is also why the files never leave the device: the browser reads the image, runs the loop, and hands back a PNG or WebP with an alpha channel via canvas.toBlob(). No queue, no storage, nothing to clean up.

What I would tell anyone building the same thing

  • Put the tolerance slider and the colour picker side by side; people adjust them together.
  • Offer an undo brush early. One bad tolerance value is usually all it takes.
  • Export PNG by default. There is no point in a transparent background you cannot keep.
  • Test with a JPEG, not a PNG. Real-world white is almost never white.

The tool itself is free, needs no sign-up and has no watermark, so if you want to see the finished version of this loop you can try it at colorremoverfromimage.com. If you are writing your own keying code, the two things I would invest in first are tolerance control and a connected-area mode — they are what turn a ten-line demo into something people can actually use.

Top comments (0)