You can use the color pack to colorize the output in the terminal with custom formatters or create custom colors with RGB and ANSI 256 index. All formatters are parsed as ANSI Escape Code.
$ go get github.com/bedirhandogan/color/v2
// Print all text in one color
fmt.Println(color.String("&Red Print this text in color."))
// Print all text in one color, specifying tone
fmt.Println(color.String("&Magenta80 Print this text in magenta 80 shades."))
// Color and print the background of all text by adding bg in front of it
fmt.Println(color.String("&BgCyan Color this text with a cyan background and print it."))
fmt.Println(color.String("&BgYellow50 Color this text with a yellow 50 shades background and print it"))
// Print all text with multiple colors
fmt.Println(color.String("&Blue Print this text with &Green multiple colors."))
// Print all text with multiple color, specifying tone
fmt.Println(color.String("&Blue70 Print this text with &Green20 multiple colors."))
// Print all text with multiple background color
fmt.Println(color.String("&BgYellow20 Print this text with &BgBlue multiple background colors."))
Use SGR Parameters
// Normalize text
fmt.Println(color.String("&Blue Print this text in color with reset. &Reset"))
// Bold text
fmt.Println(color.String("&Bold &Yellow80 Print this text in color with bold."))
fmt.Println(color.String("&Bold Print this text in bold."))
// Italic text
fmt.Println(color.String("&Italic &Cyan50 Print this text in color with italicized."))
fmt.Println(color.String("&Italic Print this text in italicized."))
// You can also format as Blink, Underline, Overline, Invert & Strike.
// This way you can still use native formatters
fmt.Printf(color.String("&Cyan Print this text with %s"), "native formatter")
// Create and use new solid color with ANSI 256 index
color.RegisterAnsi("FaintRed", 124)
fmt.Println(color.String("&FaintRed Print this text faint red color."))
// Create and use new background color with ANSI 256 index
color.RegisterAnsi("BgFaintRed", 124)
fmt.Println(color.String("&BgFaintRed Print this text faint red background color."))
// Create and use new solid color with RGB
color.RegisterRgb("LightRed", 255, 192, 203)
fmt.Println(color.String("&FaintRed Print this text light red color."))
// Create and use new background color with RGB
color.RegisterRgb("BgLightRed", 255, 192, 203)
fmt.Println(color.String("&BgFaintRed Print this text light red background color."))
// Convert RGB to ANSI index
color.RgbToAnsi(50, 0, 50)
// Convert RGB to HEX
color.RgbToHex(237, 98, 23)
// Convert RGB to escape code
color.RgbToEscape(94, 56, 82, true) // \x1b[48;2;<r>;<g>;<b>m - Background
color.RgbToEscape(94, 56, 82, false) // \x1b[38;2;<r>;<g>;<b>m - Foreground
// Convert ANSI index to RGB
color.AnsiToRgb(82)
// Convert ANSI to HEX
color.AnsiToHex(156)
// Convert ANSI Index to escape code
color.AnsiToEscape(95, true) // \x1b[48;5;<i>m - Background
color.AnsiToEscape(95, false) // \x1b[38;5;<i>m - Foreground
// Convert HEX to RGB
color.HexToRgb("#32A852")
// Convert HEX to ANSI
color.HexToAnsi("#32A852")
// Convert SGR parameter to escape code
color.SgrToEscape(3) // \x1b[<p>m
Below are the default color shades you can use:
- Red Shades
- Green Shades
- Yellow Shades
- Cyan Shades
- Blue Shades
- Magenta Shades
- White Shades
- Black Shades
Parameter Name | Escape Code |
---|---|
Reset | 0 |
Bold | 1 |
Italic | 3 |
Underline | 4 |
Blink | 5 |
Overline | 53 |
Invert | 7 |
Strike | 9 |
You scrolled down too much 😂