Color Converter & Hex/RGB/HSL Picker
Convert colors between HEX, RGB, RGBA, HSL, and CMYK formats with an interactive swatch picker.
Unlike conventional calculator sites that send your financial numbers, payload data, or code snippets across remote web servers, Affi-Chain processes all mathematical derivations, algorithms, and data parsing 100% locally within your browser sandbox. No tracking cookies, no server-side retention, and zero transmission lag.
What is Color Space Conversion?
Color Space Conversion is the mathematical translation of color coordinates between different digital and physical color representation models. In computer graphics, software engineering, and graphic design, color is represented mathematically through structured coordinate systems known as color spaces.
Because different devices capture, render, and reproduce colors through radically different physical mechanisms—such as phosphors emitting additive photons on an OLED display versus physical cyan, magenta, yellow, and black pigments absorbing reflected light on a printing press—no single color space fits every medium. Software engineers must seamlessly convert color values between HEX, RGB, HSL, CMYK, and modern wide-gamut spaces like OKLCH while preserving visual fidelity.
The Affi-Chain Multi-Format Color Converter provides instant, 100% client-side conversion across all major color standards, displaying real-time visual previews, contrast scores, and ready-to-copy CSS properties.
The Primary Color Spaces & Mathematical Formulas
Digital color pipelines rely on four core representations, plus modern wide-gamut specifications:
1. HEX (Hexadecimal Color Notation)
Hexadecimal notation is the foundational color syntax of CSS, SVG, and HTML. A standard HEX code represents 24-bit sRGB color using three pairs of base-16 digits:
$$\text{HEX} = \text{"#"} + R_{16} + G_{16} + B_{16}$$
Each channel ($R$, $G$, $B$) ranges from 00 (decimal 0) to FF (decimal 255), allowing $256^3 = 16,777,216$ discrete color combinations. An optional fourth 8-bit pair represents alpha opacity (#RRGGBBAA).
2. RGB & RGBA (Additive Color Model)
The RGB model reproduces colors by mixing Red, Green, and Blue light at varying intensities ($0$ to $255$). Mixing all three channels at maximum intensity yields pure white ($255, 255, 255$); setting all to zero yields pure black ($0, 0, 0$). The extended RGBA model appends an alpha channel ($0.0$ to $1.0$) for transparency.
3. HSL & HSLA (Cylindrical Coordinate Representation)
HSL reformulates RGB coordinates into an intuitive cylindrical coordinate system designed to match human cognitive perception:
- Hue ($H$): The angular position on the color wheel ($0^\circ$ to $360^\circ$). $0^\circ$ is Red, $120^\circ$ is Green, and $240^\circ$ is Blue.
- Saturation ($S$): Color purity or intensity ($0%$ to $100%$). $0%$ produces grayscale; $100%$ produces vivid, fully saturated pigments.
- Lightness ($L$): The photometric luminance ($0%$ to $100%$). $0%$ is absolute black; $50%$ is the pure chromatic hue; $100%$ is pure white.
RGB to HSL Mathematical Derivation:
Given normalized RGB components $r, g, b \in [0, 1]$: $$C_{\max} = \max(r, g, b), \quad C_{\min} = \min(r, g, b), \quad \Delta = C_{\max} - C_{\min}$$ $$\text{Lightness: } L = \frac{C_{\max} + C_{\min}}{2}$$ $$\text{Saturation: } S = \begin{cases} 0 & \text{if } \Delta = 0 \ \frac{\Delta}{1 - |2L - 1|} & \text{if } \Delta \ne 0 \end{cases}$$ $$\text{Hue: } H = \begin{cases} 0^\circ & \text{if } \Delta = 0 \ 60^\circ \times \left( \frac{g - b}{\Delta} \bmod 6 \right) & \text{if } C_{\max} = r \ 60^\circ \times \left( \frac{b - r}{\Delta} + 2 \right) & \text{if } C_{\max} = g \ 60^\circ \times \left( \frac{r - g}{\Delta} + 4 \right) & \text{if } C_{\max} = b \end{cases}$$
4. CMYK (Subtractive Printing Model)
Unlike screens which emit light, commercial offset printing presses apply four physical ink plates—Cyan ($C$), Magenta ($M$), Yellow ($Y$), and Key/Black ($K$)—onto paper, subtracting reflected wavelengths: $$K = 1 - \max(r, g, b)$$ $$C = \frac{1 - r - K}{1 - K}, \quad M = \frac{1 - g - K}{1 - K}, \quad Y = \frac{1 - b - K}{1 - K} \quad (\text{for } K < 1)$$
Comparison Matrix: Color Spaces Across Design & Engineering
| Color Model | Primary Medium | Coordinate Range | Human Intuitiveness | CSS Support Level |
|---|---|---|---|---|
| HEX | Web markup & CSS | #000000 to #FFFFFF | Low (requires mental hex parsing) | Universal (CSS Level 1–4) |
| RGB / RGBA | Displays & Game Engines | 0–255 per channel, $\alpha \in [0, 1]$ | Medium (hardware-aligned) | Universal (CSS Level 1–4) |
| HSL / HSLA | UI Design & Color Themes | $H: 0–360^\circ, S: 0–100%, L: 0–100%$ | High (easy to adjust lightness/saturation) | Universal (CSS Level 3–4) |
| CMYK | Offset & Digital Printing | $0–100%$ per ink channel | Medium (requires print press calibration) | Not supported directly in standard CSS |
| OKLCH | Modern Wide-Gamut Displays | $L: 0–1, C: 0–0.4, H: 0–360^\circ$ | Exceptional (perceptually uniform) | CSS Color Module Level 4 (Modern Browsers) |
Practical Code Implementation: Lossless Color Conversion Suite
Below is a production-tested JavaScript conversion module implementing bidirectional mathematical transformations between HEX, RGB, HSL, and CMYK:
/**
* Converts a 6 or 8-digit HEX code to an RGBA object.
*/
function hexToRgb(hex) {
let cleanHex = hex.replace('#', '');
if (cleanHex.length === 3) {
cleanHex = cleanHex.split('').map(c => c + c).join('');
}
const num = parseInt(cleanHex.slice(0, 6), 16);
const r = (num >> 16) & 255;
const g = (num >> 8) & 255;
const b = num & 255;
const a = cleanHex.length === 8 ? +(parseInt(cleanHex.slice(6, 8), 16) / 255).toFixed(2) : 1;
return { r, g, b, a };
}
/**
* Converts RGB values to standard 6-digit uppercase HEX.
*/
function rgbToHex(r, g, b) {
return '#' + [r, g, b].map(x => {
const hex = Math.round(Math.max(0, Math.min(255, x))).toString(16);
return hex.padStart(2, '0');
}).join('').toUpperCase();
}
/**
* Converts RGB components to HSL percentages.
*/
function rgbToHsl(r, g, b) {
const normR = r / 255;
const normG = g / 255;
const normB = b / 255;
const max = Math.max(normR, normG, normB);
const min = Math.min(normR, normG, normB);
const delta = max - min;
let h = 0;
let s = 0;
const l = (max + min) / 2;
if (delta !== 0) {
s = l > 0.5 ? delta / (2 - max - min) : delta / (max + min);
switch (max) {
case normR: h = ((normG - normB) / delta + (normG < normB ? 6 : 0)) * 60; break;
case normG: h = ((normB - normR) / delta + 2) * 60; break;
case normB: h = ((normR - normG) / delta + 4) * 60; break;
}
}
return {
h: Math.round(h),
s: Math.round(s * 100),
l: Math.round(l * 100)
};
}
/**
* Converts RGB values to commercial CMYK percentages.
*/
function rgbToCmyk(r, g, b) {
const normR = r / 255;
const normG = g / 255;
const normB = b / 255;
const k = 1 - Math.max(normR, normG, normB);
if (k === 1) return { c: 0, m: 0, y: 0, k: 100 };
const c = (1 - normR - k) / (1 - k);
const m = (1 - normG - k) / (1 - k);
const y = (1 - normB - k) / (1 - k);
return {
c: Math.round(c * 100),
m: Math.round(m * 100),
y: Math.round(y * 100),
k: Math.round(k * 100)
};
}
// Verification with Affi-Chain Brand Blue (#3B82F6):
const rgb = hexToRgb('#3B82F6');
console.log('RGB:', rgb); // { r: 59, g: 130, b: 246, a: 1 }
console.log('HSL:', rgbToHsl(rgb.r, rgb.g, rgb.b)); // { h: 217, s: 91, l: 60 }
console.log('CMYK:', rgbToCmyk(rgb.r, rgb.g, rgb.b)); // { c: 76, m: 47, y: 0, k: 4 }
Critical Edge Cases & Common Traps
1. Out-of-Gamut Printing Shifts (RGB to CMYK)
The sRGB color gamut spans significantly more vibrant blues, cyans, and greens than standard ISO 12647-2 commercial ink profiles can reproduce. When vibrant digital neon colors (e.g., #00FF00 pure electric green) are converted directly to CMYK, the colors appear muddy, dull, or dark on paper. Professional workflows require soft-proofing with ICC color profiles rather than naive mathematical CMYK conversion.
2. Floating-Point Round-Trip Degradation
Converting iteratively between color models (e.g., HEX → HSL → RGB → HEX) introduces floating-point rounding errors due to integer truncation ($0$–$255$ to $0.0$–$1.0$ and back). Always preserve original source color strings in application state and derive alternate color spaces on-demand.
3. The Perceptual Non-Uniformity of HSL
In traditional HSL, pure yellow (hsl(60, 100%, 50%)) and pure navy blue (hsl(240, 100%, 50%)) share the exact same Lightness value ($50%$). However, the human eye perceives yellow as radically brighter than dark blue because of human retinal sensitivity. For accessible UI design systems requiring consistent WCAG contrast ratios, migrate to OKLCH, which maintains mathematically uniform perceived lightness across all hues.