Colors in RGB format are used in CSS, graphic design, and programming. But sometimes you need to convert them to HEX. We have already written about how to convert HEX to RGB. Now let's look at the conversion in the opposite direction.
RGB Format
The RGB code consists of three numbers ranging from 0 to 255, which correspond to the intensity of red, green, and blue colors. For example, rgb(255, 87, 51).
How to Convert RGB to HEX Manually
-
Break RGB into three components: For example,
rgb(255, 87, 51)→255,87,51. -
Convert each number to hexadecimal:
-
255(red) =FF -
87(green) =57 -
51(blue) =33
-
-
Get the HEX value:
#FF5733.
JavaScript Code Example
function rgbToHex(r, g, b) {
return "#" + [r, g, b].map(x => {
let hex = x.toString(16);
return hex.length === 1 ? "0" + hex : hex;
}).join("");
}
console.log(rgbToHex(255, 87, 51)); // #FF5733
Converting RGB to HEX is not a complicated process. It is enough to convert each component to hexadecimal and combine them into a single string. This is a useful skill for web developers and designers. We have a separate page where you can convert RGB to HEX.