rainbow (rb)

The rainbow function (also aliased as rb) colours each character in a string with a different hue. The standard mode cycles through six 4-bit colors; the smooth mode uses a 26-stop 8-bit extended palette for a much richer gradient. Great for eye-catching banners and headings.

Example of ts-better-console rainbow function in action

Signature

function rainbow(str: string, smooth?: boolean): string

Parameters

  • str: The string to colorise.
  • smooth (default false): When false, each character cycles through the six rainbowColors red, yellow, green, cyan, blue, magenta. When true, uses the 26-stop rainbowColorsExtended 8-bit palette, distributing colors proportionally across the string for a smooth full-spectrum gradient.

Return Value

A string with individual ANSI colour escapes wrapping each character. In standard mode each character is reset with DEFAULT_COLOR (\x1b[39m), preserving any background or intensity attributes applied outside the rainbow string. In smooth mode the 8-bit SGR codes are used directly without a per-character reset.

Aliases

Exported as both rainbow and rb — use whichever is cleaner in context.

Usage

import { rainbow } from "ts-better-console";

console.log(rainbow("Hello, World!"));

Want to support this project?

If you find ts-better-console useful and want to support its development, consider starring the GitHub repository or buying me a coffee! Your support helps me dedicate more time to improving the library and adding new features.

Want to contribute to this project?

Contributions are welcome! If you're interested in improving the library, fixing bugs, or adding new features, feel free to check out the GitHub repository and submit a pull request. Whether you're a seasoned developer or new to open source, your contributions can make a difference!