You can edit the below JavaScript code to customize the image tool.
Apply Changes
function processImage(originalImg, intensity = 0.8, ashLevel = 0.4, contrastBoost = 0.3) {
// Ensure parameters are numbers and clamped to their valid ranges.
// Provide default if conversion to Number results in NaN.
let pIntensity = Number(intensity);
pIntensity = isNaN(pIntensity) ? 0.8 : Math.max(0, Math.min(1, pIntensity));
let pAshLevel = Number(ashLevel);
pAshLevel = isNaN(pAshLevel) ? 0.4 : Math.max(0, Math.min(1, pAshLevel));
let pContrastBoost = Number(contrastBoost);
pContrastBoost = isNaN(pContrastBoost) ? 0.3 : Math.max(0, Math.min(1, pContrastBoost));
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d', { willReadFrequently: true }); // Optimization hint
canvas.width = originalImg.naturalWidth || originalImg.width;
canvas.height = originalImg.naturalHeight || originalImg.height;
if (canvas.width === 0 || canvas.height === 0) {
// Handle case of invalid image dimensions, though Image obj usually has dimensions
console.error("Image has zero width or height or is not loaded yet.");
// return an empty canvas or handle as preferred
return canvas;
}
ctx.drawImage(originalImg, 0, 0, canvas.width, canvas.height);
let imageData;
try {
imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
} catch (e) {
// This can happen due to tainted canvas (CORS) if image is from another domain
// without appropriate CORS headers.
console.error("Could not getImageData due to CORS or other security restrictions: ", e);
// Fallback: return the canvas with the original image drawn, without effects.
// A more user-friendly tool might display an error message.
return canvas;
}
const data = imageData.data;
// Coefficients for the "Volcanic" color transformation
// These aim for strong reds, oranges, yellows, and suppression of cool colors.
const targetR_coeff_r = 1.4, targetR_coeff_g = 0.5, targetR_coeff_b = 0.1;
const targetG_coeff_r = 0.7, targetG_coeff_g = 0.4, targetG_coeff_b = 0.05;
const targetB_coeff_r = 0.2, targetB_coeff_g = 0.1, targetB_coeff_b = 0.05;
for (let i = 0; i < data.length; i += 4) {
const r_orig = data[i];
const g_orig = data[i + 1];
const b_orig = data[i + 2];
// Alpha channel data[i+3] is preserved
// 1. Calculate "Volcanic" target color
const targetR_volcanic = r_orig * targetR_coeff_r + g_orig * targetR_coeff_g + b_orig * targetR_coeff_b;
const targetG_volcanic = r_orig * targetG_coeff_r + g_orig * targetG_coeff_g + b_orig * targetG_coeff_b;
const targetB_volcanic = r_orig * targetB_coeff_r + g_orig * targetB_coeff_g + b_orig * targetB_coeff_b;
// 2. Blend original color with volcanic target color based on pIntensity
let currentR = r_orig * (1.0 - pIntensity) + targetR_volcanic * pIntensity;
let currentG = g_orig * (1.0 - pIntensity) + targetG_volcanic * pIntensity;
let currentB = b_orig * (1.0 - pIntensity) + targetB_volcanic * pIntensity;
// Clamp after blending
currentR = Math.min(255.0, Math.max(0.0, currentR));
currentG = Math.min(255.0, Math.max(0.0, currentG));
currentB = Math.min(255.0, Math.max(0.0, currentB));
// 3. Apply Ash/Darkness Effect
// Use original luminance to determine how much ash affect applies,
// so shadows in the original image become ashy.
const originalLuminance = (0.299 * r_orig + 0.587 * g_orig + 0.114 * b_orig) / 255.0; // Normalized 0-1
const darknessFactor = (1.0 - originalLuminance) * pAshLevel; // More darkness for darker original pixels
currentR *= (1.0 - darknessFactor * 0.6); // Red is preserved a bit more from darkening
currentG *= (1.0 - darknessFactor * 0.8);
currentB *= (1.0 - darknessFactor * 1.0); // Blue/Green darkened more for ashy look
// 4. Contrast Enhancement
if (pContrastBoost > 0.001) { // Apply if contrastBoost is meaningfully greater than 0
const contrastFactor = 1.0 + pContrastBoost * 2.0; // Factor from 1.0 (no boost) to 3.0 (max boost)
currentR = 128.0 + (currentR - 128.0) * contrastFactor;
currentG = 128.0 + (currentG - 128.0) * contrastFactor;
currentB = 128.0 + (currentB - 128.0) * contrastFactor;
}
// 5. Add subtle "glow" or "heat" to very bright, hot areas (post-contrast)
// Calculate luminance of the current color (after volcanic, ash, contrast)
const currentEffectLuminance = (0.299 * currentR + 0.587 * currentG + 0.114 * currentB);
// Condition for glow: very bright AND predominantly red/orange
if (currentEffectLuminance > 200.0 && currentR > (currentG + currentB) * 1.2) {
const glowStrength = (currentEffectLuminance - 200.0) / 55.0; // Normalized 0-1 for how much over 200 (up to 255)
const glowAmount = glowStrength * 0.2 * pIntensity; // Max 0.2 glow factor, scaled by overall intensity
currentR += (255.0 - currentR) * glowAmount; // Push R towards 255 (brighter red)
currentG += (200.0 - currentG) * glowAmount * 0.5; // Push G towards 200 (making R+G yellower if G was low, or less green if G was high)
}
// Final Clamp and Round before assigning to integer-based pixel data
data[i] = Math.min(255, Math.max(0, Math.round(currentR)));
data[i + 1] = Math.min(255, Math.max(0, Math.round(currentG)));
data[i + 2] = Math.min(255, Math.max(0, Math.round(currentB)));
}
ctx.putImageData(imageData, 0, 0);
return canvas;
}
Apply Changes