Please bookmark this page to avoid losing your image tool!

Image Character Customization Tool

(Free & Supports Bulk Upload)

Drag & drop your images here or

The result will appear here...
You can edit the below JavaScript code to customize the image tool.
/**
 * Customizes a character image by replacing a specified color with a new color.
 * This is useful for changing things like clothing, hair, or accessory colors.
 *
 * @param {HTMLImageElement} originalImg The original image object of the character.
 * @param {string} [fromColorStr='#ff0000'] The color to be replaced, in hex format (e.g., '#ff0000' for red).
 * @param {string} [toColorStr='#0000ff'] The new color to apply, in hex format (e.g., '#0000ff' for blue).
 * @param {number} [tolerance=30] A number from 0 to 255 representing the tolerance for color matching.
 *                                A higher value will match a wider range of similar colors, accounting
 *                                for shading and anti-aliasing.
 * @returns {HTMLCanvasElement} A new canvas element with the customized image.
 */
function processImage(originalImg, fromColorStr = '#ff0000', toColorStr = '#0000ff', tolerance = 30) {

    /**
     * Helper function to parse hex color strings into an {r, g, b} object.
     * Supports both shorthand (#rgb) and full (#rrggbb) formats.
     * @param {string} hex The hex color string.
     * @returns {{r: number, g: number, b: number}} An object with RGB values.
     */
    const parseColor = (hex) => {
        // Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF")
        const shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
        hex = hex.replace(shorthandRegex, (m, r, g, b) => r + r + g + g + b + b);

        const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
        return result ? {
            r: parseInt(result[1], 16),
            g: parseInt(result[2], 16),
            b: parseInt(result[3], 16)
        } : { r: 0, g: 0, b: 0 }; // Default to black on parse error
    };

    // Create a canvas to work with
    const canvas = document.createElement('canvas');
    const ctx = canvas.getContext('2d', { willReadFrequently: true });

    // Set canvas dimensions to match the original image
    canvas.width = originalImg.naturalWidth;
    canvas.height = originalImg.naturalHeight;

    // Draw the original image onto the canvas
    ctx.drawImage(originalImg, 0, 0);

    // Get the pixel data from the canvas
    const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
    const data = imageData.data;

    const fromColor = parseColor(fromColorStr);
    const toColor = parseColor(toColorStr);

    // Use squared tolerance for a more efficient distance calculation (avoids Math.sqrt)
    const toleranceSq = tolerance * tolerance;

    // Iterate through every pixel (4 bytes at a time: R, G, B, A)
    for (let i = 0; i < data.length; i += 4) {
        const r = data[i];
        const g = data[i + 1];
        const b = data[i + 2];
        
        // Calculate the squared Euclidean distance in the RGB color space
        const deltaR = r - fromColor.r;
        const deltaG = g - fromColor.g;
        const deltaB = b - fromColor.b;
        const distanceSq = deltaR * deltaR + deltaG * deltaG + deltaB * deltaB;

        // If the color is within the specified tolerance, replace it with the new color
        if (distanceSq <= toleranceSq) {
            data[i] = toColor.r;
            data[i + 1] = toColor.g;
            data[i + 2] = toColor.b;
            // The alpha channel (data[i + 3]) is left unchanged
        }
    }

    // Put the modified pixel data back onto the canvas
    ctx.putImageData(imageData, 0, 0);

    // Return the canvas element with the customized image
    return canvas;
}

Free Image Tool Creator

Can't find the image tool you're looking for?
Create one based on your own needs now!

Description

The Image Character Customization Tool allows users to modify character images by replacing specific colors within the image with new colors. This tool is particularly useful for altering aspects like clothing, hair, or accessories, enabling users to create personalized versions of character images. Applications include customizing avatars for games, creating unique characters for digital art, and modifying images for design projects where color personalization is desired.

Leave a Reply

Your email address will not be published. Required fields are marked *