How to convert hex to RGB in TypeScript

2 Answers

0 votes
function hexToRgb(hex: string) {
    const rgb = parseInt(hex, 16);
     
    const r = (rgb >> 16) & 255;
    const g = (rgb >> 8) & 255;
    const b = rgb & 255;
 
    return r + " " + g + " " + b;
}
 
 
console.log(hexToRgb('fa9805'));
 
 
 
 
/*
run:
 
"250 152 5"
 
*/

 



answered Dec 30, 2021 by avibootz
0 votes
function hexToRgb(hex: string) {
    const rgb = parseInt(hex, 16);
    
    const r = (rgb >> 16) & 255;
    const g = (rgb >> 8) & 255;
    const b = rgb & 255;

    return [r, g, b];
}

 
const arr = hexToRgb('fa9805')
 
const r = arr[0];
const g = arr[1];
const b = arr[2];
 
console.log(r, g, b);




/*
run:

250,  152,  5 

*/

 



answered Dec 30, 2021 by avibootz

Related questions

4 answers 583 views
1 answer 203 views
1 answer 185 views
1 answer 172 views
...