Note: both versions of rgbToHex expect integer values for r, g and b, so you'll need to do your own rounding if you have non-integer values.

The following will do to the RGB to hex conversion and add any required zero padding:

function componentToHex(c) {
  var hex = c.toString(16);
  return hex.length == 1 ? "0" + hex : hex;
}

function rgbToHex(r, g, b) {
  return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b);
}

alert(rgbToHex(0, 51, 255)); // #0033ff

Converting the other way:

function hexToRgb(hex) {
  var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
  return result ? {
    r: parseInt(result[1], 16),
    g: parseInt(result[2], 16),
    b: parseInt(result[3], 16)
  } : null;
}

alert(hexToRgb("#0033ff").g); // "51";

Finally, an alternative version of rgbToHex(), as discussed in @casablanca's answer and suggested in the comments by @cwolves:

function rgbToHex(r, g, b) {
  return "#" + (1 << 24 | r << 16 | g << 8 | b).toString(16).slice(1);
}

alert(rgbToHex(0, 51, 255)); // #0033ff

Update 3 December 2012

Here's a version of hexToRgb() that also parses a shorthand hex triplet such as "#03F":

function hexToRgb(hex) {
  // Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF")
  var shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
  hex = hex.replace(shorthandRegex, function(m, r, g, b) {
    return r + r + g + g + b + b;
  });

  var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
  return result ? {
    r: parseInt(result[1], 16),
    g: parseInt(result[2], 16),
    b: parseInt(result[3], 16)
  } : null;
}

alert(hexToRgb("#0033ff").g); // "51";
alert(hexToRgb("#03f").g); // "51";

Answer from Tim Down on Stack Overflow
Top answer
1 of 16
1717

Note: both versions of rgbToHex expect integer values for r, g and b, so you'll need to do your own rounding if you have non-integer values.

The following will do to the RGB to hex conversion and add any required zero padding:

function componentToHex(c) {
  var hex = c.toString(16);
  return hex.length == 1 ? "0" + hex : hex;
}

function rgbToHex(r, g, b) {
  return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b);
}

alert(rgbToHex(0, 51, 255)); // #0033ff

Converting the other way:

function hexToRgb(hex) {
  var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
  return result ? {
    r: parseInt(result[1], 16),
    g: parseInt(result[2], 16),
    b: parseInt(result[3], 16)
  } : null;
}

alert(hexToRgb("#0033ff").g); // "51";

Finally, an alternative version of rgbToHex(), as discussed in @casablanca's answer and suggested in the comments by @cwolves:

function rgbToHex(r, g, b) {
  return "#" + (1 << 24 | r << 16 | g << 8 | b).toString(16).slice(1);
}

alert(rgbToHex(0, 51, 255)); // #0033ff

Update 3 December 2012

Here's a version of hexToRgb() that also parses a shorthand hex triplet such as "#03F":

function hexToRgb(hex) {
  // Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF")
  var shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
  hex = hex.replace(shorthandRegex, function(m, r, g, b) {
    return r + r + g + g + b + b;
  });

  var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
  return result ? {
    r: parseInt(result[1], 16),
    g: parseInt(result[2], 16),
    b: parseInt(result[3], 16)
  } : null;
}

alert(hexToRgb("#0033ff").g); // "51";
alert(hexToRgb("#03f").g); // "51";

2 of 16
202

An alternative version of hexToRgb:

function hexToRgb(hex) {
    var bigint = parseInt(hex, 16);
    var r = (bigint >> 16) & 255;
    var g = (bigint >> 8) & 255;
    var b = bigint & 255;

    return r + "," + g + "," + b;
}

Edit: 3/28/2017 Here is another approach that seems to be even faster

function hexToRgbNew(hex) {
  var arrBuff = new ArrayBuffer(4);
  var vw = new DataView(arrBuff);
  vw.setUint32(0,parseInt(hex, 16),false);
  var arrByte = new Uint8Array(arrBuff);

  return arrByte[1] + "," + arrByte[2] + "," + arrByte[3];
}

Edit: 8/11/2017 The new approach above after more testing is not faster :(. Though it is a fun alternate way.

๐ŸŒ
RGB to HEX
rgbtohex.net
RGB to HEX Color Converter
It takes input in the form of values for Red, Green and Blue ranging from 0 to 255 and then converts those values to a hexadecimal string that can be used to specify color in html/css code. Photo editing software usually represents color in RGB and therefore if you would like to use the colors you use in your photo editing software as the background of your html element then you will have to get the hexadecimal representation of the RGB values.
Discussions

Quickly converting Hex to RGB and vice-versa, without using a lousy converter or huge table
wait what, people who do CSS haven't realised this? More on reddit.com
๐ŸŒ r/reddithax
10
12
October 8, 2017
TIL: hex values are faster than color names and rgb
Next most up-voted comment on that thread: You will be unable to measure any difference in the three options on any non-trivial web page. This is the truth. Once a web browser engine has parsed the code, the in-memory representation of the CSS/HTML/JavaScript color will be an ordinary 32-bit integer, so the only time the difference between the named symbol versus the hexadecimal representation will matter is during parsing. And this difference is so tiny compared to the amount of time spent optimizing the rest of the code and data structures in the web page, the efficiency difference is trivial, it is literally not even worth thinking about. Switching to hexadecimal over color names would be like trying to reduce the weight of your luggage by ripping stickers off of your pack. More on reddit.com
๐ŸŒ r/web_design
79
577
May 17, 2017
HEX, RGB or HSL?
I use hex, unless I also use an alpha channel, then I use rgba(). Why? I've used it for years, before rgb() was available. More on reddit.com
๐ŸŒ r/webdev
8
2
May 26, 2014
Which do you use more, HEX or RGB
RGB only when I need opacity. Hex is easier to copy-paste from color pallettes. More on reddit.com
๐ŸŒ r/webdev
40
10
April 21, 2020
๐ŸŒ
Tripod
gristle.tripod.com โ€บ hexconv.html
RGB - Hexadecimal Color Conversion
B60023, multiply B, or 11, by 16 to get 176, then add 6 to give you 182. See? It works. ... This will effecively give you &#147millions&#148; of hexadecimal colors.
๐ŸŒ
RapidTables
rapidtables.com โ€บ convert โ€บ color โ€บ rgb-to-hex.html
RGB to Hex Color Converter
Concatenate the 3 hex values of the red, green and blue togather: RRGGBB.
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ rgb-to-hex
RGB to Hex
The RGB color code for crimson is rgb(220, 20, 60). Take the first number, 220, and divide by 16. ... This means that the first digit of the 6-digit hex color code is 13 or D. Take the remainder of the first digit, 0.75, and multiply by 16.
๐ŸŒ
Reddit
reddit.com โ€บ r/reddithax โ€บ quickly converting hex to rgb and vice-versa, without using a lousy converter or huge table
r/reddithax on Reddit: Quickly converting Hex to RGB and vice-versa, without using a lousy converter or huge table
October 8, 2017 -

A lot of people, apparently, think that a rgb value is more precise than a hex value. In reality, a hex number can directly translate to a rgb value.

To start with, you'll need to divide the number into three sections. Each of these sections will correspond to a red, green and blue value.

#FFFFFFrgb(255, 255, 255)
FF (First two)255
FF (Middle two)255
FF (Last two)255

To convert a hex pair to a number, take your first value and multiply it by 16. Then add it to your second value.

#FFFFFFCharacter OneCharacter TwoTotal
FF(F) 15 * 16 = 240(F) 15240+15=255
FF(F) 15 * 16 = 240(F) 15240+15=255
FF(F) 15 * 16 = 240(F) 15240+15=255
#BADCFECharacter OneCharacter TwoTotal
BA(B) 11 * 16 = 176(A) 10176+10 = 186
DC(D) 13 * 15 = 208(C) 12208+12 = 220
FE(F) 15 * 16 = 240(E) 14240 + 14 = 254

Note that a = 10, b=11, c=12, d=13, e=14, and f=15

Once you do this enough, you should get the hang of it, and no longer require looking at this post.

๐ŸŒ
RapidTables
rapidtables.com โ€บ convert โ€บ color โ€บ how-rgb-to-hex.html
How to convert RGB to hex color code
The RGB color is a combination of Red, Green and Blue colors: ... The red, green and blue use 8 bits each, which have integer values from 0 to 255. ... The 2 left digits represent the red color. The 2 middle digits represent the green color. The 2 right digits represent the blue color. Convert the red, green and blue color values from decimal to hex.
Find elsewhere
๐ŸŒ
DEV Community
dev.to โ€บ hichem-mg โ€บ comprehensive-guide-converting-rgb-color-to-hex-code-2fcb
Comprehensive Guide: Converting RGB Colors to Hex Codes - DEV Community
July 17, 2024 - Hex codes consist of a pound sign (#) followed by six alphanumeric characters. ... Obtain the RGB values for the desired color. Each RGB value ranges from 0 to 255, representing the intensity of the respective color channel.
๐ŸŒ
HTML Color Codes
htmlcolorcodes.com โ€บ rgb-to-hex
RGB to Hex โ€“ HTML Color Codes
Convert any RGB value to its Hex color code, along with corresponding HSL, HSV and CMYK values (including HTML/CSS values).
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ how-to-convert-hex-to-rgb-and-rgb-to-hex-in-python
How to convert hex to RGB and RGB to hex in Python
The format method is used to construct the string with :02x specifying that the values should be formatted as two-digit hexadecimal numbers with leading zeros added if necessary. Line 4: The line print(rgb_to_hex(255, 165, 1)) calls the rgb_to_hex ...
๐ŸŒ
Testmu
testmu.ai โ€บ home โ€บ free tools โ€บ rgb to hex online converter
Convert RGB color to HEX color values | Free online tool to RGB to HEX.
The RGB-to-hexadecimal converter algorithm is simple: Convert the red, green, and blue (R, G, B) values to hex strings by ensuring that they are in the range 0...255, concatenate the three hex strings together, and then convert the result to a six-digit hexadecimal number...
๐ŸŒ
RapidTables
rapidtables.com โ€บ convert โ€บ color โ€บ hex-to-rgb.html
Hex to RGB Color Converter
Get the 2 middle digits of the hex color code and convert to decimal value to get the green color level.
๐ŸŒ
Appkong
appkong.com โ€บ tools โ€บ hex to rgb
HEX to RGB Color Converter [AppKong]
December 10, 2021 - Repeat the same division and multiplication for the third value (60). In this example, 60/16 = 3 + 0.75, while the remainder multiplied by 16 gives 12. Thus, the full code is #C9143C. Fortunately, you do not have to perform these calculations every time. Just use our instant RGB to hex converter!
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ utilities โ€บ rgb-to-hex-converter
Online RGB to HEX Color Converter (Free & Easy to Use) - GeeksforGeeks
April 23, 2024 - Precision and Consistency: Ensures precise and consistent color representation by providing accurate HEX codes for RGB values, supporting design precision. The RGB to HEX Converter works by allowing users to input RGB values through sliders or input fields. The JavaScript code then dynamically updates the color output and calculates the corresponding HEX code.
๐ŸŒ
RGB to HEX
rgbtohex.net โ€บ rgb
RGB to HEX Color
Convert RGB color codes to HEX HTML format for use in web design and CSS. Also converts RGBA to HEX.
๐ŸŒ
W3Schools
w3schools.com โ€บ colors โ€บ colors_converter.asp
Color Converter
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST ยท Colors HOME Color Names Color Values Color Groups Color Shades Color Picker Color Mixer Color Converter Color RGB Color HEX Color HSL Color HWB Color CMYK Color NCol Color Gradient Color Theory Color Wheels Color currentcolor Color Hues Color Schemes Color Palettes Color Brands Color W3.CSS Color Metro UI Color Win8 Color Flat UI Color Psychology
๐ŸŒ
RGB Color Code
rgbcolorcode.com โ€บ color โ€บ converter
HEX - RGB Color Converter
This interactive online color conversion tool allows you to calculate the transition between RGB and HEX values.
๐ŸŒ
HTML Color Codes
htmlcolorcodes.com โ€บ hex-to-rgb
Hex to RGB โ€“ HTML Color Codes
Convert any Hex color code to its RGB value, along with corresponding HSL, HSV and CMYK values (including HTML/CSS values).
๐ŸŒ
OpenReplay
openreplay.com โ€บ tools โ€บ rgb-to-hex
RGB to HEX Converter
This tool seamlessly translates color representations between two standard color formats: RGB (Red, Green, Blue) and HEX (Hexadecimal) notation.
๐ŸŒ
RGB to HEX
rgbtohex.net โ€บ hex-to-rgb
HEX to RGB Color Converter
Photo editing software usually represents color in RGB and therefore if you would like to use the same colors that you use in your html pages in your photo editing software you will need the RGB values for the hex code.