A comprehensive guide to color correction - techniques for adjusting and enhancing colors in digital images to achieve accurate, consistent, and visually appealing results across different media and viewing conditions.
Color correction is the process of adjusting colors in digital images to achieve accurate reproduction, correct color balance, and optimal visual appeal. It's essential in photography, video production, printing, and web design to ensure consistent color appearance across different devices and media.
Elevate your digital artwork with professional AI-powered tools designed to streamline your creative workflow and enhance your artistic vision.
Generate perfect color schemes • Remove backgrounds instantly • Resize images intelligently • Professional results in seconds
Basic color correction involves adjusting the fundamental properties of color:
// Color correction processor
class ColorCorrector {
constructor() {
this.canvas = document.createElement('canvas');
this.ctx = this.canvas.getContext('2d');
}
// White balance correction
correctWhiteBalance(imageData, temperature = 6500, tint = 0) {
const { data, width, height } = imageData;
const result = new Uint8ClampedArray(data.length);
// Calculate temperature adjustment factors
const tempFactor = this.calculateTemperatureFactor(temperature);
const tintFactor = this.calculateTintFactor(tint);
for (let i = 0; i < data.length; i += 4) {
const r = data[i];
const g = data[i + 1];
const b = data[i + 2];
// Apply temperature correction
result[i] = Math.max(0, Math.min(255, r * tempFactor.r));
result[i + 1] = Math.max(0, Math.min(255, g * tempFactor.g * tintFactor));
result[i + 2] = Math.max(0, Math.min(255, b * tempFactor.b));
result[i + 3] = data[i + 3];
}
return new ImageData(result, width, height);
}
calculateTemperatureFactor(temperature) {
// Simplified temperature to RGB conversion
const temp = temperature / 100;
let r, g, b;
if (temp <= 66) {
r = 255;
g = temp;
g = 99.4708025861 * Math.log(g) - 161.1195681661;
if (temp >= 19) {
b = temp - 10;
b = 138.5177312231 * Math.log(b) - 305.0447927307;
} else {
b = 0;
}
} else {
r = temp - 60;
r = 329.698727446 * Math.pow(r, -0.1332047592);
g = temp - 60;
g = 288.1221695283 * Math.pow(g, -0.0755148492);
b = 255;
}
return {
r: Math.max(0, Math.min(255, r)) / 255,
g: Math.max(0, Math.min(255, g)) / 255,
b: Math.max(0, Math.min(255, b)) / 255
};
}
calculateTintFactor(tint) {
// Tint affects green channel primarily
return 1 + (tint / 100);
}
// Exposure correction
correctExposure(imageData, stops) {
const { data, width, height } = imageData;
const result = new Uint8ClampedArray(data.length);
const factor = Math.pow(2, stops);
for (let i = 0; i < data.length; i += 4) {
// Convert to linear space, apply exposure, convert back
const r = this.srgbToLinear(data[i] / 255) * factor;
const g = this.srgbToLinear(data[i + 1] / 255) * factor;
const b = this.srgbToLinear(data[i + 2] / 255) * factor;
result[i] = Math.round(this.linearToSrgb(r) * 255);
result[i + 1] = Math.round(this.linearToSrgb(g) * 255);
result[i + 2] = Math.round(this.linearToSrgb(b) * 255);
result[i + 3] = data[i + 3];
}
return new ImageData(result, width, height);
}
srgbToLinear(value) {
return value <= 0.04045 ? value / 12.92 : Math.pow((value + 0.055) / 1.055, 2.4);
}
linearToSrgb(value) {
return value <= 0.0031308 ? value * 12.92 : 1.055 * Math.pow(value, 1 / 2.4) - 0.055;
}
// Highlight and shadow recovery
adjustHighlightsShadows(imageData, highlights = 0, shadows = 0) {
const { data, width, height } = imageData;
const result = new Uint8ClampedArray(data.length);
for (let i = 0; i < data.length; i += 4) {
const r = data[i] / 255;
const g = data[i + 1] / 255;
const b = data[i + 2] / 255;
// Calculate luminance
const lum = 0.299 * r + 0.587 * g + 0.114 * b;
// Apply highlight/shadow adjustments based on luminance
let factor = 1;
if (lum > 0.5) {
// Highlights
const highlightFactor = 1 - (lum - 0.5) * 2;
factor = 1 + (highlights / 100) * highlightFactor;
} else {
// Shadows
const shadowFactor = (0.5 - lum) * 2;
factor = 1 + (shadows / 100) * shadowFactor;
}
result[i] = Math.max(0, Math.min(255, r * factor * 255));
result[i + 1] = Math.max(0, Math.min(255, g * factor * 255));
result[i + 2] = Math.max(0, Math.min(255, b * factor * 255));
result[i + 3] = data[i + 3];
}
return new ImageData(result, width, height);
}
}// Advanced color correction with curves
class AdvancedColorCorrector {
constructor() {
this.corrector = new ColorCorrector();
}
// Apply tone curve
applyCurve(imageData, curvePoints) {
const { data, width, height } = imageData;
const result = new Uint8ClampedArray(data.length);
const lookupTable = this.generateCurveLUT(curvePoints);
for (let i = 0; i < data.length; i += 4) {
result[i] = lookupTable[data[i]];
result[i + 1] = lookupTable[data[i + 1]];
result[i + 2] = lookupTable[data[i + 2]];
result[i + 3] = data[i + 3];
}
return new ImageData(result, width, height);
}
generateCurveLUT(points) {
const lut = new Array(256);
// Interpolate curve points
for (let i = 0; i < 256; i++) {
lut[i] = this.interpolateCurve(i, points);
}
return lut;
}
interpolateCurve(x, points) {
// Find surrounding points
let p1 = points[0];
let p2 = points[points.length - 1];
for (let i = 0; i < points.length - 1; i++) {
if (x >= points[i].x && x <= points[i + 1].x) {
p1 = points[i];
p2 = points[i + 1];
break;
}
}
// Linear interpolation
const t = (x - p1.x) / (p2.x - p1.x);
return Math.round(p1.y + t * (p2.y - p1.y));
}
// Levels adjustment
adjustLevels(imageData, inputBlack = 0, inputWhite = 255, gamma = 1.0, outputBlack = 0, outputWhite = 255) {
const { data, width, height } = imageData;
const result = new Uint8ClampedArray(data.length);
// Generate levels lookup table
const lut = new Array(256);
for (let i = 0; i < 256; i++) {
// Input levels
let value = (i - inputBlack) / (inputWhite - inputBlack);
value = Math.max(0, Math.min(1, value));
// Gamma correction
value = Math.pow(value, 1 / gamma);
// Output levels
value = outputBlack + value * (outputWhite - outputBlack);
lut[i] = Math.max(0, Math.min(255, Math.round(value)));
}
// Apply lookup table
for (let i = 0; i < data.length; i += 4) {
result[i] = lut[data[i]];
result[i + 1] = lut[data[i + 1]];
result[i + 2] = lut[data[i + 2]];
result[i + 3] = data[i + 3];
}
return new ImageData(result, width, height);
}
// Color balance adjustment
adjustColorBalance(imageData, shadows = {r: 0, g: 0, b: 0}, midtones = {r: 0, g: 0, b: 0}, highlights = {r: 0, g: 0, b: 0}) {
const { data, width, height } = imageData;
const result = new Uint8ClampedArray(data.length);
for (let i = 0; i < data.length; i += 4) {
const r = data[i] / 255;
const g = data[i + 1] / 255;
const b = data[i + 2] / 255;
// Calculate luminance
const lum = 0.299 * r + 0.587 * g + 0.114 * b;
// Calculate weights for shadows, midtones, highlights
const shadowWeight = this.calculateToneWeight(lum, 0, 0.3);
const midtoneWeight = this.calculateToneWeight(lum, 0.3, 0.7);
const highlightWeight = this.calculateToneWeight(lum, 0.7, 1.0);
// Apply color balance
const rAdjust = (shadows.r * shadowWeight + midtones.r * midtoneWeight + highlights.r * highlightWeight) / 100;
const gAdjust = (shadows.g * shadowWeight + midtones.g * midtoneWeight + highlights.g * highlightWeight) / 100;
const bAdjust = (shadows.b * shadowWeight + midtones.b * midtoneWeight + highlights.b * highlightWeight) / 100;
result[i] = Math.max(0, Math.min(255, (r + rAdjust) * 255));
result[i + 1] = Math.max(0, Math.min(255, (g + gAdjust) * 255));
result[i + 2] = Math.max(0, Math.min(255, (b + bAdjust) * 255));
result[i + 3] = data[i + 3];
}
return new ImageData(result, width, height);
}
calculateToneWeight(luminance, rangeStart, rangeEnd) {
if (luminance < rangeStart || luminance > rangeEnd) {
return 0;
}
const center = (rangeStart + rangeEnd) / 2;
const distance = Math.abs(luminance - center);
const maxDistance = (rangeEnd - rangeStart) / 2;
return 1 - (distance / maxDistance);
}
}// HSL-based selective color correction
class SelectiveColorCorrector {
constructor() {
this.hslRanges = {
reds: { hMin: 345, hMax: 15, sMin: 0.3, lMin: 0.1, lMax: 0.9 },
oranges: { hMin: 15, hMax: 45, sMin: 0.3, lMin: 0.1, lMax: 0.9 },
yellows: { hMin: 45, hMax: 75, sMin: 0.3, lMin: 0.1, lMax: 0.9 },
greens: { hMin: 75, hMax: 165, sMin: 0.3, lMin: 0.1, lMax: 0.9 },
cyans: { hMin: 165, hMax: 195, sMin: 0.3, lMin: 0.1, lMax: 0.9 },
blues: { hMin: 195, hMax: 255, sMin: 0.3, lMin: 0.1, lMax: 0.9 },
magentas: { hMin: 255, hMax: 345, sMin: 0.3, lMin: 0.1, lMax: 0.9 }
};
}
// Adjust specific color range
adjustColorRange(imageData, colorRange, adjustments) {
const { data, width, height } = imageData;
const result = new Uint8ClampedArray(data.length);
const range = this.hslRanges[colorRange];
if (!range) {
throw new Error(`Unknown color range: ${colorRange}`);
}
for (let i = 0; i < data.length; i += 4) {
const r = data[i] / 255;
const g = data[i + 1] / 255;
const b = data[i + 2] / 255;
const hsl = this.rgbToHsl(r, g, b);
// Check if pixel is in target color range
if (this.isInColorRange(hsl, range)) {
// Apply adjustments
const weight = this.calculateColorWeight(hsl, range);
hsl.h = (hsl.h + adjustments.hue * weight) % 360;
if (hsl.h < 0) hsl.h += 360;
hsl.s = Math.max(0, Math.min(1, hsl.s + adjustments.saturation * weight / 100));
hsl.l = Math.max(0, Math.min(1, hsl.l + adjustments.lightness * weight / 100));
const rgb = this.hslToRgb(hsl.h, hsl.s, hsl.l);
result[i] = Math.round(rgb.r * 255);
result[i + 1] = Math.round(rgb.g * 255);
result[i + 2] = Math.round(rgb.b * 255);
} else {
result[i] = data[i];
result[i + 1] = data[i + 1];
result[i + 2] = data[i + 2];
}
result[i + 3] = data[i + 3];
}
return new ImageData(result, width, height);
}
isInColorRange(hsl, range) {
const { hMin, hMax, sMin, lMin, lMax } = range;
// Handle hue wraparound (e.g., reds: 345-15)
let hueInRange;
if (hMin > hMax) {
hueInRange = hsl.h >= hMin || hsl.h <= hMax;
} else {
hueInRange = hsl.h >= hMin && hsl.h <= hMax;
}
return hueInRange &&
hsl.s >= sMin &&
hsl.l >= lMin &&
hsl.l <= lMax;
}
calculateColorWeight(hsl, range) {
// Calculate how strongly the pixel matches the target range
const { hMin, hMax, sMin, lMin, lMax } = range;
// Hue weight
let hueWeight;
if (hMin > hMax) {
const distToMin = Math.min(Math.abs(hsl.h - hMin), Math.abs(hsl.h - hMin + 360));
const distToMax = Math.min(Math.abs(hsl.h - hMax), Math.abs(hsl.h - hMax - 360));
hueWeight = 1 - Math.min(distToMin, distToMax) / 180;
} else {
const center = (hMin + hMax) / 2;
const distance = Math.abs(hsl.h - center);
const maxDistance = (hMax - hMin) / 2;
hueWeight = 1 - (distance / maxDistance);
}
// Saturation weight
const satWeight = hsl.s >= sMin ? 1 : 0;
// Lightness weight
const lightWeight = (hsl.l >= lMin && hsl.l <= lMax) ? 1 : 0;
return hueWeight * satWeight * lightWeight;
}
rgbToHsl(r, g, b) {
const max = Math.max(r, g, b);
const min = Math.min(r, g, b);
const diff = max - min;
const sum = max + min;
const l = sum / 2;
if (diff === 0) {
return { h: 0, s: 0, l };
}
const s = l > 0.5 ? diff / (2 - sum) : diff / sum;
let h;
switch (max) {
case r: h = ((g - b) / diff + (g < b ? 6 : 0)) / 6; break;
case g: h = ((b - r) / diff + 2) / 6; break;
case b: h = ((r - g) / diff + 4) / 6; break;
}
return { h: h * 360, s, l };
}
hslToRgb(h, s, l) {
h /= 360;
if (s === 0) {
return { r: l, g: l, b: l };
}
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;
return {
r: hue2rgb(p, q, h + 1/3),
g: hue2rgb(p, q, h),
b: hue2rgb(p, q, h - 1/3)
};
}
}// Automated color correction system
class AutoColorCorrector {
constructor() {
this.corrector = new ColorCorrector();
this.advancedCorrector = new AdvancedColorCorrector();
}
// Automatic white balance
autoWhiteBalance(imageData, method = 'grayWorld') {
switch (method) {
case 'grayWorld':
return this.grayWorldWhiteBalance(imageData);
case 'whitePoint':
return this.whitePointBalance(imageData);
case 'histogram':
return this.histogramWhiteBalance(imageData);
default:
return this.grayWorldWhiteBalance(imageData);
}
}
grayWorldWhiteBalance(imageData) {
const { data, width, height } = imageData;
let rSum = 0, gSum = 0, bSum = 0;
const pixelCount = width * height;
// Calculate average RGB values
for (let i = 0; i < data.length; i += 4) {
rSum += data[i];
gSum += data[i + 1];
bSum += data[i + 2];
}
const rAvg = rSum / pixelCount;
const gAvg = gSum / pixelCount;
const bAvg = bSum / pixelCount;
// Calculate correction factors
const grayValue = (rAvg + gAvg + bAvg) / 3;
const rFactor = grayValue / rAvg;
const gFactor = grayValue / gAvg;
const bFactor = grayValue / bAvg;
// Apply correction
const result = new Uint8ClampedArray(data.length);
for (let i = 0; i < data.length; i += 4) {
result[i] = Math.max(0, Math.min(255, data[i] * rFactor));
result[i + 1] = Math.max(0, Math.min(255, data[i + 1] * gFactor));
result[i + 2] = Math.max(0, Math.min(255, data[i + 2] * bFactor));
result[i + 3] = data[i + 3];
}
return new ImageData(result, width, height);
}
// Auto levels adjustment
autoLevels(imageData, clipPercent = 1) {
const { data, width, height } = imageData;
const histogram = this.calculateHistogram(data);
// Find clip points
const totalPixels = width * height;
const clipPixels = Math.floor(totalPixels * clipPercent / 100);
let blackPoint = 0, whitePoint = 255;
// Find black point
let cumulative = 0;
for (let i = 0; i < 256; i++) {
cumulative += histogram[i];
if (cumulative > clipPixels) {
blackPoint = i;
break;
}
}
// Find white point
cumulative = 0;
for (let i = 255; i >= 0; i--) {
cumulative += histogram[i];
if (cumulative > clipPixels) {
whitePoint = i;
break;
}
}
// Apply levels adjustment
return this.advancedCorrector.adjustLevels(
imageData,
blackPoint,
whitePoint,
1.0,
0,
255
);
}
calculateHistogram(data) {
const histogram = new Array(256).fill(0);
for (let i = 0; i < data.length; i += 4) {
// Use luminance for histogram
const lum = Math.round(0.299 * data[i] + 0.587 * data[i + 1] + 0.114 * data[i + 2]);
histogram[lum]++;
}
return histogram;
}
// Auto contrast enhancement
autoContrast(imageData) {
const { data, width, height } = imageData;
// Find min and max values for each channel
let rMin = 255, rMax = 0;
let gMin = 255, gMax = 0;
let bMin = 255, bMax = 0;
for (let i = 0; i < data.length; i += 4) {
rMin = Math.min(rMin, data[i]);
rMax = Math.max(rMax, data[i]);
gMin = Math.min(gMin, data[i + 1]);
gMax = Math.max(gMax, data[i + 1]);
bMin = Math.min(bMin, data[i + 2]);
bMax = Math.max(bMax, data[i + 2]);
}
// Calculate stretch factors
const rRange = rMax - rMin;
const gRange = gMax - gMin;
const bRange = bMax - bMin;
const result = new Uint8ClampedArray(data.length);
for (let i = 0; i < data.length; i += 4) {
// Stretch each channel to full range
result[i] = rRange > 0 ? Math.round((data[i] - rMin) * 255 / rRange) : data[i];
result[i + 1] = gRange > 0 ? Math.round((data[i + 1] - gMin) * 255 / gRange) : data[i + 1];
result[i + 2] = bRange > 0 ? Math.round((data[i + 2] - bMin) * 255 / bRange) : data[i + 2];
result[i + 3] = data[i + 3];
}
return new ImageData(result, width, height);
}
// Complete auto correction
autoCorrect(imageData, options = {}) {
const {
whiteBalance = true,
levels = true,
contrast = true,
saturation = 1.1
} = options;
let result = imageData;
if (whiteBalance) {
result = this.autoWhiteBalance(result);
}
if (levels) {
result = this.autoLevels(result);
}
if (contrast) {
result = this.autoContrast(result);
}
if (saturation !== 1.0) {
result = this.corrector.adjustSaturation(result, saturation);
}
return result;
}
}Explore these comprehensive pillar articles related to Color Correction
A comprehensive guide to color theory, its fundamental principles, and practical applications in digital design and web development.
Comprehensive guide to complementary colors - opposing hues on the color wheel that create maximum visual contrast, dynamic tension, and powerful design impact through scientific color relationships and perceptual phenomena.
A comprehensive guide to tetradic color schemes - sophisticated four-color harmonies that use two complementary pairs arranged in rectangular or square formations on the color wheel, providing maximum color variety while maintaining structured relationships essential for dynamic design compositions, brand systems, and advanced color theory applications.
A comprehensive guide to split-complementary color schemes - sophisticated three-color harmonies that combine a base color with two colors adjacent to its complement, creating balanced visual contrast with reduced tension compared to direct complementary pairs, essential for refined design compositions, brand identity systems, and advanced color theory applications.
Discover more about Color Correction and related color theory concepts
A comprehensive guide to color theory, its fundamental principles, and practical applications in digital design and web development.
Comprehensive guide to complementary colors - opposing hues on the color wheel that create maximum visual contrast, dynamic tension, and powerful design impact through scientific color relationships and perceptual phenomena.
A comprehensive guide to split-complementary color schemes - sophisticated three-color harmonies that combine a base color with two colors adjacent to its complement, creating balanced visual contrast with reduced tension compared to direct complementary pairs, essential for refined design compositions, brand identity systems, and advanced color theory applications.
A comprehensive guide to tetradic color schemes - sophisticated four-color harmonies that use two complementary pairs arranged in rectangular or square formations on the color wheel, providing maximum color variety while maintaining structured relationships essential for dynamic design compositions, brand systems, and advanced color theory applications.
Comprehensive guide to analogous color schemes - adjacent hues on the color wheel that create natural harmony, visual cohesion, and sophisticated design solutions through subtle color relationships and graduated transitions.
A comprehensive guide to monochromatic color schemes - sophisticated single-hue harmonies that create unified visual experiences through systematic variations in saturation, lightness, and tonal relationships, essential for elegant design compositions, brand consistency, and advanced color theory applications requiring cohesive aesthetic unity.
Discover more color terminology and concepts in our comprehensive glossary.
Browse Full Glossary