An open API service indexing awesome lists of open source software.

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

Awesome Lists containing this project

README

        

AtomicGo | color


Downloads


Latest Release


Tests


Coverage


Unit test count


License: MIT



Go report

---


Documentation
|
Contributing
|
Code of Conduct

---


AtomicGo


go get atomicgo.dev/color



# color

```go
import "atomicgo.dev/color"
```

Package color is a minimalistic package for coloring terminal output.

```go
package main

import (
"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).Sprint

Red = NewStyle(ANSIRed, nil).Sprint
BrightRed = NewStyle(ANSIBrightRed, nil).Sprint

Green = NewStyle(ANSIGreen, nil).Sprint
BrightGreen = NewStyle(ANSIBrightGreen, nil).Sprint

Yellow = NewStyle(ANSIYellow, nil).Sprint
BrightYellow = NewStyle(ANSIBrightYellow, nil).Sprint

Blue = NewStyle(ANSIBlue, nil).Sprint
BrigthBlue = NewStyle(ANSIBrightBlue, nil).Sprint

Magenta = NewStyle(ANSIMagenta, nil).Sprint
BrightMagenta = NewStyle(ANSIBrightMagenta, nil).Sprint

Cyan = NewStyle(ANSICyan, nil).Sprint
BrightCyan = NewStyle(ANSIBrightCyan, nil).Sprint

White = 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
```


## type [ANSI256Color]()

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.


## type [ANSIColor]()

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.


## type [Color]()

Color is an interface for colors.

```go
type Color interface {
Sequence(background bool) string
}
```

```go
var NoColor Color = noColor{}
```


### func [NewColorFromHex]()

```go
func NewColorFromHex(hex string) Color
```

NewColorFromHex creates a new Color from a hex string. If the hex string is invalid, NoColor is returned.


### func [NewColorFromRGB]()

```go
func NewColorFromRGB(r, g, b uint8) Color
```

NewColorFromRGB creates a new Color from RGB values.


## type [Mode]()

Mode represents the color mode used by the terminal.

```go
type Mode int
```

```go
const (
TrueColor Mode = iota
ANSI256
ANSI

Disabled
)
```


### func \(Mode\) [String]()

```go
func (m Mode) String() string
```


## type [Modifier]()

Modifier type for text modifiers.

```go
type Modifier int
```

Modifiers

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


## type [RGBColor]()

RGBColor represents a color in the RGB color space.

```go
type RGBColor struct {
R, G, B uint8
}
```


### func \(RGBColor\) [Hex]()

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


## type [Style]()

Style represents a text style with a foreground and background color and modifiers.

```go
type Style struct {
Foreground Color
Background Color

Modifiers []Modifier
}
```


### func [NewStyle]()

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


### func \(Style\) [Fprint]()

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


### func \(Style\) [Print]()

```go
func (s Style) Print(a ...any)
```

Print formats using the default formats for its operands and writes to standard output.


### func \(Style\) [Printf]()

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


### func \(Style\) [Sprint]()

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