The RGB to hexadecimal formula is: HEX = "#" + two-digit base-16(R) + two-digit base-16(G) + two-digit base-16(B), with each RGB channel ranging from 0 to 255.
RGB and HEX are two notations for the same color values. The conversion changes how each channel is written, not the underlying color model: convert red, green, and blue separately, give every channel two hexadecimal digits, and join them in that order.

The formula at a glance
Each part of the formula has a specific job. RGB values are decimal channel measurements, while each pair in a six-digit HEX code is one channel written in base 16. The two-digit requirement matters because one hexadecimal digit has only 16 possible values, but two digits cover 256 values—the same range used by an 8-bit RGB channel.
| Component | Meaning | Required form |
|---|---|---|
| R, G, and B | Red, green, and blue channel values | Decimal integers from 0 to 255 |
| HEX(R), HEX(G), and HEX(B) | One RGB channel expressed in base 16 | Exactly two characters, from 00 to FF |
| #RRGGBB | Complete HEX color | Three channel pairs in red, green, blue order |
The # symbol identifies the value as a HEX color. After it, the first two characters represent red, the next two represent green, and the final two represent blue. Every pair must remain in its assigned position; moving a pair changes the color rather than merely changing its notation.
Zero-padding is another essential part of the formula. A channel that would fit into one hexadecimal digit still needs two positions in the output. This is why a normalized HEX result always contains six digits, even when one of those digits is zero.
Convert RGB channels by hand
Start with one decimal channel and divide it by 16. The quotient supplies the first hexadecimal digit, and the remainder supplies the second. Remainders from 10 through 15 use the letters A through F. Repeat this for green and blue, then place the three two-digit results together after the # symbol.
For one complete example, take the RGB value (59, 130, 246). Decimal 59 converts to base-16 3B, decimal 130 converts to 82, and decimal 246 converts to F6. Joining the pairs in red-green-blue order and normalizing the letters to lowercase produces #3b82f6.
Two errors account for most incorrect hand conversions. First, do not convert the three RGB values as if they were one combined number; each channel is converted independently. Second, do not drop a leading zero from a channel pair. Keeping the pairs at two characters preserves the fixed #RRGGBB structure expected by CSS, HTML, and common design tools.
Reverse the formula for HEX input
To convert in the opposite direction, remove the optional # symbol and divide the remaining characters into three pairs: RR, GG, and BB. Interpret each pair as a base-16 number and return the results as decimal red, green, and blue values.
RGB = (base-16(RR), base-16(GG), base-16(BB))
For example, the RED pair represents the red channel, GREEN represents green, and BLUE represents blue. A three-digit shorthand code must first be expanded by doubling each digit so that it has the same six-character channel structure. Letter case does not affect the value, and a leading # is optional when entering a HEX code.
For valid 8-bit RGB colors, the two notations represent the same 24-bit color space. HEX therefore gives you a compact way to store or transmit the channel values without changing their precision.
Convert values in the browser tool
- Open the RGB To HEX converter in your browser.
- Enter the red, green, and blue values in their corresponding fields. Each channel is intended to be an integer from 0 to 255, and the matching HEX code is generated automatically.
- Read the generated HEX value beside the live color preview. The same view also displays the corresponding RGB and HSL values.
- To work backward, paste a six-digit or three-digit HEX value into the HEX field. The converter expands and decodes it into RGB values and adds an HSL readout.
- Click Copy beside the format you need. You can copy the HEX, RGB, or HSL value without retyping it.
The conversion runs locally in JavaScript, so the entered color values are not uploaded to a server. That keeps the workflow suitable for unreleased designs, internal palettes, and other color information that should remain on the device.
Input rules and output normalization
The converter accepts two input paths and gives each one a predictable result. Its normalized output removes ambiguity when the result is copied into a stylesheet or design specification.
| Input or action | Accepted form | Result or behavior |
|---|---|---|
| RGB channel fields | Red, green, and blue values, each intended to be 0–255 | Generates the matching six-digit HEX code |
| HEX field | Six-digit HEX or three-digit shorthand; case-insensitive; leading # optional | Produces normalized lowercase six-digit HEX with RGB and HSL readouts |
| Malformed HEX | Wrong length or illegal characters | Flagged clearly rather than silently producing the wrong color |
| Copy control | HEX, RGB, and HSL values shown near the live preview | Copies the selected format |
RGB channel behavior has one important limit: out-of-range or fractional channel values are clamped into the valid 0–255 integer range instead of producing an error. Enter integer values when you need the exact color specification supplied by a designer or application. By contrast, malformed HEX input is clearly identified, allowing you to correct its length or illegal characters.
The documented inputs are RGB and HEX. HSL is an additional readout that describes the same color with a hue angle from 0 to 360 degrees and saturation and lightness percentages from 0 to 100. Use that readout to understand how a color might be made lighter, darker, or more muted, but enter RGB or HEX values when using the conversion fields.
Verify the color before copying
A mathematically valid conversion is not necessarily the color you intended. Use this checklist after generating or decoding a value:
- Confirm that red, green, and blue appear in the correct order.
- Check that the output contains six hexadecimal digits after the # symbol.
- Look at the live swatch to confirm that the displayed shade matches the expected color.
- When converting from HEX, compare the returned RGB channels with the original specification.
- Review the HSL readout when you need to reason about hue, saturation, or lightness before adjusting the color.
- Copy the required output directly from the converter to avoid introducing a typo.
HEX output is always lowercase, six-digit, and zero-padded. Lowercase is not a different color; it is simply the converter’s normalized presentation. A value of zero appears as 00, not as a bare 0.
For code or design work, make the visual check before leaving the converter. The swatch catches channel-order mistakes and mistaken color choices that a string comparison may not reveal, especially when a nearby shade has the same general hue.
Where the conversion fits
RGB-to-HEX conversion is useful whenever a color is specified numerically in one system and needs to be applied in another. Common tasks include turning a designer’s RGB specification into a CSS-ready value, reading a HEX color from a stylesheet for an image editor, documenting brand colors, or checking the HSL composition of a shade.
- Web development: Translate RGB channel specifications into the #RRGGBB notation used in CSS and HTML.
- Design handoff: Move a color specification from an RGB-based workflow into a HEX value supported by tools such as Figma and Photoshop.
- Image editing: Decode a stylesheet color into red, green, and blue channels for a tool that expects RGB input.
- Palette documentation: Record a consistent HEX value alongside the original RGB specification and the HSL readout.
The practical rule remains simple: convert each channel to two base-16 digits, keep the red-green-blue order, and verify the result visually. The browser converter handles that conversion and the reverse direction while preserving a clear, local workflow.
If you're weighing options, Algorithm to Convert RGB to HSV: Logic and Validation covers this in detail.
If you're weighing options, How Do I Check the Result After CMYK to RGB Conversion? covers this in detail.