Retrieving the RGB values from a chalk object

48 views Asked by At

I'm using the Node.js Chalk module to add some color to a terminal script I'm writing, and I'm currently defining the original colors in Hex value like so:

const chalk = require('chalk');
const t = { 
    // Primary:  blue
    primary: chalk.hex('#2196f3'),
    // Secondary: grey
    secondary: chalk.hex('#888888'),
    // ... More styles
}

This works just fine for when I'm simply outputting those styles to the terminal using something like console.log. But I would like to be able to use the same color values for other components which require the RGB vales to be handed to them. However, I couldn't find any method in the Chalk library that would output the RGB values (or ANSI values).

Is there a way to have chalk output the actual RGB (or ANSI) color values in an array?

I tried looking through the Chalk source code, as well as looking through the properties/methods of the instances.

1

There are 1 answers

0
J H On

It doesn't look like there's a good way to pull the RGB values from the chalk objects, so I instead converted the hex values to the RGB values and stored them as a property of the color config values, and used chalk.rgb(r, g, b) instead of the chalk.hex(hexVal).