Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

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

hex to rgba in js

hexToRGB(hex: string, alpha: string) {

  const r = parseInt(hex.slice(1, 3), 16);
  const g = parseInt(hex.slice(3, 5), 16);
  const b = parseInt(hex.slice(5, 7), 16);

  if (alpha) {
    return `rgba(${r}, ${g}, ${b}, ${alpha})`;
  } else {
    return `rgb(${r}, ${g}, ${b})`;
  }
}
Comment

javascript hex color to rgba

//If you write your own code, remember hex color shortcuts (eg., #fff, #000)

function hexToRgbA(hex){
    var c;
    if(/^#([A-Fa-f0-9]{3}){1,2}$/.test(hex)){
        c= hex.substring(1).split('');
        if(c.length== 3){
            c= [c[0], c[0], c[1], c[1], c[2], c[2]];
        }
        c= '0x'+c.join('');
        return 'rgba('+[(c>>16)&255, (c>>8)&255, c&255].join(',')+',1)';
    }
    throw new Error('Bad Hex');
}

hexToRgbA('#fbafff')

/*  returned value: (String)
rgba(251,175,255,1)
*/
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 :: enquirer confirm 
Javascript :: flash message in hbs 
Javascript :: how to shorten billion in javascript 
Javascript :: datatables width issue for less number of columns 
Javascript :: corousal in react 
Javascript :: callbacks 
Javascript :: 9.4.1.3. Update Expression¶ // Loops 
Javascript :: isempty is not a function javascript 
Javascript :: grommetjs remove green over buttons 
Javascript :: 10.4.2. Functions // Default Value 
Javascript :: && in react jsx 
Javascript :: scroll down react js typescript 
Javascript :: check if anagram 
Javascript :: sort 
Javascript :: underscore javascript 
Javascript :: Multiple destinations with gulp js 
Javascript :: delete all properties from an javascript object second solution 
Javascript :: make express app object accessible from all project modules 
Javascript :: angular copy folder to dist 
Javascript :: javascript leetcode solutions 
Javascript :: typeerror: chogigabsi eobnun bin beyole dehan recudenun error. 
Javascript :: if element touches another element on scroll 
Javascript :: ip address pattern regex javascript 
Javascript :: Ajax Mixed content blocked 
Javascript :: react currency format 
Javascript :: jquery override page title 
Javascript :: convert componentDidUpdate into useEffect 
Javascript :: Node_connect 
Javascript :: JavaScript startsWith() example with Position parameter 
Javascript :: jquery click ony works once on dropdown 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =