https://github.com/atomicgo/color
🎨 Simple colors for your terminal in Go
https://github.com/atomicgo/color
atomicgo go golang golang-library hacktoberfest
Last synced: about 2 months ago
JSON representation
🎨 Simple colors for your terminal in Go
- Host: GitHub
- URL: https://github.com/atomicgo/color
- Owner: atomicgo
- License: mit
- Created: 2024-07-13T12:51:45.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2025-02-01T13:22:19.000Z (5 months ago)
- Last Synced: 2025-02-19T00:34:23.948Z (4 months ago)
- Topics: atomicgo, go, golang, golang-library, hacktoberfest
- Language: Go
- Homepage: https://atomicgo.dev
- Size: 57.6 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
AtomicGo | color
---
Documentation
|
Contributing
|
Code of Conduct---
![]()
go get atomicgo.dev/color
# color
```go
import "atomicgo.dev/color"
```Package color is a minimalistic package for coloring terminal output.
```go
package mainimport (
"fmt""atomicgo.dev/color"
)func main() {
// Simple coloring
fmt.Println("Hello, " + color.Green("World") + "!")fmt.Println() // blank line
// Theme colors - can be customized in init() function if needed
fmt.Println(color.Success("Success message"))
fmt.Println(color.Info("Info message"))
fmt.Println(color.Warning("Warning message"))
fmt.Println(color.Error("Error message"))
fmt.Println(color.Fatal("Fatal message"))fmt.Println() // blank line
// Supports ANSI colors
ansiRed := color.NewStyle(color.ANSIRed, nil).Sprint
fmt.Println(ansiRed("This is printed red using an ANSI color code"))// Supports ANSI256 colors
ansi256Red := color.NewStyle(color.ANSI256Color(196), nil).Sprint
fmt.Println(ansi256Red("This is printed red using an ANSI256 color code"))// Supports RGB colors
redRGB := color.NewStyle(color.NewColorFromRGB(255, 0, 0), nil).Sprint
fmt.Println(redRGB("This is printed red using a RGB color code"))// Supports hex colors
redHex := color.NewStyle(color.NewColorFromHex("#ff0000"), nil).Sprint
fmt.Println(redHex("This is printed red using a hex color code"))
}
```## Index
- [Variables](<#variables>)
- [type ANSI256Color](<#ANSI256Color>)
- [func \(c ANSI256Color\) Sequence\(background bool\) string](<#ANSI256Color.Sequence>)
- [func \(c ANSI256Color\) String\(\) string](<#ANSI256Color.String>)
- [type ANSIColor](<#ANSIColor>)
- [func \(c ANSIColor\) Sequence\(background bool\) string](<#ANSIColor.Sequence>)
- [type Color](<#Color>)
- [func NewColorFromHex\(hex string\) Color](<#NewColorFromHex>)
- [func NewColorFromRGB\(r, g, b uint8\) Color](<#NewColorFromRGB>)
- [type Mode](<#Mode>)
- [func \(m Mode\) String\(\) string](<#Mode.String>)
- [type Modifier](<#Modifier>)
- [func \(m Modifier\) Sequence\(\) string](<#Modifier.Sequence>)
- [type RGBColor](<#RGBColor>)
- [func \(c RGBColor\) Hex\(\) string](<#RGBColor.Hex>)
- [func \(c RGBColor\) Sequence\(background bool\) string](<#RGBColor.Sequence>)
- [type Style](<#Style>)
- [func NewStyle\(foregroundColor, backgroundColor Color, modifiers ...Modifier\) Style](<#NewStyle>)
- [func \(s \*Style\) AddModifier\(modifier Modifier\)](<#Style.AddModifier>)
- [func \(s Style\) Fprint\(w io.Writer, a ...any\) \(n int, err error\)](<#Style.Fprint>)
- [func \(s Style\) Fprintf\(w io.Writer, format string, a ...any\) \(n int, err error\)](<#Style.Fprintf>)
- [func \(s Style\) Fprintfln\(w io.Writer, format string, a ...any\) \(n int, err error\)](<#Style.Fprintfln>)
- [func \(s Style\) Fprintln\(w io.Writer, a ...any\) \(n int, err error\)](<#Style.Fprintln>)
- [func \(s Style\) Print\(a ...any\)](<#Style.Print>)
- [func \(s Style\) Printf\(format string, a ...any\)](<#Style.Printf>)
- [func \(s Style\) Printfln\(format string, a ...any\)](<#Style.Printfln>)
- [func \(s Style\) Println\(a ...any\)](<#Style.Println>)
- [func \(s Style\) Sequence\(\) string](<#Style.Sequence>)
- [func \(s Style\) Sprint\(a ...any\) string](<#Style.Sprint>)
- [func \(s Style\) Sprintf\(format string, a ...any\) string](<#Style.Sprintf>)
- [func \(s Style\) WithModifier\(modifier Modifier\) Style](<#Style.WithModifier>)## Variables
```go
var (
// ANSI colors
Black = NewStyle(ANSIBlack, nil).Sprint
BrightBlack = NewStyle(ANSIBrightBlack, nil).SprintRed = NewStyle(ANSIRed, nil).Sprint
BrightRed = NewStyle(ANSIBrightRed, nil).SprintGreen = NewStyle(ANSIGreen, nil).Sprint
BrightGreen = NewStyle(ANSIBrightGreen, nil).SprintYellow = NewStyle(ANSIYellow, nil).Sprint
BrightYellow = NewStyle(ANSIBrightYellow, nil).SprintBlue = NewStyle(ANSIBlue, nil).Sprint
BrigthBlue = NewStyle(ANSIBrightBlue, nil).SprintMagenta = NewStyle(ANSIMagenta, nil).Sprint
BrightMagenta = NewStyle(ANSIBrightMagenta, nil).SprintCyan = NewStyle(ANSICyan, nil).Sprint
BrightCyan = NewStyle(ANSIBrightCyan, nil).SprintWhite = NewStyle(ANSIWhite, nil).Sprint
BrightWhite = NewStyle(ANSIBrightWhite, nil).Sprint// Special colors
Success = NewStyle(ANSIBrightGreen, nil).Sprint
Info = NewStyle(ANSIBrightBlue, nil).Sprint
Warning = NewStyle(ANSIBrightYellow, nil).Sprint
Error = NewStyle(ANSIBrightRed, nil).Sprint
Fatal = NewStyle(ANSIBrightRed, nil, Bold).Sprint
)
```Writer is the writer to write colorized output to.
```go
var Writer io.Writer = os.Stdout
```ANSI256Color represents a color in the ANSI256 color palette.
```go
type ANSI256Color uint8
```
### func \(ANSI256Color\) [Sequence]()```go
func (c ANSI256Color) Sequence(background bool) string
```Sequence returns the ANSI escape sequence for the color.
### func \(ANSI256Color\) [String]()```go
func (c ANSI256Color) String() string
```String returns the hex code of the color.
ANSIColor represents an ANSI color code.
```go
type ANSIColor int
``````go
const (
ANSIBlack ANSIColor = iota
ANSIRed
ANSIGreen
ANSIYellow
ANSIBlue
ANSIMagenta
ANSICyan
ANSIWhite
ANSIBrightBlack
ANSIBrightRed
ANSIBrightGreen
ANSIBrightYellow
ANSIBrightBlue
ANSIBrightMagenta
ANSIBrightCyan
ANSIBrightWhite
)
```
### func \(ANSIColor\) [Sequence]()```go
func (c ANSIColor) Sequence(background bool) string
```Sequence represents the ANSI escape sequence for the color.
Color is an interface for colors.
```go
type Color interface {
Sequence(background bool) string
}
``````go
var NoColor Color = noColor{}
``````go
func NewColorFromHex(hex string) Color
```NewColorFromHex creates a new Color from a hex string. If the hex string is invalid, NoColor is returned.
```go
func NewColorFromRGB(r, g, b uint8) Color
```NewColorFromRGB creates a new Color from RGB values.
Mode represents the color mode used by the terminal.
```go
type Mode int
``````go
const (
TrueColor Mode = iota
ANSI256
ANSIDisabled
)
``````go
func (m Mode) String() string
```Modifier type for text modifiers.
```go
type Modifier int
``````go
const (
Reset Modifier = iota
Bold
Faint
Italic
Underline
)
```
### func \(Modifier\) [Sequence]()```go
func (m Modifier) Sequence() string
```Sequence returns the ANSI escape sequence for the modifier.
RGBColor represents a color in the RGB color space.
```go
type RGBColor struct {
R, G, B uint8
}
``````go
func (c RGBColor) Hex() string
```Hex returns the hex representation of the color.
### func \(RGBColor\) [Sequence]()```go
func (c RGBColor) Sequence(background bool) string
```Sequence returns the ANSI escape sequence for the color.
Style represents a text style with a foreground and background color and modifiers.
```go
type Style struct {
Foreground Color
Background ColorModifiers []Modifier
}
``````go
func NewStyle(foregroundColor, backgroundColor Color, modifiers ...Modifier) Style
```NewStyle creates a new Style with the given foreground and background colors and modifiers.
### func \(\*Style\) [AddModifier]()```go
func (s *Style) AddModifier(modifier Modifier)
```AddModifier adds a modifier to the style, if it's not already present.
```go
func (s Style) Fprint(w io.Writer, a ...any) (n int, err error)
```Fprint formats using the default formats for its operands and writes to w.
### func \(Style\) [Fprintf]()```go
func (s Style) Fprintf(w io.Writer, format string, a ...any) (n int, err error)
```Fprintf formats according to a format specifier and writes to w.
### func \(Style\) [Fprintfln]()```go
func (s Style) Fprintfln(w io.Writer, format string, a ...any) (n int, err error)
```Fprintfln formats according to a format specifier and writes to w.
### func \(Style\) [Fprintln]()```go
func (s Style) Fprintln(w io.Writer, a ...any) (n int, err error)
```Fprintln formats using the default formats for its operands and writes to w.
```go
func (s Style) Print(a ...any)
```Print formats using the default formats for its operands and writes to standard output.
```go
func (s Style) Printf(format string, a ...any)
```Printf formats according to a format specifier and writes to standard output.
### func \(Style\) [Printfln]()```go
func (s Style) Printfln(format string, a ...any)
```Printfln formats according to a format specifier and writes to standard output.
### func \(Style\) [Println]()```go
func (s Style) Println(a ...any)
```Println formats using the default formats for its operands and writes to standard output.
### func \(Style\) [Sequence]()```go
func (s Style) Sequence() string
```Sequence returns the ANSI escape sequence for the style.
```go
func (s Style) Sprint(a ...any) string
```Sprint formats using the default formats for its operands and returns the resulting string.
### func \(Style\) [Sprintf]()```go
func (s Style) Sprintf(format string, a ...any) string
```Sprintf formats according to a format specifier and returns the resulting string.
### func \(Style\) [WithModifier]()```go
func (s Style) WithModifier(modifier Modifier) Style
```WithModifier returns a new Style with the given modifier added, if it's not already present.
Generated by [gomarkdoc]()
---
> [AtomicGo.dev](https://atomicgo.dev) ·
> with ❤️ by [@MarvinJWendt](https://github.com/MarvinJWendt) |
> [MarvinJWendt.com](https://marvinjwendt.com)