Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

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

PREVIOUS NEXT
Code Example
Javascript :: react native scrollable 
Javascript :: input to state 
Javascript :: tabuada js 
Javascript :: html iframe and JS contentwindow 
Javascript :: Binary Agents 
Javascript :: javascript truncate array 
Javascript :: javascript click events 
Javascript :: copy to clipboard js 
Javascript :: Navbar Componet Nextjs 
Javascript :: javascript get all classes 
Javascript :: get month in two digit in javascript date 
Javascript :: remove matching element from two array javascript 
Javascript :: javascript append element to array 
Javascript :: hello word in js 
Javascript :: settext javascript 
Javascript :: how to get datetime javascript now 
Javascript :: month list javascript 
Javascript :: how to stop react app in terminal 
Javascript :: js display 2d array 
Javascript :: kamus bahasa inggris 
Javascript :: loop through files in directory javascript 
Javascript :: axios timeout post example 
Javascript :: javascript detect when number of elements change 
Javascript :: kb to mb js 
Javascript :: opening a link in another tab in react 
Javascript :: js add more text to element 
Javascript :: Regular expression: Match everything after a particular word 
Javascript :: javascript redirect to home page 
Javascript :: chrome input disable autofill 
Javascript :: sequelize migration add column foreign key 
ADD CONTENT
Topic
Content
Source link
Name
6+8 =