* 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)
| 209 | * @returns {Buffer} The RGB components |
| 210 | */ |
| 211 | function 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. |
no test coverage detected
searching dependent graphs…