scolor

package module
v1.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 2, 2026 License: MIT Imports: 6 Imported by: 0

README

String colorizer - scolor

Go Report Card

scolor is a lib made for Go making it easier to use colorized strings in your programs.

Showcase gif

Was originally made inside of another project of mine, agopass, i decided to make it separate from the orginal project because i will be using it on other projects, specially because this version is way better then the original.

The lib has a main scolor package that format strings using 24bit colors and a separate ansi package that uses the colors defined by the terminal.

Import it in your code!

import (
    "github.com/Alvesafk/scolor" // main 24 bit RGB colors

    "github.com/Alvesafk/scolor/ansi" // ansi color package
)

Just run go mod tidy after this and the lib will be available in your code.

The documentation can be found by using the go doc command on the codebase, you can also read it directly from the source code, or, read it on the official go pkgs website.

Inside the source code there is an examples directory with code for the main scolor package and the ansi package, you can test them with go run or just see the gif on their READMEs.

Roadmap:

  • Gradient formatting
  • Tests
  • Better examples

This library was released with the MIT license.

Documentation

Overview

Author © 2026 alvesafk <migueldealmeidaalves55@gmail.com>

scolor makes it easy to colorize strings in go, it supports ansi and rgb colors.

Index

Constants

View Source
const (
	Bold = iota + 1
	Underline
	Strike
	Italic
)

Mod "enum", it's used on a switch statement for the AddMod function.

Variables

View Source
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.

View Source
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(s string, mod int) string

func AddMod receives a string to be modified and a mod, its recomended the use of the "enum" defined within this package, 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(s string, firstColor, secondColor Color) string

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(s string, color Color) string

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(s string, firstColor, secondColor Color) string

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(s string, color Color) string

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(s string) string

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(red, green, blue int) Color

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 (color Color) BgPrint(a ...any) (n int, err error)

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 (color Color) BgPrintf(format string, a ...any) (n int, err error)

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 (color Color) BgPrintln(a ...any) (n int, err error)

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 (color Color) FgPrint(a ...any) (n int, err error)

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

func (Color) FgPrintf

func (color Color) FgPrintf(format string, a ...any) (n int, err error)

func FgPrintf is a color method for printing text with a colored foreground on the terminal, the use is identical to the fmt Printf function.

Usage:

pink.FgPrintf("Hello, ", "World")

func (Color) FgPrintln

func (color Color) FgPrintln(a ...any) (n int, err error)

func FgPrintln is a color method for printing text with a colored foreground on the terminal, the use is identical to the fmt Println function.

Usage:

pink.FgPrintln("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!")

Directories

Path Synopsis
examples
ansi command
rgb command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL