javascript rgb to hex

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

function hexToRgb(hex) {
  var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
  if(result){
      var r= parseInt(result[1], 16);
      var g= parseInt(result[2], 16);
      var b= parseInt(result[3], 16);
      return r+","+g+","+b;//return 23,14,45 -> reformat if needed 
  } 
  return null;
}
console.log(rgbToHex(10, 54, 120)); //#0a3678
console.log(hexToRgb("#0a3678"));//"10,54,120"

5
2

                                    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;
}
var hex = &quot;#0a3678&quot;;
console.log(hexToRgb(hex).r+&quot;,&quot;+hexToRgb(hex).g+&quot;,&quot;+hexToRgb(hex).b);//10,54,120

5 (2 Votes)
0
0
0
Paul Wagland 105 points

                                    function hexToRgb(hex) {
  // Expand shorthand form (e.g. &quot;03F&quot;) to full form (e.g. &quot;0033FF&quot;)
  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 ? [
    parseInt(result[1], 16),
    parseInt(result[2], 16),
    parseInt(result[3], 16)
  ] : null;
}

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

0
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
rgb to hex color js rgba to hex in js convert hex to rgb javascript function convert hex string to rgb javascript convert color hex to rgb js conver rgba to hexa by javascript convert hex color to rgb in javascript javascript rgb to hexa color hexadecimal to rgb js hexa to rgb javascript function javascript function to convert rgb to hex from rgb to hex js javascript convert rgb color as hex hex to rgb string in js rgb to hex jaavscript js color rgb to he how to convert hex into rgba in js js convert color hex code to rgb javascript convert rgb into hex how to convert rgba to hex in javascript change hex to rgb javascript convert hex color code to rgb javascript js function that convert rgba into hex calculate rgb from hex javascript from hex to rgb javascript javascript Convert A Hex String To RGB javascript convert rgb string to hex transform rgb to hex js change rgb to hexidecimals js how to convert rgb to hexadecimal in javascrpt rgb to hex and back javascript convert rgba to hex javascript online css rgb to hex js javascript to convery hex to rgba how to convert rgb to hexadecimal in javascript convert rgb to hex code in javascript rgb string to hex in javascript color to rgb js RGB To Hex Conversion codewars javascript how to convert rgb to hex javascript javascript convert hex to rgba how to turn a hex color into rgb values in javascript hex to rgba convert javascript hex to rgb convert javascript from rgb to hex javascript transform hexa to rgba js rgba to hexhex javascript hex string to rgb javascript js hex to rgb array rgb to hex convertor javascript convert rgb to hex color javascript js change from hex to rgb hsl function js hex character to rgb convert hex color to rgb js JS for converting hex to rgb how to convert hex to rgb in javascript convert rgba to hex javascript RGB To Hex Conversion JAVASCRIPT convert rbg a to hex js convert rgb style value to hex value, javascript node js hex to rgb js change hex to rgb js hex to hsl and rgb js convert hex into rgb and hsl PASS RGB TO HEXJS how to change rgb to hex javascript js convert code to hex covnert hex to rbg with javascriptr rgb string to hex converter javascript rgba to hex javacsript transform hex to rgba js convert hex to rgba js how to change a hex code into rgb in javascript rgba to hex js hex to rgb nodejs js style rgb to hex How to Convert RGB Colors to Hexadecimal with Javascript transform hex to rgb js color hex to rgba javascript js rgb from hex convert rgba to hex in js javascript hex to rgba how to convert hex into rgb color in javascript convert pantone to rgb +javascript javascript rpg to hex hax to rgb javascript hex code to rgb hex code to rgb javascript convert rgb(25, 0, 0) string to hex javascript convert rgb string to hex javascript convert rgb hex to rgb javascript rgb to hexjavascript rgb color to hex javascript rgb ot hex js rgb to hexadecimal javascript js hex to rgb color hex to rgba js js convert hex to rgba convert hex color to rgba javascript rgb to hex conversion javascript codewars rgb decimal to hex javascript convert rgba to hex in javascript rgb to hsl javascript change hex to rgb js hex characters to rgb js conver rgb to hex color js convert hex to rgba javascript javascript convert rbg to hex convert rgb to hexadecimal javascript parse hex into color js 3 rgb numbers to hex node js get hex code from rgb jquery js color conversion hex number to rgb js from hex to rgb hexToRgbArray typescript rgb to hex typescript hex to rgb hexcolor to rgbjavascript hexcolor to number javascript how to convert hex to rgb in react convert hex to rgb get rgb from hex javascript 'hexToRgb' is not defined convert rgb to hex hex to rgb react convert color to hex rgbtohex function rbg to hexcodes in js rgb to hex in js converting rgb value to hexcodei n js rgb to color js how to convert hexadecimal color code to rgb in javascript if I set hex color by java it converted to rgb automatically. why ? rgb2hex javascript convert hex to rgb javascrit javascript rgb to string js convert hexcolor to rgb hex color to rgb javascript get rgba from hex javascript javascript rbg to hex javascript hex color to rgb javascript color conversion js get rgb from hex js hex to rgba nodejs hex to rgb js color to rgb hex color to int javascript de hexadecimal a rgb js javascript hex inverter rgb to # javascript variable color rgb to hex javascript convert color to rgb javascript jshex to rgb convert color to rgb js js hex array to rgb rgba to hex javascript javascript rgb to hex code javascript RGB to hec color rgb to hex js javascript convert hexadecimal to rgb javascript rgba to hex js rgba to hex function to convert rgb to hex js typescript convert number to hex color hex to rgba javascript js how to convert rgb to hex how to convert an r, g, and b to a hex code in js get rgb from hex js javascript hex to rb js rgb to 0xrgb convert hex code to rgb javascript color hex to rgb js rgb to hex in javascript java script rgb to hex rgb to string javascript parse brazilian color to hexadecimal javascript work with rgb in javascript mapping from decimal to hex javascript color hex to rgb color converter javascript covert hex to rgb js turn hex to rgb js convert hex color to rgb in js function from hex to rgb react hex to rgb angular function rgb to hexa js convert hex to rgb javascript function to convert hex to rgb rgb to hex color javascript convert rgh to hex js how to convert color to rgb with javascript how to get only the red value in the rgb with javascript rgb to hex converter javascript hex rgb converter javascript one.color rgb to hex js hexToRgb typescript typescript convert color to rgb javascript hextorgb hex to rgb in javascript hex to rgb on javascript js convert hex color to rgb convert hash color to rgb with javascript hexacimal color to rgb function rgb to hex javascript algorithm js add white to hex convert hex to rgb js hex to rgb javascript function hex to rgb and rgb to hex javscript javascript convert hex to rgb convert rgb color object into number js js convert hex string into rgb hex to rgb converter javascript javascript get rgb from hex color rgb to hex &quot;0x&quot; color convert rgb to hex &quot;0x&quot; convert hex color to rgb javascript hexadecimal to rgb js rgb to decimal code javscript javascript convert color from hex to rgb rgb channels to hex js hex to rgb typescript rgb to hex javascript code js hexa to rgb hex to rgb js how to convert hex to rgb javascript javascript convert hex code to html transform rgb to hex javascript RGB To Hex Conversion function in Javascript convert hex to rgb function convert rgb to hex javascript function hex to rgb function converting rgb to hex javascript javascript hrx to rgb js rgb to hex why 16 rgb to hex function javascript convert hex to rgb es6 ger RGB equivalent to string color javsctips convert rgb to hex js hex to srgb js hex to srgb_hex js get decimal color from hex javascript rgb to hexjs from hexadecimal to color js How to combine rgb to hexa java blue to hex js change color of an integer javascript rgbToHex javascript convert r g b to hex javascript hex to rgb jas convert #rrggbb to #rgb js js hex to rgb converter js hexa to rgb converter rgb-hex converter javascript from html rgb-hex converter javascript hexstring to rgb array js javadcript hex to rgb js rgb to hex color javascript convert rgb to hex color js convert rgb to hex js from hex to rgb js rbg to hex convert rgb value to hex javascript hextorgb js hextorgb js not found color input convert to rgb javascript function hex To Rgb stack overflow function hexToRgb stackoverflow function to convert color rgb javascript convert color string to rgb convert rgb color value to number javascript is there a function to convert rgb to hex javascript javascript change rgb to hex rgb to hex how to convert rgb to hex javascript get rgb from hex number get color in hex format js rgb() to hex js color to hex js js hex color to rgb convert from hex to rgb js from rbg(r,g,b) to int js how to convert rgb to hex in javascript rgb to hex conversion javascript js hex to rgb javascript convert number in rgb javascript convert number in color to rgb js return rgb value from range convert rgb to hex in javascript return rgb int from hex rgb to hex js js rgb to hex convert hex to rgb javascript convert color to hex javascript convert regb to hex angular hex to rgb conversion javascript js get convert color to rgb js get color with bit format javascript convert rgb to hex javascript rgb to hex javascript color string to rgb hex to rgb javascript javascript hex to rgb convert rgb to hex javascript js to rb converter rgb to hex javascript
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source