Skip to main content

Replace Colors in Images Online Replace a specific color with another using tolerance matching.

Image Color Replace illustration
🖼️

Image Color Replace

Replace a specific color with another using tolerance matching.

1

Upload Image

Drop or select an image.

2

Choose Colors

Pick source and target colors.

3

Apply and Download

Adjust tolerance, apply, download.

Loading tool...

What Is Image Color Replace?

Image Color Replace is a tool that swaps a specific color with a replacement color using Euclidean RGB distance matching, allowing users to recolor images by changing the original color to a new one. Graphic designers and digital artists use it to solve problems like inconsistent branding colors in their visual assets, where they need to replace a specific color with a new one to maintain brand consistency. One specific problem it solves is the need to change color schemes in images without manually editing each pixel, which can be time-consuming and prone to errors.

What makes this tool different is its ability to preserve luminance variations for natural results, achieved through calculating the luminance of each pixel using the NTSC formula, as seen in the code where it calculates `lum = (d[i]*0.299+d[i+1]*0.587+d[i+2]*0.114) - (sr*0.299+sg*0.587+sb*0.114)`. It also features a tolerance control that determines how similar pixels must be to match the source color, allowing for flexible color replacement and color swap options. The tool's interface allows users to input the source and target colors using hex codes or color pickers, and adjust the tolerance level using a range slider, making it easy to perform a recolor image operation.

It uses a canvas element to process the image and apply the color replacement in real-time, allowing for instant previewing of the results. The processed image can then be downloaded as a PNG file, providing a convenient way to obtain the final result after performing the color replacement or color swap operation.

Why Use Image Color Replace?

  • Color pickers for precision
  • Adjustable tolerance
  • Preserves luminance variations
  • Per-pixel color distance matching

Common Use Cases

Product Variations

Show different color options.

Design Tweaks

Change specific colors.

Creative Effects

Surreal color swaps.

Brand Updates

Update brand colors in images.

Technical Guide

The tool works by utilizing the HTML5 Canvas API to process and manipulate images. When an image is dropped into the FileDropzone component, it creates a URL object using `URL.createObjectURL` and sets it as the source for an Image object. Once the image is loaded, it draws the image onto a canvas element using `ctx.drawImage`. The tool then retrieves the image data from the canvas using `ctx.getImageData`, which returns a Uint8ClampedArray representing the pixel data.

The color replacement algorithm iterates over each pixel in the image data array, calculating the Euclidean distance between the current pixel's RGB values and the source color's RGB values. If the calculated distance is within the specified tolerance range, it replaces the pixel's RGB values with the target color's RGB values while preserving the luminance offset. This is achieved by adjusting the target color's RGB values based on the difference in luminance between the original pixel and the source color.

The tool uses React Hooks such as `useState`, `useRef`, and `useCallback` to manage state, references, and function memoization. The `canvasRef` reference is used to access the canvas element and its context, while `image` and `result` states store the input image URL and processed image data URL respectively. When the user clicks the Apply button, it triggers the `processImage` function which executes the color replacement algorithm on the input image.

The tolerance range is mapped from a scale of 0-100 to a distance range of 0-441 using the formula `maxDist = tolerance * 4.41`. This allows users to control how similar pixels must be to match the source color, enabling flexible color replacement options. The processed image can then be downloaded as a PNG file using the `canvas.toDataURL` method and the `download` function.

The React components such as FileDropzone and glass-card are used for handling user input and displaying the interface. The tool also utilizes browser APIs like URL.revokeObjectURL to clean up object URLs when they are no longer needed, preventing memory leaks. By combining these technologies and algorithms, it enables efficient color replacement in images while preserving natural shading and luminance variations.

Tips & Best Practices

  • 1
    Adjust tolerance to fine-tune color replacement accuracy
  • 2
    Select source and target colors using hex codes
  • 3
    Click Apply to process the image after setting options
  • 4
    Use Reset to clear previous image and results
  • 5
    Download resulting images as PNG files
  • 6
    Revoke object URLs when resetting or unmounting

Related Tools

Frequently Asked Questions

Q How does tolerance work?
Sets how similar a pixel must be to be replaced.
Q Can I replace multiple colors?
Apply multiple times with different source/target.
Q Does it preserve shading?
Yes, luminance variations are maintained.
Q Why are some pixels not replaced?
Increase tolerance to capture more variations.

About This Tool

Image Color Replace is a free online tool by FreeToolkit.ai. All processing happens directly in your browser — your data never leaves your device. No registration or installation required.