Documentation
¶
Overview ¶
Author © 2026 alvesafk <migueldealmeidaalves55@gmail.com>
scolor makes it easy to colorize strings in go, it supports ansi and rgb colors.
Index ¶
- Variables
- func AddMod(s, mod string) string
- func BgGradient(s string, firstColor, secondColor Color) string
- func BgRGB(s string, color Color) string
- func FgGradient(s string, firstColor, secondColor Color) string
- func FgRGB(s string, color Color) string
- func RemoveEscapeSequence(s string) string
- func TmplGradient(s string, firstTemplate, secondTemplate RgbTemplate) string
- type Color
- func (color Color) BgPrint(a ...any) (n int, err error)
- func (color Color) BgPrintf(format string, a ...any) (n int, err error)
- func (color Color) BgPrintln(a ...any) (n int, err error)
- func (color Color) FgPrint(a ...any) (n int, err error)
- func (color Color) FgPrintf(format string, a ...any) (n int, err error)
- func (color Color) FgPrintln(a ...any) (n int, err error)
- type RgbTemplate
Constants ¶
This section is empty.
Variables ¶
var ( BLACK = Color{Red: 0, Green: 0, Blue: 0} BLUE = Color{Red: 0, Green: 0, Blue: 200} BROWN = Color{Red: 110, Green: 20, Blue: 20} CYAN = Color{Red: 0, Green: 200, Blue: 200} GREEN = Color{Red: 0, Green: 200, Blue: 0} ORANGE = Color{Red: 200, Green: 115, Blue: 0} PINK = Color{Red: 200, Green: 140, Blue: 150} PURPLE = Color{Red: 80, Green: 0, Blue: 80} RED = Color{Red: 200, Green: 0, Blue: 0} YELLOW = Color{Red: 200, Green: 200, Blue: 0} WHITE = Color{Red: 255, Green: 255, Blue: 255} )
Some preset colors, they all can be changed by the user of the lib.
var IsRGBSupported bool
IsRGBSupported tells if the user terminal has support to True Color in order to use the RGB colors.
Functions ¶
func AddMod ¶
func AddMod receives a string to be modified and a mod string, it returns the modified string, the mods are: "bold", "underline", "strike", "italic", if the mod string is different than this the function returns the string to be modified.
Usage:
boldString := AddMod("Hello, world!", "bold")
func BgGradient ¶
func BgGradient receives a string and two colors, it returns a string whose background is colored with a gradient, starting in the first color going to the second color.
Usage:
pinkToBlueString := FgGradient("Hello, world!", pink, RGB(104, 150, 214))
func BgRGB ¶
func BgRGB receives a string and a color, it returns a string which it's background is colored.
Usage:
pinkString := BgRGB("Hello, world!", pink)
func FgGradient ¶
func FgGradient receives a string and two colors, it returns a string whose foreground is colored with a gradient, starting in the first color going to the second color.
Usage:
pinkToBlueString := FgGradient("Hello, world!", pink, RGB(104, 150, 214))
func FgRGB ¶
func FgRGB receives a string and a color, it returns a string which it's foreground is colored.
Usage:
pinkString := FgRGB("Hello, world!", pink)
func RemoveEscapeSequence ¶ added in v1.1.0
func RemoveEscapeSequence receives a string and returns the same string without the escape sequences if any.
Usage:
boldString := AddMod("Hello, world!", "bold") cleanString := RemoveEscapeSequence(boldString)
func TmplGradient ¶
func TmplGradient(s string, firstTemplate, secondTemplate RgbTemplate) string
func TmplGradient accepts a string and two templates, it returns a colored string with a back and foreground gradient based on the templates, the gradient begins with the first one and goes to the second one.
Usage:
stringWhiteToBlack := TmplGradient("Hello, world!", whiteBgWithBlackFg, blackBgWithWhiteFg)
Types ¶
type Color ¶
type Color struct {
Red, Green, Blue int
}
Color struct, it defines a 24bit RGB color, it has Red, Green and Blue fields.
func RGB ¶
func RGB receives a red, green and blue int and returns a instantiated Color struct. It will check if the value that is being passed is greater than 255 (8 bits), this is done because RGB Colors are 24 bits, 8 bits (red) + 8 bits (green) + 8 bits (blue).
Usage:
pink := RGB(215, 106, 151)
func (Color) BgPrint ¶
func BgPrint is a color method for printing text with a colored background on the terminal, the use is identical to the fmt Print function.
Usage:
pink.BgPrint("Hello, ", "World")
func (Color) BgPrintf ¶
func BgPrintf is a color method for printing text with a colored background on the terminal, the use is identical to the fmt Printf function.
Usage:
pink.BgPrintf("Hello, ", "World")
func (Color) BgPrintln ¶
func BgPrintln is a color method for printing text with a colored background on the terminal, the use is identical to the fmt Println function.
Usage:
pink.BgPrintln("Hello, ", "World")
func (Color) FgPrint ¶
func FgPrint is a color method for printing text with a colored foreground on the terminal, the use is identical to the fmt Print function.
Usage:
pink.FgPrint("Hello, ", "World")
type RgbTemplate ¶
type RgbTemplate struct {
Bg, Fg Color
}
RgbTemplate accepts a background color and a foreground color, making it easier to print strings with colored background and foreground.
func CreateRgbTemplate ¶
func CreateRgbTemplate(bg, fg Color) *RgbTemplate
func CreateRgbTemplate receives a background color and a foreground color, it returns a initialized RgbTemplate struct.
Usage:
whiteBgWithBlackFg := CreateRgbTemplate(RGB(255, 255, 255), RGB(0, 0, 0))
func (RgbTemplate) FormatString ¶
func (template RgbTemplate) FormatString(s string) string
func FormatString is a RgbTemplate method, it receives a string and returns a formatted string with the colors of the template.
Usage;
stringWithWhiteBgBlackFg := whiteBgWithBlackFg.FormatString("Hello, world!")
func (RgbTemplate) Print ¶
func (template RgbTemplate) Print(a ...any) (n int, err error)
func Print is a RgbTemplate method for printing text with the background and foreground of the template onto the terminal, the use is identical to the fmt Print function.
Usage:
whiteBgWithBlackFg.Print("Hello, ", "World!")
func (RgbTemplate) Printf ¶
func (template RgbTemplate) Printf(format string, a ...any) (n int, err error)
func Printf is a RgbTemplate method for printing text with the background and foreground of the template onto the terminal, the use is identical to the fmt Printf function.
Usage:
whiteBgWithBlackFg.Printf("Hello, ", "World!")
func (RgbTemplate) Println ¶
func (template RgbTemplate) Println(a ...any) (n int, err error)
func Println is a RgbTemplate method for printing text with the background and foreground of the template onto the terminal, the use is identical to the fmt Println function.
Usage:
whiteBgWithBlackFg.Println("Hello, ", "World!")