Skip to content

bedirhandogan/color

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

74 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Color Go Reference Static Badge Static Badge

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.

code sample

Install

$ go get github.com/bedirhandogan/color/v2

Uses

Single Colorize

// 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"))

Multiple Colorize

// 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.

Native Formatters

// This way you can still use native formatters
fmt.Printf(color.String("&Cyan Print this text with %s"), "native formatter")

Create New Color

// 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."))

Converters

RGB

// 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

ANSI

// 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

HEX

// Convert HEX to RGB
color.HexToRgb("#32A852")

// Convert HEX to ANSI
color.HexToAnsi("#32A852")

SGR

// Convert SGR parameter to escape code
color.SgrToEscape(3) // \x1b[<p>m

Below are the default color shades you can use:

Color Name RGB Hex Preview
Red (255, 0, 0) #FF0000 Loading...
Red10 (240, 0, 0) #F00000 Loading...
Red20 (224, 0, 0) #E00000 Loading...
Red30 (208, 0, 0) #D00000 Loading...
Red40 (192, 0, 0) #C00000 Loading...
Red50 (176, 0, 0) #B00000 Loading...
Red60 (160, 0, 0) #A00000 Loading...
Red70 (144, 0, 0) #900000 Loading...
Red80 (128, 0, 0) #800000 Loading...
Red90 (112, 0, 0) #700000 Loading...
Red100 (96, 0, 0) #600000 Loading...
Color Name RGB Hex Preview
Green (0, 255, 0) #00FF00 Loading...
Green10 (0, 240, 0) #00F000 Loading...
Green20 (0, 224, 0) #00E000 Loading...
Green30 (0, 208, 0) #00D000 Loading...
Green40 (0, 192, 0) #00C000 Loading...
Green50 (0, 176, 0) #00B000 Loading...
Green60 (0, 160, 0) #00A000 Loading...
Green70 (0, 144, 0) #009000 Loading...
Green80 (0, 128, 0) #008000 Loading...
Green90 (0, 112, 0) #007000 Loading...
Green100 (0, 96, 0) #006000 Loading...
Color Name RGB Hex Preview
Yellow (255, 255, 0) #FFFF00 Loading...
Yellow10 (240, 240, 0) #F0F000 Loading...
Yellow20 (224, 224, 0) #E0E000 Loading...
Yellow30 (208, 208, 0) #D0D000 Loading...
Yellow40 (192, 192, 0) #C0C000 Loading...
Yellow50 (176, 176, 0) #B0B000 Loading...
Yellow60 (160, 160, 0) #A0A000 Loading...
Yellow70 (144, 144, 0) #909000 Loading...
Yellow80 (128, 128, 0) #808000 Loading...
Yellow90 (112, 112, 0) #707000 Loading...
Yellow100 (96, 96, 0) #606000 Loading...
Color Name RGB Hex Preview
Cyan (0, 255, 255) #00FFFF Loading...
Cyan10 (0, 240, 240) #00F0F0 Loading...
Cyan20 (0, 224, 224) #00E0E0 Loading...
Cyan30 (0, 208, 208) #00D0D0 Loading...
Cyan40 (0, 192, 192) #00C0C0 Loading...
Cyan50 (0, 176, 176) #00B0B0 Loading...
Cyan60 (0, 160, 160) #00A0A0 Loading...
Cyan70 (0, 144, 144) #009090 Loading...
Cyan80 (0, 128, 128) #008080 Loading...
Cyan90 (0, 112, 112) #007070 Loading...
Cyan100 (0, 96, 96) #006060 Loading...
Color Name RGB Hex Preview
Blue (0, 0, 255) #0000FF Loading...
Blue10 (0, 0, 240) #0000F0 Loading...
Blue20 (0, 0, 224) #0000E0 Loading...
Blue30 (0, 0, 208) #0000D0 Loading...
Blue40 (0, 0, 192) #0000C0 Loading...
Blue50 (0, 0, 176) #0000B0 Loading...
Blue60 (0, 0, 160) #0000A0 Loading...
Blue70 (0, 0, 144) #000090 Loading...
Blue80 (0, 0, 128) #000080 Loading...
Blue90 (0, 0, 112) #000070 Loading...
Blue100 (0, 0, 96) #000060 Loading...
Color Name RGB Hex Preview
Magenta (255, 0, 255) #FF00FF Loading...
Magenta10 (240, 0, 240) #F000F0 Loading...
Magenta20 (224, 0, 224) #E000E0 Loading...
Magenta30 (208, 0, 208) #D000D0 Loading...
Magenta40 (192, 0, 192) #C000C0 Loading...
Magenta50 (176, 0, 176) #B000B0 Loading...
Magenta60 (160, 0, 160) #A000A0 Loading...
Magenta70 (144, 0, 144) #900090 Loading...
Magenta80 (128, 0, 128) #800080 Loading...
Magenta90 (112, 0, 112) #700070 Loading...
Magenta100 (96, 0, 96) #600060 Loading...
Color Name RGB Hex Preview
White (255, 255, 255) #FFFFFF Loading...
White10 (240, 240, 240) #F0F0F0 Loading...
White20 (224, 224, 224) #E0E0E0 Loading...
White30 (208, 208, 208) #D0D0D0 Loading...
White40 (192, 192, 192) #C0C0C0 Loading...
White50 (176, 176, 176) #B0B0B0 Loading...
White60 (160, 160, 160) #A0A0A0 Loading...
White70 (144, 144, 144) #909090 Loading...
White80 (128, 128, 128) #808080 Loading...
White90 (112, 112, 112) #707070 Loading...
White100 (96, 96, 96) #606060 Loading...
Color Name RGB Hex Preview
Black (0, 0, 0) #000000 Loading...
Black10 (16, 16, 16) #101010 Loading...
Black20 (32, 32, 32) #202020 Loading...
Black30 (48, 48, 48) #303030 Loading...
Black40 (64, 64, 64) #404040 Loading...
Black50 (80, 80, 80) #505050 Loading...
Black60 (96, 96, 96) #606060 Loading...
Black70 (112, 112, 112) #707070 Loading...
Black80 (128, 128, 128) #808080 Loading...
Black90 (144, 144, 144) #909090 Loading...
Black100 (160, 160, 160) #A0A0A0 Loading...
Parameter Name Escape Code
Reset 0
Bold 1
Italic 3
Underline 4
Blink 5
Overline 53
Invert 7
Strike 9

Ups!!!

You scrolled down too much 😂