You can edit the below JavaScript code to customize the image tool.
function processImage(originalImg, hueRotation = 0) {
/**
* Adjusts the hue of an image.
* @param {Image} originalImg - The original javascript Image object.
* @param {number} hueRotation - The amount to rotate the hue, in degrees (0-360).
* @returns {HTMLCanvasElement} A canvas element with the hue-adjusted image.
*/
// Create a canvas element
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
// 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 image data from the canvas
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
const data = imageData.data;
// Helper function to convert RGB to HSL
// Input: r, g, b in [0, 255]
// Output: h, s, l in [0, 1]
const rgbToHsl = (r, g, b) => {
r /= 255;
g /= 255;
b /= 255;
const max = Math.max(r, g, b);
const min = Math.min(r, g, b);
let h = 0, s, l = (max + min) / 2;
if (max === min) {
h = s = 0; // achromatic
} else {
const d = max - min;
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
switch (max) {
case r:
h = (g - b) / d + (g < b ? 6 : 0);
break;
case g:
h = (b - r) / d + 2;
break;
case b:
h = (r - g) / d + 4;
break;
}
h /= 6;
}
return [h, s, l];
};
// Helper function to convert HSL to RGB
// Input: h, s, l in [0, 1]
// Output: r, g, b in [0, 255]
const hslToRgb = (h, s, l) => {
let r, g, b;
if (s === 0) {
r = g = b = l; // achromatic
} else {
const hue2rgb = (p, q, t) => {
if (t < 0) t += 1;
if (t > 1) t -= 1;
if (t < 1 / 6) return p + (q - p) * 6 * t;
if (t < 1 / 2) return q;
if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6;
return p;
};
const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
const p = 2 * l - q;
r = hue2rgb(p, q, h + 1 / 3);
g = hue2rgb(p, q, h);
b = hue2rgb(p, q, h - 1 / 3);
}
return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
};
// Convert the hue rotation from degrees to a value between 0 and 1
const hueShift = parseFloat(hueRotation) / 360;
// Iterate over each pixel
for (let i = 0; i < data.length; i += 4) {
// Get the RGB values for the current pixel
const r = data[i];
const g = data[i + 1];
const b = data[i + 2];
// Convert the pixel's color from RGB to HSL
let [h, s, l] = rgbToHsl(r, g, b);
// Adjust the hue
h += hueShift;
// Ensure hue wraps around correctly (remains within 0 to 1)
if (h > 1) h -= 1;
if (h < 0) h += 1;
// Convert the modified HSL color back to RGB
const [newR, newG, newB] = hslToRgb(h, s, l);
// Update the pixel data with the new RGB values
data[i] = newR;
data[i + 1] = newG;
data[i + 2] = newB;
}
// Put the modified image data back onto the canvas
ctx.putImageData(imageData, 0, 0);
// Return the canvas with the adjusted 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!
The Image HSL Hue Adjuster is an online tool that allows users to modify the hue of an image by rotating its color spectrum. Users can input an image and specify a hue rotation angle in degrees (0 to 360). This tool is useful for creative projects, photo editing, and graphic design tasks where color adjustments are desired. By changing the hue, users can achieve various visual effects, enhance image aesthetics, or create unique artistic representations of their photos.