Skip to content

Getting Started with Color

Color provides utilities for parsing, formatting, and outputting colors for terminals and web.

What is Color?

The Color class allows you to work with colors in multiple formats: RGB, hex, and ANSI escape sequences.

ts
import { Color } from "@briklab/lib/color";

const color = new Color({ r: 255, g: 100, b: 50 }, "boundary");
console.log(color.hex());              // "#ff6432"
console.log(color.rgb());              // "rgb(255, 100, 50)"
console.log(color.ansiTruecolor());    // ANSI escape for the color

Basic Usage

Creating Colors

ts
import { Color } from "@briklab/lib/color";

// From RGB
const rgb = new Color({ r: 255, g: 0, b: 0 }, "boundary");

// From hex (via parse - check Color class for full input types)
const fromInput = new Color({ r: 255, g: 0, b: 0 }, "boundary");

console.log(rgb.hex());    // "#ff0000"

Color Conversions

ts
const color = new Color({ r: 100, g: 150, b: 200 }, "boundary");

// Convert to different formats
console.log(color.hex());              // "#6496c8"
console.log(color.rgb());              // "rgb(100, 150, 200)"
console.log(color.ansiTruecolor());    // ANSI sequence
console.log(color.ansi256());          // 256-color ANSI

Styling Text

ts
const color = new Color({ r: 255, g: 0, b: 0 }, "boundary");

// Wrap text with color and styles
const styledText = color.wrapAnsi("Error!", {
  bold: true,
  underline: false
});

console.log(styledText);  // Red, bold "Error!"

Live Demo

Console
No logs yet.

Format Support

  • Hex: #RRGGBB format
  • RGB: rgb(red, green, blue) format
  • ANSI: Terminal color codes (256-color and true color)
  • CSS: Standard CSS color values

Features

  • Multiple Input Types: Accept colors in various formats
  • Multiple Output Formats: Convert to hex, RGB, or ANSI
  • Terminal Styling: Add bold, underline, and other effects
  • Color Space Support: Work in different color spaces

Next Steps

Learn more about Color: