ToolNest logoToolNest.

How to Convert RGB to Hex Color Code

Convert each RGB channel (0-255) to a two-digit hexadecimal number, then join them in R-G-B order with a # prefix. For example, rgb(255, 87, 51) becomes #FF5733 because 255=FF, 87=57, and 51=33 in hex. ToolNest's free color converter turns any RGB value into its hex code instantly, in either direction.

RGB to hex in 10 seconds: the whole idea

An RGB color is three numbers — red, green, and blue — each ranging from 0 to 255. A hex color code is the exact same three numbers written in hexadecimal: two digits per channel, joined together after a # sign. So converting RGB to hex is just three small decimal-to-hex conversions. Take rgb(255, 0, 0): 255 in hex is FF, 0 in hex is 00, so the code is #FF0000 — pure red. The channel order never changes: red first, green second, blue third. And every channel must be exactly two hex digits, which means padding with a leading zero whenever a channel is below 16 — 10 becomes 0A, not A. That is the entire concept. The arithmetic below is just the division method for turning each 0-255 number into its two hex digits, the same positional math our hexadecimal guide covers in depth.

RGB to hex formula, worked step by step

The formula is one line: hex = '#' + toHex(R) + toHex(G) + toHex(B), where toHex(n) converts a single 0-255 channel into two hex digits. To convert one channel by hand, divide by 16: the quotient and remainder become the two hex digits (10-15 written as A-F). Worked example — convert rgb(255, 87, 51), a warm coral. Red (255): 255 / 16 = 15 remainder 15. In hex, 15 is F, so the digits are F and F: 'FF'. Green (87): 87 / 16 = 5 remainder 7: '57'. Blue (51): 51 / 16 = 3 remainder 3: '33'. Join them in order with the hash: #FF5733. Second example, with padding — convert rgb(0, 128, 0), the classic web green. Red (0): 0 / 16 = 0 remainder 0: '00'. Green (128): 128 / 16 = 8 remainder 0: '80'. Blue (0): '00'. Result: #008000. Notice how the padding rule bit in the first example's blue channel — a value like 5 would become '05', never '5'. Do two or three by hand to lock in the method, then let a color converter handle the rest.

How to convert hex to RGB

The reverse trip: strip the #, split the six digits into three pairs, and convert each pair from hex to decimal. Take #FF5733 — the coral from above. FF = 15x16 + 15 = 255. 57 = 5x16 + 7 = 87. 33 = 3x16 + 3 = 51. Back to rgb(255, 87, 51) — the round trip confirms the method. Watch for shorthand hex: CSS allows three-digit codes like #F53, where each digit doubles — #F53 means #FF5533, which converts to rgb(255, 85, 51). Expand first, then convert each pair. If you do this conversion often, our hex-to-RGB guide walks through the positional math in full, and the color converter runs both directions in one paste.

RGB vs hex color codes: when to use which

They describe identical colors — the choice is about context, not color. Use hex for static values in CSS: #FF5733 is shorter and more scannable than rgb(255, 87, 51), and every designer on the team reads hex fluently. Use rgb() when you need an alpha channel — rgba(255, 87, 51, 0.5) for 50% opacity has no clean hex equivalent in older CSS (the 8-digit #FF573380 form exists but reads poorly). Use rgb() for computed or dynamic values in preprocessors and JavaScript, where the channels are variables. Design tools like Figma and Photoshop display both and copy either. APIs and config files vary — check what the consumer expects before committing. Hex is case-insensitive (#ff5733 = #FF5733), so pick one casing per project and stay consistent. And when a color needs human-friendly tweaking — 'make it a touch darker' — consider hsl(), which separates hue, saturation, and lightness so one slider does the job instead of recomputing three channels.

Why RGB channels stop at 255

Each RGB channel gets exactly 8 bits of storage — one byte. Eight bits hold 2^8 = 256 distinct values, numbered 0 to 255. That is also why each channel maps to exactly two hex digits: one hex digit encodes 4 bits, so two digits encode the full byte — the same nibble math behind hexadecimal itself. Three channels at 8 bits each = 24 bits per pixel, giving 256^3 = 16,777,216 possible colors — the '16.7 million colors' on every monitor spec sheet. This byte-per-channel design is why hex codes are always six digits plus #: the format is literally three bytes written out. Higher-end displays use 10 bits per channel ('deep color'), but the web's RGB/hex system remains 8-bit — a constraint from the 1980s that stuck because 16.7 million colors already exceeds what human vision distinguishes. Bookmark this relationship — 8 bits = 1 byte = 2 hex digits — because it recurs everywhere in computing: IPv6 addresses, MAC addresses, UUIDs, and color codes are all just bytes written in hex.

Five RGB-to-hex mistakes everyone makes

1. Dropping the leading zero. Channel value 5 converts to '05', not '5' — #5A3C9 is a broken five-digit code, not a color. 2. Converting the whole thing at once. rgb(255, 87, 51) is not the decimal number 2558751 — each channel converts independently. 3. Scrambling the channel order. #3357FF is a blue, not the coral you wanted; R-G-B order is sacred. 4. Forgetting the # prefix. In CSS, FF5733 without the hash is an invalid value — the browser silently ignores the whole declaration. 5. Hex letters past F. There is no 'G' in hexadecimal — channel values above 255 don't exist, so if your math produces one, the input was wrong. When a conversion looks off, recheck each channel separately: value in range 0-255, two digits each, correct order. Or skip the hand math and verify with the converter.

Reading hex codes at a glance

Designers rarely convert — they read hex. The trick is pattern recognition. #FF0000 is pure red, #00FF00 pure green, #0000FF pure blue — one channel maxed, the others zero. Equal channels make grays: #000000 black, #888888 mid-gray, #FFFFFF white. The higher a channel's pair, the stronger that color: in #FF5733, red (FF) dominates, green (57) moderates, blue (33) barely registers — a warm coral. Warm colors lead with red; cool colors lead with blue; muddy or muted colors keep all three channels in the same middle range (#8A7F72). Landmarks worth memorizing: #FFD700 gold, #800080 purple, #FFA500 orange, #008080 teal. Practice by covering the color swatch and guessing from the code — after a week of it, you'll parse most codes without thinking.

Convert instantly with a color converter

Hand conversion builds understanding; a tool builds speed. ToolNest's free color converter translates RGB to hex — and back — instantly: paste rgb(255, 87, 51), get #FF5733, no arithmetic. It runs entirely in your browser, so palette experiments never leave your device. Use the manual method when you're learning or double-checking someone's code; use the tool when you're shipping. Either way, the byte-per-channel logic above is what makes both directions work.

Do it in one click

Convert any RGB value to hex instantly — free, no signup.

Open the Free Tool →