Answers for "hex to rgb typescript"

11

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"
Posted by: Guest on August-02-2019
0

hexstring to 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";
Posted by: Guest on June-21-2020
0

hex to rgb typescript

export const hexToRgb = (h: string, isPct: boolean) => {
    let r: string = '0', g: string = '0', b: string = '0';
    isPct = isPct === true;

    if (h.length == 4) {
        r = "0x" + h[1] + h[1];
        g = "0x" + h[2] + h[2];
        b = "0x" + h[3] + h[3];

    } else if (h.length == 7) {
        r = "0x" + h[1] + h[2];
        g = "0x" + h[3] + h[4];
        b = "0x" + h[5] + h[6];
    }

    if (isPct) {
        r = (parseInt(r) / 255 * 100).toFixed(1).toString();
        g = (parseInt(g) / 255 * 100).toFixed(1).toString();
        b = (parseInt(b) / 255 * 100).toFixed(1).toString();
    }

    return "rgb(" + (isPct ? r + "%," + g + "%," + b + "%" : +r + "," + +g + "," + +b) + ")";
}
Posted by: Guest on April-24-2021

Code answers related to "TypeScript"

Browse Popular Code Answers by Language