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 :: recursion javascript 
Javascript :: javascript toisostring without milliseconds 
Javascript :: jqery get text 
Javascript :: javascript initialize array 
Javascript :: javascript get array object by id 
Javascript :: ajax open new tab with post 
Javascript :: maths 
Javascript :: email regex pattern input css 
Javascript :: force rerender react 
Javascript :: forming an object with reduce 
Javascript :: quotation marks javascript 
Javascript :: react native header 
Javascript :: jquery element width 
Javascript :: formik and yup reactjs 
Javascript :: stop keyframe animation javascript 
Javascript :: angular library run tests 
Javascript :: how would you check if a number is an integer in javascript 
Javascript :: concat js mdn 
Javascript :: react current path 
Javascript :: get the last day of the month in js 
Javascript :: javascript splice without changing array 
Javascript :: print all days names of a month 
Javascript :: click unbind 
Javascript :: jquery form submit ajax 
Javascript :: avascript regex email 
Javascript :: nepali date picker 
Javascript :: regex expression to match domain name 
Javascript :: add on click to div using jquery 
Javascript :: require a json as a string 
Javascript :: jquery change button click function 
ADD CONTENT
Topic
Content
Source link
Name
2+4 =