A comprehensive guide to color models - mathematical representations of colors including RGB, CMYK, HSL, HSV, LAB, and other systems used in digital imaging, printing, and color science.
Color models are mathematical representations that define how colors can be described using numerical values. Different color models serve various purposes in digital imaging, printing, display technology, and color science, each with unique advantages for specific applications.
A color model is a systematic way to describe colors using a set of numbers or coordinates. Each model represents the same visual colors differently, optimized for specific use cases:
RGB uses red, green, and blue light to create colors through addition:
// RGB color representation
const rgbColor = {
red: 255, // 0-255 (8-bit)
green: 128, // 0-255 (8-bit)
blue: 64, // 0-255 (8-bit)
};
// RGB to hex conversion
function rgbToHex(r, g, b) {
return '#' +
r.toString(16).padStart(2, '0') +
g.toString(16).padStart(2, '0') +
b.toString(16).padStart(2, '0');
}
// RGB color mixing (additive)
function mixRGBColors(color1, color2, ratio = 0.5) {
return {
red: Math.round(color1.red * (1 - ratio) + color2.red * ratio),
green: Math.round(color1.green * (1 - ratio) + color2.green * ratio),
blue: Math.round(color1.blue * (1 - ratio) + color2.blue * ratio)
};
}
// RGB primaries and secondaries
const rgbPrimaries = {
red: { r: 255, g: 0, b: 0 },
green: { r: 0, g: 255, b: 0 },
blue: { r: 0, g: 0, b: 255 }
};
const rgbSecondaries = {
yellow: { r: 255, g: 255, b: 0 }, // Red + Green
magenta: { r: 255, g: 0, b: 255 }, // Red + Blue
cyan: { r: 0, g: 255, b: 255 } // Green + Blue
};CMYK uses cyan, magenta, yellow, and black inks for printing:
// CMYK color representation
const cmykColor = {
cyan: 75, // 0-100%
magenta: 50, // 0-100%
yellow: 25, // 0-100%
black: 10 // 0-100% (Key)
};
// RGB to CMYK conversion
function rgbToCmyk(r, g, b) {
// Normalize RGB values to 0-1
const rNorm = r / 255;
const gNorm = g / 255;
const bNorm = b / 255;
// Calculate black (K)
const k = 1 - Math.max(rNorm, gNorm, bNorm);
// Calculate CMY
const c = k === 1 ? 0 : (1 - rNorm - k) / (1 - k);
const m = k === 1 ? 0 : (1 - gNorm - k) / (1 - k);
const y = k === 1 ? 0 : (1 - bNorm - k) / (1 - k);
return {
cyan: Math.round(c * 100),
magenta: Math.round(m * 100),
yellow: Math.round(y * 100),
black: Math.round(k * 100)
};
}
// CMYK to RGB conversion
function cmykToRgb(c, m, y, k) {
// Normalize CMYK values to 0-1
const cNorm = c / 100;
const mNorm = m / 100;
const yNorm = y / 100;
const kNorm = k / 100;
// Calculate RGB
const r = 255 * (1 - cNorm) * (1 - kNorm);
const g = 255 * (1 - mNorm) * (1 - kNorm);
const b = 255 * (1 - yNorm) * (1 - kNorm);
return {
red: Math.round(r),
green: Math.round(g),
blue: Math.round(b)
};
}HSL provides an intuitive way to work with colors:
// HSL color representation
const hslColor = {
hue: 240, // 0-360 degrees
saturation: 75, // 0-100%
lightness: 50 // 0-100%
};
// RGB to HSL conversion
function rgbToHsl(r, g, b) {
r /= 255;
g /= 255;
b /= 255;
const max = Math.max(r, g, b);
const min = Math.min(r, g, b);
const diff = max - min;
// Lightness
const l = (max + min) / 2;
let h, s;
if (diff === 0) {
h = s = 0; // Achromatic
} else {
// Saturation
s = l > 0.5 ? diff / (2 - max - min) : diff / (max + min);
// Hue
switch (max) {
case r:
h = (g - b) / diff + (g < b ? 6 : 0);
break;
case g:
h = (b - r) / diff + 2;
break;
case b:
h = (r - g) / diff + 4;
break;
}
h /= 6;
}
return {
hue: Math.round(h * 360),
saturation: Math.round(s * 100),
lightness: Math.round(l * 100)
};
}
// HSL to RGB conversion
function hslToRgb(h, s, l) {
h /= 360;
s /= 100;
l /= 100;
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;
};
let r, g, b;
if (s === 0) {
r = g = b = l; // Achromatic
} else {
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 {
red: Math.round(r * 255),
green: Math.round(g * 255),
blue: Math.round(b * 255)
};
}HSV (also known as HSB - Hue, Saturation, Brightness) is another intuitive color model:
// HSV color representation
const hsvColor = {
hue: 240, // 0-360 degrees
saturation: 75, // 0-100%
value: 80 // 0-100% (brightness)
};
// RGB to HSV conversion
function rgbToHsv(r, g, b) {
r /= 255;
g /= 255;
b /= 255;
const max = Math.max(r, g, b);
const min = Math.min(r, g, b);
const diff = max - min;
// Value
const v = max;
// Saturation
const s = max === 0 ? 0 : diff / max;
// Hue
let h;
if (diff === 0) {
h = 0;
} else {
switch (max) {
case r:
h = (g - b) / diff + (g < b ? 6 : 0);
break;
case g:
h = (b - r) / diff + 2;
break;
case b:
h = (r - g) / diff + 4;
break;
}
h /= 6;
}
return {
hue: Math.round(h * 360),
saturation: Math.round(s * 100),
value: Math.round(v * 100)
};
}
// HSV to RGB conversion
function hsvToRgb(h, s, v) {
h /= 360;
s /= 100;
v /= 100;
const c = v * s;
const x = c * (1 - Math.abs((h * 6) % 2 - 1));
const m = v - c;
let r, g, b;
if (h < 1/6) {
[r, g, b] = [c, x, 0];
} else if (h < 2/6) {
[r, g, b] = [x, c, 0];
} else if (h < 3/6) {
[r, g, b] = [0, c, x];
} else if (h < 4/6) {
[r, g, b] = [0, x, c];
} else if (h < 5/6) {
[r, g, b] = [x, 0, c];
} else {
[r, g, b] = [c, 0, x];
}
return {
red: Math.round((r + m) * 255),
green: Math.round((g + m) * 255),
blue: Math.round((b + m) * 255)
};
}CIELAB provides perceptually uniform color representation:
// LAB color representation
const labColor = {
l: 50, // Lightness: 0-100
a: 25, // Green-Red axis: -128 to +127
b: -50 // Blue-Yellow axis: -128 to +127
};
// RGB to LAB conversion (simplified)
function rgbToLab(r, g, b) {
// First convert RGB to XYZ
const xyz = rgbToXyz(r, g, b);
// Then XYZ to LAB
return xyzToLab(xyz.x, xyz.y, xyz.z);
}
// XYZ to LAB conversion
function xyzToLab(x, y, z) {
// Reference white D65
const xn = 95.047;
const yn = 100.000;
const zn = 108.883;
const fx = labF(x / xn);
const fy = labF(y / yn);
const fz = labF(z / zn);
const l = 116 * fy - 16;
const a = 500 * (fx - fy);
const b = 200 * (fy - fz);
return { l, a, b };
}
function labF(t) {
const delta = 6 / 29;
return t > delta ** 3 ? Math.cbrt(t) : t / (3 * delta ** 2) + 4 / 29;
}
// Calculate color difference (Delta E)
function deltaE(lab1, lab2) {
const deltaL = lab1.l - lab2.l;
const deltaA = lab1.a - lab2.a;
const deltaB = lab1.b - lab2.b;
return Math.sqrt(deltaL ** 2 + deltaA ** 2 + deltaB ** 2);
}Used in video compression and broadcasting:
// YUV color representation
const yuvColor = {
y: 128, // Luminance (brightness)
u: 64, // Blue-Luminance difference
v: 192 // Red-Luminance difference
};
// RGB to YUV conversion (BT.601)
function rgbToYuv(r, g, b) {
const y = 0.299 * r + 0.587 * g + 0.114 * b;
const u = -0.14713 * r - 0.28886 * g + 0.436 * b + 128;
const v = 0.615 * r - 0.51499 * g - 0.10001 * b + 128;
return {
y: Math.round(y),
u: Math.round(u),
v: Math.round(v)
};
}
// YUV to RGB conversion
function yuvToRgb(y, u, v) {
const r = y + 1.13983 * (v - 128);
const g = y - 0.39465 * (u - 128) - 0.58060 * (v - 128);
const b = y + 2.03211 * (u - 128);
return {
red: Math.max(0, Math.min(255, Math.round(r))),
green: Math.max(0, Math.min(255, Math.round(g))),
blue: Math.max(0, Math.min(255, Math.round(b)))
};
}| Color Model | Best For | Advantages | Disadvantages |
|---|---|---|---|
| RGB | Digital displays, web | Direct device mapping | Not perceptually uniform |
| CMYK | Printing | Matches ink behavior | Smaller gamut than RGB |
| HSL | User interfaces | Intuitive for humans | Not perceptually uniform |
| HSV | Color pickers | Brightness control | Less intuitive than HSL |
| LAB | Color science | Perceptually uniform | Complex calculations |
| YUV | Video compression | Separates luminance | Limited general use |
class ColorModelConverter {
constructor(color, model) {
this.color = color;
this.model = model;
}
// Convert to any supported model
to(targetModel) {
if (this.model === targetModel) {
return this.color;
}
// Convert through RGB as intermediate
const rgb = this.toRgb();
return this.fromRgb(rgb, targetModel);
}
toRgb() {
switch (this.model) {
case 'rgb':
return this.color;
case 'hsl':
return hslToRgb(this.color.hue, this.color.saturation, this.color.lightness);
case 'hsv':
return hsvToRgb(this.color.hue, this.color.saturation, this.color.value);
case 'cmyk':
return cmykToRgb(this.color.cyan, this.color.magenta, this.color.yellow, this.color.black);
default:
throw new Error(`Unsupported source model: ${this.model}`);
}
}
fromRgb(rgb, targetModel) {
switch (targetModel) {
case 'rgb':
return rgb;
case 'hsl':
return rgbToHsl(rgb.red, rgb.green, rgb.blue);
case 'hsv':
return rgbToHsv(rgb.red, rgb.green, rgb.blue);
case 'cmyk':
return rgbToCmyk(rgb.red, rgb.green, rgb.blue);
case 'lab':
return rgbToLab(rgb.red, rgb.green, rgb.blue);
default:
throw new Error(`Unsupported target model: ${targetModel}`);
}
}
// Get color in CSS format
toCss() {
const rgb = this.toRgb();
return `rgb(${rgb.red}, ${rgb.green}, ${rgb.blue})`;
}
// Get color in hex format
toHex() {
const rgb = this.toRgb();
return rgbToHex(rgb.red, rgb.green, rgb.blue);
}
}
// Usage examples
const redHsl = new ColorModelConverter(
{ hue: 0, saturation: 100, lightness: 50 },
'hsl'
);
console.log(redHsl.to('rgb')); // { red: 255, green: 0, blue: 0 }
console.log(redHsl.to('cmyk')); // { cyan: 0, magenta: 100, yellow: 100, black: 0 }
console.log(redHsl.toCss()); // rgb(255, 0, 0)
console.log(redHsl.toHex()); // #ff0000Explore these comprehensive pillar articles related to Color Models
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.
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.
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.
Discover more about Color Models and related color theory concepts
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.
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 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.
Discover more color terminology and concepts in our comprehensive glossary.
Browse Full Glossary