Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript convert hex color to rgb

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

function hexToRgb(hex, result = /^#?([a-fd]{2})([a-fd]{2})([a-fd]{2})$/i.exec(hex)) {
  return result ? result.map(i => parseInt(i, 16)).slice(1) : null
  //returns [23, 14, 45] -> reformat if needed
}

console.log(rgbToHex(10, 54, 120)); // #0a3678
console.log(hexToRgb("#0a3678")); // [10, 54, 120]
Comment

convert hex code to rgb javascript

function hexToRgb(hex){
	var result = /^#?([a-fd]{2}])([a-fd]{2})([a-fd]{2})$/i.exec(hex);
  	
 	return result ? {
    	r: parseInt(result[1],  16);
      	g: parseInt(result[2],  16);
  		b: parseInt(result[3],  16);
    } : null;
}
var hex = "#0a3678";
console.log(hexToRgb(hex).r+","+hexToRgb(hex).g+","+hexToRgb(hex).b);//10,54,120
Comment

PREVIOUS NEXT
Code Example
Javascript :: angular JavaScript heap out of memory 
Javascript :: react native debug apk 
Javascript :: js remove last character from string 
Javascript :: how to edit a web page 
Javascript :: remove spaces in a string js 
Javascript :: how to get clicked radio button value in jquery 
Javascript :: electron hide menu bar 
Javascript :: scroll to element jquery 
Javascript :: javascript loop through array backwards 
Javascript :: Source file requires different compiler version (current compiler is 0.8.4+commit.c7e474f2.Emscripten.clang) - note that nightly builds are considered to be strictly less than the released version 
Javascript :: remove punctuation marks from string js 
Javascript :: Uncaught TypeError: firebase_app__WEBPACK_IMPORTED_MODULE_4__.default.storage is not a function 
Javascript :: event exit fullscreen 
Javascript :: regex pattern for positive numbers only 
Javascript :: create random 4 digit number js 
Javascript :: javascript check if function exists 
Javascript :: testng before class vs before test 
Javascript :: scroll jquery to anchor 
Javascript :: run a function after delay javascript 
Javascript :: radio button onchange jquery 
Javascript :: javascript wait for document load 
Javascript :: js vanilla dom ready 
Javascript :: This error occurred during the build time and cannot be dismissed. 
Javascript :: react create app 
Javascript :: Appium find Android element by Id using Javascript 
Javascript :: responsive slick slider 
Javascript :: acces store from vue console javascript 
Javascript :: sort object alphabetically javascript 
Javascript :: javascript repeat each second 
Javascript :: regular expression for links 
ADD CONTENT
Topic
Content
Source link
Name
7+2 =