Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

rgb to hex javascript

// Convert RGB to Hex
function getColorFun(r, g, b) {
   return '#' + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
}

getColorFun(178, 232, 55);
// The output here is #b2e837
Comment

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

rgb to hex js

const rgb2hex = (rgb) => `#${rgb.match(/^rgb((d+),s*(d+),s*(d+))$/).slice(1).map(n => parseInt(n, 10).toString(16).padStart(2, '0')).join('')}`

// Use as you wish...
console.log(rgb2hex('rgb(0,0,0)'))
console.log(rgb2hex('rgb(255, 255, 255)'))
console.log(rgb2hex('rgb(255,0,0)'))
console.log(rgb2hex('rgb(38, 170, 90)'))
 Run code snippet
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

rgb to hex js

function rgb(r, g, b){
  // complete this function 
  return hexC(r)+hexC(g)+hexC(b)
}
function hexC(v){
  if(v>255){
    v=255
  }else if(v<0){
    v=0
  }
  let a=v%16;
  let b=Math.floor(v/16);
  return hmaps[b]+hmaps[a]
  }
let hmaps={
  0:'0',1:'1',2:'2',3:'3',4:'4',5:'5',6:'6',7:'7',8:'8',9:'9',10:'A',11:'B',12:'C',13:'D',14:'E',15:'F'
  }
Comment

PREVIOUS NEXT
Code Example
Javascript :: only letters and spaces regex 
Javascript :: regex date validation mm/dd/yyyy 
Javascript :: javascript measure time function 
Javascript :: java scripyt code to edit webapge 
Javascript :: three js cdn 
Javascript :: jquery check how many child elements 
Javascript :: dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.66.dylib Referenced from: /usr/local/bin/node Reason: image not found 
Javascript :: check ip json 
Javascript :: js loop array backward 
Javascript :: body-parser deprecated undefined extended provide extended option 
Javascript :: js get element window offset top 
Javascript :: javascript hide element by class 
Javascript :: javascript get element by class name 
Javascript :: pick random string from array javascript 
Javascript :: Error: Requires Babel "^7.0.0-0", but was loaded with "6.26.3". If 
Javascript :: bootstrap modal title center 
Javascript :: remove special characters from string javascript 
Javascript :: onclick for dynamically created element jquery 
Javascript :: nodejs atob 
Javascript :: how to get the height of window in javascript 
Javascript :: javascript run document ready 
Javascript :: jquery remove string from string 
Javascript :: how to know connected internet in js 
Javascript :: react simbu 
Javascript :: modal.show jquery 
Javascript :: check if date time string is invalid date js 
Javascript :: js match alphabet 
Javascript :: sort array by date moment 
Javascript :: how to remove modal-backdrop fade in jquery 
Javascript :: react native flatlist margin bottom 
ADD CONTENT
Topic
Content
Source link
Name
8+5 =