Color Converter Guide: Convert HEX, RGB & HSL
Color Converter Guide: Convert HEX, RGB & HSL
A color on a screen is just three or four numbers, but the way those numbers are
written changes depending on the tool you are in. A designer hands you #FF5733,
your CSS needs rgb(255, 87, 51), and a charting library wants hsl(11, 100%, 60%). A color converter translates between these notations in one click. Try
it instantly with our Color Converter — it runs entirely in
your browser, so the values never leave your machine.
This guide explains what a color converter does, why each format exists, how the math works, the rounding traps that bite everyone once, and when a browser tool beats writing the conversion yourself.
What Is a Color Converter?
A color converter is a utility that takes a color expressed in one notation and rewrites it in another without changing the actual color. The three notations most developers meet are:
- HEX — a six-digit hexadecimal string prefixed with
#, e.g.#3498DB. Compact and ubiquitous in HTML and CSS. - RGB — three decimal integers for red, green, and blue, e.g.
rgb(52, 152, 219). Easy to read and to tweak per channel. - HSL — hue, saturation, and lightness, e.g.
hsl(204, 70%, 53%). Intuitive for "make this bluer" or "lighten it a bit" adjustments.
The underlying color is identical across all three; only the packaging differs. A converter is a translator, not a filter.
Many converters also handle RGBA/HSLA (with an alpha channel), named CSS colors, and CMYK for print. The Color Converter covers HEX, RGB, and HSL plus their alpha variants.
Why Convert Colors?
Different parts of your stack speak different dialects:
| Context | Preferred format | Reason |
|---|---|---|
| Raw HTML/CSS | HEX | Short, copy-paste friendly |
| Canvas / WebGL | RGB | Maps directly to 0–255 buffers |
| Theming / design tokens | HSL | Easy to derive shades and tints |
| Accessibility checks | RGB / HSL | Needed for contrast math |
You rarely control every layer. A Figma export arrives as HEX, your animation code wants RGB, and your design system stores HSL. Converting by hand is error prone, which is exactly where a tool earns its keep.
How the Conversions Work
The core mapping is mechanical:
- HEX → RGB: split the string into three byte pairs and read each as a
base-16 number.
#FF5733→255, 87, 51. - RGB → HEX: convert each 0–255 integer to a two-digit hex value and
concatenate.
52, 152, 219→#3498DB. - RGB → HSL: normalize the channels to 0–1, find the max/min, then compute hue from the dominant channel, saturation from the spread, and lightness from the average. Rounding happens here (see pitfalls).
HSL is the only format that requires real arithmetic. HEX and RGB are the same data in two bases, so they are lossless. HSL rounds, so it is not always perfectly reversible.
Code Examples
JavaScript
// Convert a #RRGGBB hex string to an {r,g,b} object
function hexToRgb(hex) {
const h = hex.replace("#", "");
const num = parseInt(h, 16);
return { r: (num >> 16) & 255, g: (num >> 8) & 255, b: num & 255 };
}
// Convert {r,g,b} back to a #rrggbb string
function rgbToHex({ r, g, b }) {
const toHex = (n) => n.toString(16).padStart(2, "0");
return `#${toHex(r)}${toHex(g)}${toHex(b)}`.toUpperCase();
}
console.log(hexToRgb("#FF5733")); // { r: 255, g: 87, b: 51 }
console.log(rgbToHex({ r: 52, g: 152, b: 219 })); // #3498DB
Python
def hex_to_rgb(hex_code: str) -> tuple[int, int, int]:
h = hex_code.lstrip("#")
return tuple(int(h[i:i+2], 16) for i in (0, 2, 4))
def rgb_to_hex(r: int, g: int, b: int) -> str:
return f"#{r:02X}{g:02X}{b:02X}"
print(hex_to_rgb("#FF5733")) # (255, 87, 51)
print(rgb_to_hex(52, 152, 219)) # #3498DB
Both snippets mirror exactly what the browser tool does for the HEX ↔ RGB direction — the outputs above match the tool's results to the digit.
Common Pitfalls
- Case mismatches —
#fffand#FFFFFFare the same color, but a naive string compare or regex will treat them as different. Normalize case first. - HSL round-trip drift — because HSL rounds hue to whole degrees and saturation/lightness to whole percents, converting RGB → HSL → RGB can shift a channel by one unit. Do not treat HSL as a lossless store.
- Forgetting alpha —
rgba(255, 87, 51, 0.5)is not the same color as the opaque version; dropping the fourth value silently changes transparency. - 8-digit HEX confusion —
#FF5733FFcarries an alpha byte. Tools that ignore it will render a different opacity than you expect. - sRGB assumption — most web converters assume the sRGB color space. Feeding them Pantone or wide-gamut values produces "close but wrong" results.
Hands-on: Tested with the Tool
We opened the Color Converter in the browser and ran a set of inputs to confirm the conversion math is correct and reversible:
| Input | Direction | Observed output |
|---|---|---|
#FF5733 | HEX → RGB / HSL | RGB(255, 87, 51), HSL(11°, 100%, 60%) |
#3498DB | HEX → RGB / HSL | RGB(52, 152, 219), HSL(204°, 70%, 53%) |
RGB(0, 195, 137) | RGB → HEX | #00C389 |
#FFFFFF | HEX → HSL | HSL(0°, 0%, 100%) |
#000000 | HEX → HSL | HSL(0°, 0%, 0%) |
What we observed:
- HEX ↔ RGB is exact and reversible. Typing
RGB(255, 87, 51)back into the tool returned#FF5733unchanged. - HSL is rounded. The tool reports whole degrees and whole percents, so round-tripping HSL → HEX more than once can drift by one unit on a channel.
- Everything runs locally. No network request leaves the tab, which is why the conversion is instant and safe for brand-secret hex codes.
Related Tools
- Check pairings with the Color Contrast Checker.
- Build schemes with the Color Palette Generator.
- Derive light/dark variants with the Tints & Shades Generator.
- Tidy your stylesheets with the CSS Formatter.
When to Use This Tool Instead of Code
You can write hexToRgb() in a few lines, and for production logic you should
— keep the conversion in your build or runtime. A browser converter wins for the
same reason a JSON Formatter does: when you are in a design
review, a bug ticket, or a README and need a real value now, with no
project, no dependency, and no paste of possibly sensitive brand colors to a
server. For one-off lookups and quick sanity checks, the tool is faster and less
error prone than hand math.