Stylog Utilities
Helper functions for styling console output, including validation, detection, and conversion.
Style Utilities
The style utilities provide helper functions for working with styling, including color conversion, validation, type checking, and color support detection. These utilities are internally used in LogStyler and Stylog but can also be used directly for custom styling needs.
Import
import {
hexToAnsi,
rgbToAnsi,
isCSSColor,
isBGColor,
isTextStyle,
detectColorSupport
} from 'toolbox-x/stylog';API Overview
hexToAnsi(hex, isBg?)
Convert a HEX color to ANSI escape sequences.
Parameters
| Parameter | Type | Description |
|---|---|---|
hex | Hex | HEX color string (e.g., #FF0000). |
isBg | boolean (optional) | Whether to create background color. Defaults to false. |
Returns
[string, string]: Tuple with opening and closing ANSI sequences.
rgbToAnsi(r, g, b, isBg?)
Convert RGB color components to ANSI escape sequences.
Parameters
| Parameter | Type | Description |
|---|---|---|
r | number | Red component (0-255). |
g | number | Green component (0-255). |
b | number | Blue component (0-255). |
isBg | boolean (optional) | Whether to create background color. Defaults to false. |
Returns
[string, string]: Tuple with opening and closing ANSI sequences.
detectColorSupport()
Detects color support level of the current terminal/shell.
Caution
This is a basic level shell color detector. For more advanced cases, use the supports-color package!
Returns
0 | 1 | 2 | 3: Color support level:0: No color support1: Basic 16-color support2: 256-color support3: Truecolor (24-bit) support
isCSSColor(value)
Check if a string is a valid CSS color name.
Parameters
| Parameter | Type | Description |
|---|---|---|
value | string | String to check. |
Returns
boolean: Whether the string is a valid CSS color.
isBGColor(value)
Check if a string is a valid background color (starts with "bg" + valid CSS color).
Parameters
| Parameter | Type | Description |
|---|---|---|
value | string | String to check. |
Returns
boolean: Whether the string is a valid background color.
isTextStyle(value)
Check if a string is a valid text style.
Parameters
| Parameter | Type | Description |
|---|---|---|
value | string | String to check. |
Returns
boolean: Whether the string is a valid text style.
Type Definitions
CSSColor
Represents a valid CSS color name (e.g., 'red', 'blue', 'cornflowerblue').
BGColor
Represents a background color prefixed with bg (e.g., 'bgRed', 'bgBlue').
TextStyle
Represents text effects:
'bold','bolder'- Bold text'dim'- Dimmed text'italic'- Italic text'underline'- Underlined text'strikethrough'- Strikethrough text'inverse'- Inverted colors
Ansi16Color
Represents ANSI 16-color names (e.g., 'red', 'greenBright', 'bgRed').
CSS16Color
Represents CSS16 color names (against Ansi16Color) with css- prefix (e.g., 'css-red', 'css-bgRed').
Ansi16Value
Represents the value of ANSI 16-color codes as number tuples.
AnsiSequence
Represents ANSI escape code sequences as string tuples.
Styles
Union type of all available styles: CSSColor | BGColor | TextStyle
Examples
See also
- LogStyler class - Console styling class
- Stylog - Chainable
LogStylerwrapper - Color Conversion - Additional color utilities
Last updated: Sun, Jun 14, 2026 07:15:25PM (UTC)
