colors

package
v0.0.0-...-3827e29 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2025 License: BSL-1.0 Imports: 7 Imported by: 0

Documentation

Overview

Package colors implements ANSI escape sequences for coloring effects.

Several types of colors are supported:

  • 3-bit colors are provided as built-in constants
  • Bright variants can be obtained using Bright
  • XTerm (8-bit) colors can be obtained using NewXTerm
  • RGB (24-bit) colors can be obtained using NewRGB and NewRGBFromHex

Some terminals may not support all types of colors.

Configuration

This package includes built-in support for popular configuration options, including NO_COLOR and FORCE_COLOR. It also attempts to detect when a terminal does not support color at all and modify its configuration accordingly.

Use Enable and Disable to control color output for higher-level packages. Packages that add ANSI codes to text directly should review the documentation for IsEnabled.

Index

Examples

Constants

View Source
const (
	Black basic = iota
	Red
	Green
	Yellow
	Blue
	Magenta
	Cyan
	White
)

Variables

View Source
var ErrInvalidHexCode = errors.New("colors: hex code must be exactly 6 characters (excluding '#')")

ErrInvalidHexCode is returned when a hex color code is the wrong number of characters.

View Source
var ErrValueRange = errors.New("colors: value must be between 0 and 255 (inclusive)")

ErrValueRange is returned when a parameter outside the permitted range is passed to a Color constructor.

Functions

func Disable

func Disable()

Disable turns off color output for higher-level packages.

func Enable

func Enable()

Enable turns on color output for higher-level packages.

func IsEnabled

func IsEnabled() bool

IsEnabled reports whether color output is enabled. Higher-level packages that emit ANSI codes should use this as config state.

Colors are enabled when a terminal supports them unless disabled with NO_COLOR. Colors are always enabled if FORCE_COLOR is set to a non-zero value.

Types

type Color

type Color interface {
	TextCode() string
	BgCode() string
}

Color represents a coloring effect that can be applied to text or its background.

func Bright

func Bright(c Color) Color

Bright returns the bright variant of a color. It returns c unchanged if c is not a built-in Color constant.

func NewRGB

func NewRGB(r int, g int, b int) (Color, error)

NewRGB creates a 24-bit color using the specified intensity values for red, green, and blue components.

Returns ErrValueRange if r, g, or b is not between 0 and 255 (inclusive).

Example
package main

import (
	"fmt"

	"codefloe.com/binanary/style"
	"codefloe.com/binanary/style/colors"
)

func main() {
	brown, _ := colors.NewRGB(139, 69, 19)
	fmt.Println(style.Apply("This text is brown", brown, nil))
}

func NewRGBFromHex

func NewRGBFromHex(hexCode string) (Color, error)

NewRGB creates a 24-bit color based on a 6-character hex code (optionally prefixed with #).

Returns ErrInvalidHexCode if the hex code is not 6 characters long.

Example
package main

import (
	"fmt"

	"codefloe.com/binanary/style"
	"codefloe.com/binanary/style/colors"
)

func main() {
	brown, _ := colors.NewRGBFromHex("#8b4513")
	fmt.Println(style.Apply("This text is brown", brown, nil))
}

func NewXTerm

func NewXTerm(i int) (Color, error)

NewXTerm creates an 8-bit color for i-values between 0 and 255 (inclusive). Colors are arranged as follows:

  • 0-7: standard 3-bit colors (provided as constants in this package)
  • 8-15: bright variants of standard colors
  • 16-231: 6×6×6 color cube defined such that i=16+(36×r)+(6×g)+b, where 0≤r,g,b≤5
  • 232-255: 24 shades of grey from dark to light

Returns ErrValueRange if i is not between 0 and 255 (inclusive).

Jump to

Keyboard shortcuts

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