How to convert rgb color in string (E.g. rgb(0, 163, 255) to hex value in Node.js

1 Answer

0 votes
const rgbString = "rgb(0, 163, 255)";

let matchResult = rgbString.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
console.log(matchResult + "\n");

delete(matchResult[0]);
for (let i = 1; i <= 3; i++) {
    matchResult[i] = parseInt(matchResult[i]).toString(16);
    if (matchResult[i].length == 1) {
        matchResult[i] = '0' + matchResult[i];
    }
} 

const hexString ='#' + matchResult.join('').toUpperCase(); 

console.log(hexString);
   
   
   
/*
run:
   
rgb(0, 163, 255),0,163,255

#00A3FF
   
*/

 



answered Jan 27, 2025 by avibootz

Related questions

...