JSFiddle - React, Tailwind, and code Playground

by Erick Petrucelli

JavaScript

const rgb2hex = (rgb) => `#${rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/).slice(1).map(n => parseInt(n, 10).toString(16).padStart(2, '0')).join('')}`

console.log(rgb2hex('rgb(080, 0, 0)'))
console.log(rgb2hex('rgb(255, 255, 255)'))
console.log(rgb2hex('rgb(255, 0, 0)'))
console.log(rgb2hex('rgb(38, 170, 90)'))