Is there a way to calculate HEX colors in CSS?

480 views Asked by At

On userstyles.org you can create color settings to let the user define it's own color that applies in style.

In my style I'm using main color that the user can define and some additional colors that derives from main color. But userstyles put color in HEX(

So is there any way to calculate HEX colors or convert HEX to RGB or HSL?

1

There are 1 answers

0
Sandeep On
 function convertHexToRGBA (hex, alpha) {
    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 + ")";
    }
  }