RGB to Hex Color Conversion: A Complete Reference Guide
RGB to Hex conversion is the process of translating a color defined by its red, green, and blue components into a six-digit hexadecimal representation used extensively in web development. Understanding how these formats relate helps you work more effectively with CSS, design tools, and color manipulation in code.
Color Models Explained
RGB (Red, Green, Blue)
RGB is an additive color model where each color component is an integer from 0 to 255. The combination of three values produces over 16 million possible colors.
rgb(255, 0, 0) → pure red
rgb(0, 255, 0) → pure green
rgb(0, 0, 255) → pure blue
rgb(255, 255, 255) → white
rgb(0, 0, 0) → black
rgb(128, 128, 128) → middle gray
Hexadecimal (Hex)
Hex color codes use six hexadecimal digits grouped as RRGGBB. Each pair represents the intensity of red, green, or blue in base-16 notation, where 00 is the minimum and FF is the maximum.
#FF0000 → red
#00FF00 → green
#0000FF → blue
#FFFFFF → white
#000000 → black
#808080 → gray
HSL (Hue, Saturation, Lightness)
HSL represents colors as an angle on a color wheel (0-360 degrees), plus saturation and lightness percentages. It is more intuitive for humans because you can adjust the lightness or saturation independently.
hsl(0, 100%, 50%) → red
hsl(120, 100%, 50%) → green
hsl(240, 100%, 50%) → blue
hsl(0, 0%, 50%) → middle gray
CMYK (Cyan, Magenta, Yellow, Key/Black)
CMYK is a subtractive color model used for printing. Values range from 0% to 100% for each component. It is the inverse of RGB — adding more ink reduces reflected light.
cmyk(0%, 100%, 100%, 0%) → red
cmyk(100%, 0%, 100%, 0%) → green
cmyk(0%, 0%, 0%, 100%) → black
cmyk(0%, 0%, 0%, 0%) → white
Conversion Formulas
RGB to Hex
Convert each RGB component to a two-digit hexadecimal value and concatenate them:
R = 255 → FF
G = 128 → 80
B = 64 → 40
Result: #FF8040
function rgbToHex(r, g, b) {
const toHex = (n) => {
const hex = Math.max(0, Math.min(255, n)).toString(16);
return hex.length === 1 ? '0' + hex : hex;
};
return `#${toHex(r)}${toHex(g)}${toHex(b)}`.toUpperCase();
}
console.log(rgbToHex(255, 128, 64)); // #FF8040
function rgbToHex(int $r, int $g, int $b): string {
$r = max(0, min(255, $r));
$g = max(0, min(255, $g));
$b = max(0, min(255, $b));
return sprintf('#%02X%02X%02X', $r, $g, $b);
}
echo rgbToHex(255, 128, 64); // #FF8040
Hex to RGB
Parse the six-digit hex string into three pairs and convert each from base-16 to base-10:
function hexToRgb(hex) {
const clean = hex.replace('#', '');
const r = parseInt(clean.substring(0, 2), 16);
const g = parseInt(clean.substring(2, 4), 16);
const b = parseInt(clean.substring(4, 6), 16);
return { r, g, b };
}
console.log(hexToRgb('#FF8040')); // { r: 255, g: 128, b: 64 }
def hex_to_rgb(hex_color: str) -> tuple:
clean = hex_color.lstrip('#')
return tuple(int(clean[i:i+2], 16) for i in (0, 2, 4))
print(hex_to_rgb('#FF8040')) # (255, 128, 64)
RGB to HSL
HSL conversion involves finding the dominant and secondary color components to determine the hue angle:
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 l = (max + min) / 2;
if (max === min) return { h: 0, s: 0, l: Math.round(l * 100) };
const d = max - min;
const s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
let h = 0;
if (max === r) h = ((g - b) / d + (g < b ? 6 : 0)) * 60;
else if (max === g) h = ((b - r) / d + 2) * 60;
else h = ((r - g) / d + 4) * 60;
return { h: Math.round(h), s: Math.round(s * 100), l: Math.round(l * 100) };
}
console.log(rgbToHsl(255, 128, 64)); // { h: 20, s: 100, l: 63 }
Quick Reference Table
| RGB | Hex | HSL | Color Name |
|---|---|---|---|
| 255, 0, 0 | #FF0000 | hsl(0, 100%, 50%) | Red |
| 0, 255, 0 | #00FF00 | hsl(120, 100%, 50%) | Lime |
| 0, 0, 255 | #0000FF | hsl(240, 100%, 50%) | Blue |
| 255, 255, 0 | #FFFF00 | hsl(60, 100%, 50%) | Yellow |
| 0, 255, 255 | #00FFFF | hsl(180, 100%, 50%) | Cyan |
| 255, 0, 255 | #FF00FF | hsl(300, 100%, 50%) | Magenta |
| 0, 0, 0 | #000000 | hsl(0, 0%, 0%) | Black |
| 255, 255, 255 | #FFFFFF | hsl(0, 0%, 100%) | White |
| 128, 128, 128 | #808080 | hsl(0, 0%, 50%) | Gray |
| 128, 0, 0 | #800000 | hsl(0, 100%, 25%) | Maroon |
| 0, 128, 0 | #008000 | hsl(120, 100%, 25%) | Green |
| 0, 0, 128 | #000080 | hsl(240, 100%, 25%) | Navy |
| 255, 165, 0 | #FFA500 | hsl(39, 100%, 50%) | Orange |
| 128, 128, 0 | #808000 | hsl(60, 100%, 25%) | Olive |
| 128, 0, 128 | #800080 | hsl(300, 100%, 25%) | Purple |
CSS Color Functions
Modern CSS supports color functions that go beyond basic hex codes:
/* Traditional hex */
.element { color: #FF8040; }
/* RGB with alpha */
.element { color: rgba(255, 128, 64, 0.5); }
/* HSL with alpha */
.element { color: hsla(20, 100%, 63%, 0.5); }
/* Modern relative color syntax */
.element { color: hsl(from #FF8040 h s calc(l + 10%)); }
/* OKLCH — perceptually uniform color space */
.element { color: oklch(0.7 0.15 45); }
Common Use Cases
- Web development — Translating design mockups (RGB/HSL) to CSS (Hex)
- Data visualization — Generating color scales programmatically
- Image processing — Reading pixel data in RGB and storing as hex
- Brand guidelines — Maintaining consistent color across formats
- Color accessibility — Calculating contrast ratios between colors
Online Tool
The RGB to HEX Converter tool on Help2Code converts colors between RGB, HEX, RGBA, HSL, and CMYK with live preview. Pick a color, paste a hex code, or generate a random color to see all four formats instantly. The color preview updates in real time, and you can copy any format with one click. The Color Palette Extractor tool is also useful for extracting color schemes from images.
Conclusion
Understanding RGB to Hex conversion — and the relationship between color models — is essential for web developers and designers. The conversion is straightforward: each RGB channel converts to a two-digit hex pair. Once you know the pattern, moving between RGB, Hex, HSL, and CMYK becomes second nature. Use the RGB to HEX Converter tool for instant conversions and the code examples above for integrating color logic into your projects.