ColorSwatches.org

Hex ↔ RGB Converter

Enter a hex color code to get its RGB values — or enter R, G, B numbers (0–255) to get the hex code. Both sides update in real time. Supports 3-digit shorthand (#F0A) and 6-digit (#FF00AA) hex codes.

Hex Code
RGB Values
rgb(58, 123, 213)

About Hex ↔ RGB Conversion

Hexadecimal color codes and RGB values are two notations for the same color space. Hex encodes all three channels as a single compact string; RGB lists them as three separate decimal integers. Converting in either direction is a straightforward base-16 ↔ base-10 translation applied independently to each channel.

The Formula

A hex color like #3A7BD5 is split into three two-character pairs, each encoding one channel in base 16 (hexadecimal):

\[ \text{Hex: } \underbrace{3A}_{\text{Red}} \underbrace{7B}_{\text{Green}} \underbrace{D5}_{\text{Blue}} \]

Each pair is converted to decimal using the standard positional formula for base 16. For a two-digit hex number with digits \(d_1\) (high) and \(d_2\) (low):

\[ V = 16 \times d_1 + d_2 \]

Where digits A–F have values 10–15. For example:

\[ 3A_{16} = 16 \times 3 + 10 = 58_{10} \] \[ 7B_{16} = 16 \times 7 + 11 = 123_{10} \] \[ D5_{16} = 16 \times 13 + 5 = 213_{10} \]

So #3A7BD5 = rgb(58, 123, 213).

3-Digit Shorthand

A 3-digit hex code like #F0A is shorthand where each digit is doubled: #F0A#FF00AA. This only works when both digits in each pair would be identical.

Converting RGB → Hex

Going the other way, each channel value \( V \) (0–255) is expressed as two base-16 digits using integer division and modulo:

\[ \text{high digit} = \left\lfloor V / 16 \right\rfloor, \qquad \text{low digit} = V \bmod 16 \]

Digits 10–15 map to A–F. For example: R = 58 → \(\lfloor 58/16 \rfloor = 3\), \(58 \bmod 16 = 10 = \text{A}\) → 3A. Prepend # and concatenate all three pairs to get the hex code.


Frequently Asked Questions

What does each hex digit represent?

Hex uses 16 digits: 0–9 and A–F (where A=10, B=11, C=12, D=13, E=14, F=15). Two hex digits can represent values from 00 (0) to FF (255), which matches the 0–255 range of each RGB channel.

Are hex colors case-sensitive?

No. #ff6b35, #FF6B35, and #Ff6B35 all represent the same color. CSS accepts both cases.

What is the difference between hex and rgb() in CSS?

They encode the same color space. #3A7BD5 and rgb(58, 123, 213) produce identical results in a browser. rgb() also accepts an optional alpha channel: rgba(58, 123, 213, 0.5). Hex supports transparency via an 8-digit form: #3A7BD580.

Can hex codes contain more than 6 digits?

Yes — 8-digit hex codes include an alpha (opacity) channel as the last two digits. For example, #3A7BD5FF is fully opaque and #3A7BD500 is fully transparent.

More Color Tools

Browse all tools →