Toolbox-XToolbox-X

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.

playground.ts
Parameters
ParameterTypeDescription
hexHexHEX color string (e.g., #FF0000).
isBgboolean (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.

playground.ts
Parameters
ParameterTypeDescription
rnumberRed component (0-255).
gnumberGreen component (0-255).
bnumberBlue component (0-255).
isBgboolean (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!

playground.ts
Returns
  • 0 | 1 | 2 | 3: Color support level:
    • 0: No color support
    • 1: Basic 16-color support
    • 2: 256-color support
    • 3: Truecolor (24-bit) support

isCSSColor(value)

Check if a string is a valid CSS color name.

playground.ts
Parameters
ParameterTypeDescription
valuestringString 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).

playground.ts
Parameters
ParameterTypeDescription
valuestringString to check.
Returns
  • boolean: Whether the string is a valid background color.

isTextStyle(value)

Check if a string is a valid text style.

playground.ts
Parameters
ParameterTypeDescription
valuestringString 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

playground.ts

See also

Last updated: Sun, Jun 14, 2026 07:15:25PM (UTC)

On this page