Generating colorful text in Deno
Purpose
It’s often required to move away from the standard black and white text, to generate colorful text with or without a colorful background. The colorful text can be used anywhere like on the console, or inside an HTML page.
Deno’s standard library comes with a useful module and functions to convert any text into colorful text. Instead of logging text on the console, these functions return a color-coded text that can be logged anywhere.
Functions
To use the coloring functions, the following module needs to be imported:
import * as color from "https://deno.land/std/fmt/colors.ts"
There are four types of functions that are provided by the colors module:
- Property functions: These functions are part of the colors module, but they set the property like bold, italic, etc.
- Direct text color functions: These are the set of functions that are named after a color and can be used directly to color-code a text. For example — black, red, green, etc. These functions are useful for the commonly used colors.
- Direct background color functions: These are the same as direct text color functions, but they set the color-coded background instead of text.
- RGB functions: There are four such functions that can color-code text or background in the given RGB color combination. These functions are flexible as the color can be controlled through the RGB input.
Here is the list of property functions provided by the colors module:
- bold
- dim
- italic
- underline
- inverse
- hidden
- strikethrough
Here is the list of the direct text color functions:
- black
- red
- green
- yellow
- blue
- magenta
- cyan
- white
- gray
- brightBlack
- brightRed
- brightGreen
- brightYellow
- brightBlue
- brightMagenta
- brightCyan
- brightWhite
Here is the list of direct background color functions:
- bgBlack
- bgRed
- bgGreen
- bgYellow
- bgBlue
- bgMagenta
- bgCyan
- bgWhite
- bgBrightBlack
- bgBrightRed
- bgBrightGreen
- bgBrightYellow
- bgBrightBlue
- bgBrightMagenta
- bgBrightCyan
- bgBrightWhite
Here is the list of RGB functions for text and background coloring:
- rgb8
- bgRgb8
- rgb24
- bgRgb24
Note that all the functions can be chained, for example — bold(red(text)).
The last function is used to remove coloring information from a given text. This function is useful if the data needs to be processed without color codes.
- stripColor
Inputs
All the functions, except the RGB ones, takes a string as the input. This string would be color-coded.
black(str: string)
bgBrightWhite(str: string)
The two RGB8 functions take a string and a color number as input. The other two RGB24 functions take a string and either a color number or an rgb object.
rgb8(str: string, color: number)
rgb24(str: string, color: number | Rgb)
Outputs
The output of all the coloring functions is a color-coded string.
bold(str: string): string
brightCyan(str: string): string
rgb24(str: string, color: number | Rgb): string
Examples
There are a lot of functions in the colors module. It is neither useful nor practical to demonstrate all of them. We’ll go over a limited set.
First, let’s see some of the basic text coloring functions:
import { red, green, gray } from "https://deno.land/std/fmt/colors.ts"red('Hello Deno!');
green('Hello Deno!');
gray('Hello Deno!');
Next, let’s see chaining of property functions with basic text coloring functions:
import { red, green, gray, blue, bold, dim } from "https://deno.land/std/fmt/colors.ts"bold(red('Hello Deno!'));
bold(green('Hello Deno!'));
dim(gray('Hello Deno!'));
dim(bold(blue('Hello Deno!')));
Next, let’s see a combination of property, text, and background coloring functions:
import { red, green, bgYellow, bgBlack, bgWhite, bold } from "https://deno.land/std/fmt/colors.ts"bgYellow(bold(red('Hello Deno!')));
bgWhite(bold(green('Hello Deno!')));
Finally, let’s see examples of using RGB colors in both text and background color.
import { rgb8, bgRgb8 } from "https://deno.land/std/fmt/colors.ts"bgRgb8(rgb8('Hello Deno!', 20), 80);
bgRgb8(rgb8('Hello Deno!', 80), 20);