Please bookmark this page to avoid losing your image tool!

Image Atari Filter Effect 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.
function processImage(originalImg, pixelationFactor = 8, customPaletteStr = "") {
    // Helper function to convert HEX to RGB object {r, g, b}
    function hexToRgb(hex) {
        if (!hex) return null;
        // 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)
        } : null;
    }

    // Helper function to calculate squared Euclidean distance between two RGB colors.
    // Squared distance is used for comparison efficiency as sqrt is not needed.
    function colorDistanceSquared(rgb1, rgb2) {
        const dr = rgb1.r - rgb2.r;
        const dg = rgb1.g - rgb2.g;
        const db = rgb1.b - rgb2.b;
        return dr * dr + dg * dg + db * db;
    }

    // Default Atari-like palette (hex strings)
    // This is a curated palette inspired by common Atari 2600 game colors.
    const DEFAULT_ATARI_PALETTE_HEX = [
        "#000000", // Black
        "#545454", // Dark Gray
        "#A8A8A8", // Light Gray
        "#FCFCFC", // White
        "#B45C54", // Dull Red (e.g., Pac-Man ghosts, Combat tanks)
        "#D89C54", // Orange/Brown (e.g., Pitfall Harry)
        "#D0D05C", // Dull Yellow (e.g., Pac-Man)
        "#64B444", // Dull Green (e.g., Combat tanks, Pitfall vines)
        "#7070D8", // Dull Blue (e.g., Space Invaders shields, water)
        "#B470B4"  // Dull Magenta/Purple (e.g., Adventure dragon, various accents)
    ];

    // Validate and set pixelationFactor
    // Ensure it's a number and at least 1. Default to 8 if invalid.
    const currentPixelationFactor = Math.max(1, Number(pixelationFactor) || 8);

    // Parse and prepare the color palette (convert hex strings to RGB objects)
    let activePaletteRgb = [];
    if (customPaletteStr && typeof customPaletteStr === 'string' && customPaletteStr.trim() !== "") {
        const customColorsHex = customPaletteStr.split(',');
        customColorsHex.forEach(hexColor => {
            const rgb = hexToRgb(hexColor.trim());
            if (rgb) {
                activePaletteRgb.push(rgb);
            }
        });
    }

    // If custom palette is empty or all entries were invalid, use the default palette
    if (activePaletteRgb.length === 0) {
        DEFAULT_ATARI_PALETTE_HEX.forEach(hexColor => {
            // Assuming default palette hex codes are always valid
            const rgb = hexToRgb(hexColor);
            if (rgb) activePaletteRgb.push(rgb);
        });
    }
    
    // Ultimate fallback if even the default palette somehow failed to load (e.g., programming error in default hex)
    // This ensures there's always at least one color to map to.
    if (activePaletteRgb.length === 0) {
        console.error("Atari Filter: No valid palette available. Using a minimal black & white palette.");
        activePaletteRgb = [{ r: 0, g: 0, b: 0 }, { r: 255, g: 255, b: 255 }];
    }

    // Get image dimensions. Use naturalWidth/Height for intrinsic size.
    const imgWidth = originalImg.naturalWidth || originalImg.width;
    const imgHeight = originalImg.naturalHeight || originalImg.height;

    // Handle cases where image dimensions are invalid or image not loaded
    if (imgWidth === 0 || imgHeight === 0) {
        console.error("Atari Filter: Image has zero width or height. Ensure the image is loaded before processing.");
        const errorCanvas = document.createElement('canvas');
        errorCanvas.width = 1; // Return a 1x1 canvas to avoid further errors downstream
        errorCanvas.height = 1;
        return errorCanvas;
    }

    // Create the final canvas to be returned
    const finalCanvas = document.createElement('canvas');
    finalCanvas.width = imgWidth;
    finalCanvas.height = imgHeight;
    const finalCtx = finalCanvas.getContext('2d');

    // 1. Pixelation Step:
    // Calculate dimensions for the small, temporary canvas.
    // These dimensions determine the size of the "retro" pixels.
    // Ensure dimensions are at least 1x1.
    const scaledWidth = Math.max(1, Math.floor(imgWidth / currentPixelationFactor));
    const scaledHeight = Math.max(1, Math.floor(imgHeight / currentPixelationFactor));

    // Create a temporary canvas to draw the downscaled image.
    const tempCanvas = document.createElement('canvas');
    tempCanvas.width = scaledWidth;
    tempCanvas.height = scaledHeight;
    const tempCtx = tempCanvas.getContext('2d');

    // Draw the original image onto the temporary canvas, scaled down.
    // This scaling inherently performs a type of color averaging for each "retro" pixel block.
    tempCtx.drawImage(originalImg, 0, 0, scaledWidth, scaledHeight);

    // 2. Color Quantization Step:
    // Get pixel data from the temporary (small, pixelated) canvas.
    const imageData = tempCtx.getImageData(0, 0, scaledWidth, scaledHeight);
    const data = imageData.data; // This is a Uint8ClampedArray: [R, G, B, A, R, G, B, A, ...]

    // Iterate over each pixel in the temporary canvas
    for (let i = 0; i < data.length; i += 4) {
        const r = data[i];     // Red value
        const g = data[i + 1]; // Green value
        const b = data[i + 2]; // Blue value
        // const a = data[i + 3]; // Alpha value (currently unused in distance, but preserved if needed)

        let closestColor = activePaletteRgb[0]; // Assume first palette color is closest initially
        let minDistance = Infinity;

        // Find the closest color_in_palette for the current pixel's color
        for (const paletteColor of activePaletteRgb) {
            const distance = colorDistanceSquared({ r, g, b }, paletteColor);
            if (distance < minDistance) {
                minDistance = distance;
                closestColor = paletteColor;
            }
            // Optimization: if a perfect match is found, no need to check further
            if (minDistance === 0) break; 
        }

        // Replace the original pixel color with the closest palette color
        data[i] = closestColor.r;
        data[i + 1] = closestColor.g;
        data[i + 2] = closestColor.b;
        data[i + 3] = 255; // Atari-style effects typically use opaque colors
    }
    // Put the modified pixel data back onto the temporary canvas
    tempCtx.putImageData(imageData, 0, 0);

    // 3. Scale Up Step:
    // Draw the processed (pixelated and color-quantized) image from the temporary canvas
    // onto the final canvas, scaling it up to the original image dimensions.
    
    // Disable image smoothing to maintain sharp, blocky pixels.
    finalCtx.imageSmoothingEnabled = false;
    // For older browsers, you might need vendor prefixes, but largely unnecessary now.
    // finalCtx.mozImageSmoothingEnabled = false;
    // finalCtx.webkitImageSmoothingEnabled = false;
    // finalCtx.msImageSmoothingEnabled = false;

    finalCtx.drawImage(tempCanvas, 0, 0, scaledWidth, scaledHeight, 0, 0, imgWidth, imgHeight);

    return finalCanvas;
}

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 Atari Filter Effect Tool allows users to transform their images into a retro-style look reminiscent of classic Atari games. By applying a pixelation effect and using a limited color palette inspired by the Atari 2600, this tool makes images appear like they were created with vintage gaming graphics. Users can customize the level of pixelation and even input their own color palettes. This tool is ideal for creating nostalgic artwork, game graphics, and social media posts that evoke a sense of retro gaming culture.

Leave a Reply

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