“javascript rgb à hex” Réponses codées

javascript rgb à 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"
Friendly Hawk

JavaScript convertit la couleur hex

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

function hexToRgb(hex, result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{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]
Adorable Armadillo

Convertir le code hexagonal en JavaScript RVB

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

hextring à rgb array js

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 ? [
    parseInt(result[1], 16),
    parseInt(result[2], 16),
    parseInt(result[3], 16)
  ] : null;
}

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

Réponses similaires à “javascript rgb à hex”

Questions similaires à “javascript rgb à hex”

Plus de réponses similaires à “javascript rgb à hex” dans JavaScript

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code