MCPcopy Index your code
hub / github.com/nodejs/node / hexToRgb

Function hexToRgb

lib/util.js:211–225  ·  view source on GitHub ↗

* Parses a hex color string into RGB components. * Supports both 3-digit (#RGB) and 6-digit (#RRGGBB) formats. * @param {string} hex A valid hex color string * @returns {Buffer} The RGB components

(hex)

Source from the content-addressed store, hash-verified

209 * @returns {Buffer} The RGB components
210 */
211function hexToRgb(hex) {
212 // Normalize to 6 digits
213 let hexStr;
214 if (hex.length === 4) {
215 // Expand #RGB to #RRGGBB
216 hexStr = hex[1] + hex[1] + hex[2] + hex[2] + hex[3] + hex[3];
217 } else if (hex.length === 7) {
218 hexStr = StringPrototypeSlice(hex, 1);
219 } else {
220 throw new ERR_OUT_OF_RANGE('hex', '#RGB or #RRGGBB', hex);
221 }
222
223 // TODO(araujogui): use Uint8Array.fromHex
224 return Buffer.from(hexStr, 'hex');
225}
226
227/**
228 * Generates the ANSI TrueColor (24-bit) escape sequence for a foreground color.

Callers 2

getHexStyleFunction · 0.85
styleTextFunction · 0.85

Calls 1

fromMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…