RGB to HEX Converter
Convert Red Green Blue values back to a web-safe HEX string.
What does RGB to HEX do?
This converter translates standard Base-10 Red, Green, and Blue (RGB) color values into a concise 6-digit Hexadecimal (HEX) format.
How do you convert RGB to HEX?
Every RGB color channel (Red, Green, and Blue) has a numerical value between 0 and 255. To convert to HEX, you take each channel's number and translate it into Base-16. The resulting three hexadecimal pairs are joined together to form a standard 6-digit web code.
What is the mathematical formula?
If you have the RGB color rgb(255, 87, 51):
- Red (255): 255 ÷ 16 = 15 (F) with a remainder of 15 (F) =
FF - Green (87): 87 ÷ 16 = 5 with a remainder of 7 =
57 - Blue (51): 51 ÷ 16 = 3 with a remainder of 3 =
33
Therefore, rgb(255, 87, 51) equals #FF5733.
What formats are supported?
The input actively validates standard decimal integers from 0 to 255 for the R, G, and B channels. Out-of-bound inputs are blocked to prevent invalid color generation. All processing happens locally on your device.
JavaScript Code Example
To implement this logic locally in your own application, you can map the channels and use the native toString(16) method:
function rgbToHex(r, g, b) {
return '#' + [r, g, b].map(x => {
const hex = Math.max(0, Math.min(255, x)).toString(16);
return hex.length === 1 ? '0' + hex : hex;
}).join('').toUpperCase();
}Related Converters
Need to go the other way? Use our HEX to RGB Converter ⇄.
Frequently Asked Questions
Direct answers and solutions to common questions, technical challenges, and industry standards.
How do you mathematically convert RGB values to HEX manually?expand_more
- The integer quotient (0–15) gives the first hexadecimal character (where 10=A, 11=B, 12=C, 13=D, 14=E, 15=F).
- The remainder (0–15) gives the second hexadecimal character.
rgb(255, 87, 51): - Red 255: 255 ÷ 16 = 15 with remainder 15 →
FF - Green 87: 87 ÷ 16 = 5 with remainder 7 →
57 - Blue 51: 51 ÷ 16 = 3 with remainder 3 →
33
#FF5733.What happens if an RGB value is less than 0 or greater than 255?expand_more
0 (minimum light) and 255 (maximum phosphor saturation). Any value below 0 is clamped to 00, and any value above 255 is clamped to FF. In modern CSS Color Level 4, wide-gamut formats like color(display-p3 r g b) can accept floating-point numbers outside 0–1, but classic hex codes remain bounded to 8 bits per channel.How do I convert RGBA with alpha transparency to an 8-digit HEX string?expand_more
0.0 to 1.0) into hexadecimal, multiply the decimal opacity by 255, round to the nearest whole integer, and convert to base-16: const alphaHex = Math.round(alpha * 255).toString(16).padStart(2, '0').toUpperCase();For example, 50% opacity (0.5) is Math.round(0.5 * 255) = 128 → 80. Appending this to #FF5733 yields the 8-digit hex #FF573380.Is HEX or RGB faster for browser rendering in CSS?expand_more
rgb() expressions into internal 32-bit integer color buffers. HEX has the advantage of smaller transfer size (fewer bytes over the wire), while RGB offers easier dynamic manipulation via CSS variables and modern slash-opacity syntax: rgb(255 87 51 / 50%).