An RGB to hex converter takes three separate red, green and blue numbers — each from 0 to 255 — and packs them into the compact 6-character hex code that CSS, HTML and design tools use everywhere. Drag the sliders above or type the numbers directly, and this tool instantly builds the matching HEX, an 8-digit HEX with alpha, an RGBA string, and the equivalent HSL value — every one of them copy-ready.
At Arb Digital, this is the conversion our developers reach for when a color starts life as code rather than a design file — a value returned from a canvas pixel read, a color generated by a JavaScript theming function, or numbers pulled from a client's brand spreadsheet. Turning those raw RGB numbers into a clean hex string by hand, digit by digit, is tedious and easy to get wrong, so this tool does it in real time as you adjust each channel.
What This RGB to Hex Converter Does
Set the red, green and blue channels using the sliders or the number fields — they stay perfectly in sync with each other — and the tool immediately renders the resulting color as a swatch alongside four output formats: the standard 6-digit hex, an 8-digit hex that folds in your alpha value, the RGBA string, and HSL. Add an optional alpha value between 0 and 1 if you need a translucent color, and the 8-digit hex and RGBA outputs update to include it automatically.
Every value entered is validated and clamped to the valid 0–255 range for each color channel, so you can't accidentally generate a broken hex code by typing 300 into a field or a stray negative number — the converter simply pulls the value back into range and shows you the corrected result.
How to Use the RGB to Hex Converter
- Set the Red value. Drag the R slider or type a number from 0 to 255 into the matching field — both stay linked.
- Set Green and Blue the same way. Adjust each slider or number field until the swatch shows the color you want.
- Add alpha if needed. Enter a value between 0 (fully transparent) and 1 (fully opaque) to also generate an 8-digit hex and an RGBA string with transparency.
- Read the hex code. The headline result shows the standard 6-digit hex — copy it directly for most CSS uses.
- Copy any format you need. Individual copy buttons sit next to the 8-digit hex, HSL and RGBA outputs.
The Formula — How RGB to Hex Conversion Works
This is the mirror image of a hex-to-RGB conversion, and it's just as mechanical once you see the pattern: each 0–255 decimal channel becomes a two-digit hexadecimal pair, zero-padded when necessary. Hexadecimal is base-16, so it needs exactly two digits to represent any value from 0 to 255 — the digits run 0–9 then A–F, giving 16 possible symbols per position, and two positions give 16 × 16 = 256 possible values, which lines up exactly with the 256 possible values (0–255) of an RGB channel. Take red = 27: convert 27 to base-16 and you get 1B. Green = 58 becomes 3A. Blue = 107 becomes 6B. Glue the three pairs together in order and you get #1B3A6B — precisely what this tool outputs for the default values above.
The zero-padding step matters more than it looks: a channel like 5 converts to hex as just 5, a single digit — but a valid hex color pair always needs two digits, so it must be written as 05, not 5. Skip that padding step and you get a hex string that's the wrong length and parses incorrectly. This converter always pads every pair to two digits automatically, which is one of the most common bugs to introduce when writing this conversion by hand in a hurry. As MDN's hex-color documentation notes, a valid hex color must be exactly 3, 4, 6 or 8 digits — never an odd or inconsistent length — which is exactly why the padding step isn't optional.
Clamping Out-of-Range RGB Values
A valid RGB channel in CSS only accepts 0 to 255 — nothing outside that range is meaningful, because there simply is no "256" or "negative 10" amount of red light on a screen. But in real code, out-of-range numbers show up more often than you'd expect: a calculation that overshoots during a color-blending function, a value read from user input without validation, or an animation easing function that briefly overshoots its target before settling. Rather than producing a garbled or invalid hex string, this converter clamps every channel — any value below 0 is treated as 0, and anything above 255 is treated as 255 — before it builds the hex output. That mirrors how you should handle RGB values defensively in your own code: clamp before you convert, not after, so a broken input never reaches your stylesheet or your rendering layer as an invalid color.
When You'd Generate Hex Programmatically
Most people encounter hex codes as something a designer hands them, but plenty of real development work runs the other direction — generating hex color codes from RGB numbers your code already has:
- Canvas and image processing. Reading pixel data from an HTML canvas returns RGB(A) values directly. If you need to display that color as a CSS value elsewhere on the page — a color swatch, a picked-color label — you convert it to hex first.
- Data visualization and theming. A chart library that computes colors along a gradient scale, or a design system that generates tints and shades programmatically, usually works internally with RGB or HSL numbers and needs a hex output for CSS custom properties or inline styles.
- Color extraction tools. A "dominant color" or "average color" algorithm run against an image typically averages pixel RGB values first, then converts the result to hex for display or storage.
- API and design-token pipelines. Design tokens exported from a design tool sometimes arrive as RGB objects and need converting to hex to match a CSS variable naming convention used across a codebase.
Why Hex Is More Compact Than RGB for Static Colors
For a color that never changes — a brand primary, a fixed border color, a static background — hex is simply shorter to write and read than the equivalent rgb() function: #1B3A6B is eight characters including the hash, while rgb(27, 58, 107) is nineteen. Multiply that difference across a large stylesheet with hundreds of color references and the file-size and readability gap adds up. Hex also matches what most design tools display by default, so a hex-first stylesheet tends to line up more closely with what a designer sees in their color picker, reducing translation errors during handoff. RGB earns its keep specifically when you need transparency (via RGBA) or when a value needs to be calculated or interpolated at runtime — for a color that's simply fixed in your CSS, hex remains the more compact, more common choice.
Arb Digital designs and builds fast, beautifully-crafted websites with a clean, consistent color system from the very first line of CSS — no more converting values by hand across a dozen files.
See Our Web Design Services All Free ToolsCommon Mistakes to Avoid
- Forgetting to zero-pad single-digit hex values. A channel that converts to a single hex digit (like 5, which is just "5" in hex) must be written as "05" — otherwise the resulting hex string has the wrong length.
- Confusing alpha as a percentage. The alpha pair appended to an 8-digit hex code is itself a hex value from 00 to FF, not a direct 0–100 percentage — this tool converts your 0–1 alpha into the correct hex pair for you.
- Not clamping input values. RGB numbers above 255 or below 0 aren't valid CSS colors; always clamp before converting, whether by hand or in code.
- Mixing RGB percentage and integer syntax. CSS supports rgb(10%, 20%, 40%) as an alternative syntax, but don't mix percentages and 0–255 integers within the same color value.
- Assuming uppercase and lowercase hex differ. They don't — #1b3a6b and #1B3A6B are the identical color; use whichever case matches your project's style guide consistently.
Related Free Tools From Arb Digital
Need to go the other direction? Use the hex to RGB converter to turn a hex code back into R, G, B numbers. Once you have your color, build a full background with the CSS gradient generator, add depth with the box shadow generator, check readability with the color contrast checker, or explore any color visually with the color picker & converter. Browse every tool at our free online tools hub.
Frequently Asked Questions
Convert each of the three RGB channels — red, green, blue — from its 0–255 decimal value into a two-digit base-16 (hexadecimal) pair, padding with a leading zero if the result is a single digit. Then join the three pairs together with a leading hash. For example, rgb(27, 58, 107) becomes 1B, 3A, 6B, joined as #1B3A6B.
This converter automatically clamps any channel value above 255 down to 255, and any value below 0 up to 0, before generating the hex code. That guarantees the output is always a valid hex color even if an input number is out of the standard range.
Add an alpha value between 0 and 1 in the alpha field. This converter turns that into a two-digit hex pair appended to the standard 6-digit hex, producing an 8-digit hex color, and also reflects it in the RGBA output. Note that 8-digit hex alpha support requires a reasonably modern browser.
The 3-digit shorthand only works when both digits of every channel are identical, such as #ff0000 shortening to #f00. Most real colors, including typical brand colors, don't meet that condition, so they're written in full as 6-digit hex, which is what this converter always outputs for accuracy.
No. #1B3A6B and #1b3a6b represent the exact same color in CSS — hex color codes are case-insensitive. This tool outputs uppercase by convention for readability, but either case works identically in a browser.
Yes — completely free, with no sign-up or usage limits. Every conversion happens locally in your browser using plain JavaScript; nothing you enter is stored or transmitted anywhere.