package ccolor
import (
"fmt"
"io"
"strconv"
)
type Color uint8
const (
fgBase uint8 = 30
fgMax uint8 = 37
bgBase uint8 = 40
bgMax uint8 = 47
hiFgBase uint8 = 90
hiFgMax uint8 = 97
hiBgBase uint8 = 100
hiBgMax uint8 = 107
optMax = 10
)
const (
FgBlack Color = iota + 30
FgRed
FgGreen
FgYellow
FgBlue
FgMagenta
FgCyan
FgWhite
FgDefault Color = 39
)
const (
FgDarkGray Color = iota + 90
FgLightRed
FgLightGreen
FgLightYellow
FgLightBlue
FgLightMagenta
FgLightCyan
FgLightWhite
FgGray Color = 90
)
const (
BgBlack Color = iota + 40
BgRed
BgGreen
BgYellow
BgBlue
BgMagenta
BgCyan
BgWhite
BgDefault Color = 49
)
const (
BgDarkGray Color = iota + 100
BgLightRed
BgLightGreen
BgLightYellow
BgLightBlue
BgLightMagenta
BgLightCyan
BgLightWhite
BgGray Color = 100
)
const (
OpReset Color = iota
OpBold
OpFuzzy
OpItalic
OpUnderscore
OpBlink
OpFastBlink
OpReverse
OpConcealed
OpStrikethrough
)
const (
Red = FgRed
Cyan = FgCyan
Gray = FgDarkGray
Blue = FgBlue
Black = FgBlack
Green = FgGreen
White = FgWhite
Yellow = FgYellow
Magenta = FgMagenta
Bold = OpBold
Normal = FgDefault
LightRed = FgLightRed
LightCyan = FgLightCyan
LightBlue = FgLightBlue
LightGreen = FgLightGreen
LightWhite = FgLightWhite
LightYellow = FgLightYellow
LightMagenta = FgLightMagenta
HiRed = FgLightRed
HiCyan = FgLightCyan
HiBlue = FgLightBlue
HiGreen = FgLightGreen
HiWhite = FgLightWhite
HiYellow = FgLightYellow
HiMagenta = FgLightMagenta
BgHiRed = BgLightRed
BgHiCyan = BgLightCyan
BgHiBlue = BgLightBlue
BgHiGreen = BgLightGreen
BgHiWhite = BgLightWhite
BgHiYellow = BgLightYellow
BgHiMagenta = BgLightMagenta
)
func (c Color ) Text (message string ) string { return RenderString (c .String (), message ) }
func (c Color ) Render (a ...any ) string { return RenderCode (c .String (), a ...) }
func (c Color ) Renderln (a ...any ) string { return RenderWithSpaces (c .String (), a ...) }
func (c Color ) Sprint (a ...any ) string { return RenderCode (c .String (), a ...) }
func (c Color ) Sprintf (format string , args ...any ) string {
return RenderString (c .String (), fmt .Sprintf (format , args ...))
}
func (c Color ) Print (args ...any ) {
doPrint (c .Code (), fmt .Sprint (args ...))
}
func (c Color ) Printf (format string , a ...any ) {
doPrint (c .Code (), fmt .Sprintf (format , a ...))
}
func (c Color ) Println (a ...any ) { doPrintln (c .String (), a ) }
func (c Color ) Fprint (w io .Writer , a ...any ) {
doPrintTo (w , c .String (), fmt .Sprint (a ...))
}
func (c Color ) Fprintf (w io .Writer , format string , a ...any ) {
doPrintTo (w , c .String (), fmt .Sprintf (format , a ...))
}
func (c Color ) Fprintln (w io .Writer , a ...any ) {
doPrintlnTo (w , c .String (), a )
}
func (c Color ) Light () Color {
val := uint8 (c )
if val >= 30 && val <= 47 {
return Color (val + 60 )
}
return c
}
func (c Color ) Darken () Color {
val := uint8 (c )
if val >= 90 && val <= 107 {
return Color (val - 60 )
}
return c
}
func (c Color ) ToFg () Color {
val := uint8 (c )
if val < 10 {
return c
}
return Color (Bg2Fg (val ))
}
func (c Color ) ToBg () Color {
val := uint8 (c )
if val < 10 {
return c
}
return Color (Fg2Bg (val ))
}
func (c Color ) Code () string {
return strconv .FormatInt (int64 (c ), 10 )
}
func (c Color ) String () string {
return strconv .FormatInt (int64 (c ), 10 )
}
func (c Color ) IsBg () bool {
val := uint8 (c )
return val >= bgBase && val <= bgMax || val >= hiBgBase && val <= hiBgMax
}
func (c Color ) IsFg () bool {
val := uint8 (c )
return val >= fgBase && val <= fgMax || val >= hiFgBase && val <= hiFgMax
}
func (c Color ) IsOption () bool { return uint8 (c ) < optMax }
func (c Color ) IsValid () bool { return uint8 (c ) < hiBgMax }
var FgColors = map [string ]Color {
"black" : FgBlack ,
"red" : FgRed ,
"green" : FgGreen ,
"yellow" : FgYellow ,
"blue" : FgBlue ,
"magenta" : FgMagenta ,
"cyan" : FgCyan ,
"white" : FgWhite ,
"default" : FgDefault ,
}
var BgColors = map [string ]Color {
"black" : BgBlack ,
"red" : BgRed ,
"green" : BgGreen ,
"yellow" : BgYellow ,
"blue" : BgBlue ,
"magenta" : BgMagenta ,
"cyan" : BgCyan ,
"white" : BgWhite ,
"default" : BgDefault ,
}
var ExFgColors = map [string ]Color {
"darkGray" : FgDarkGray ,
"lightRed" : FgLightRed ,
"lightGreen" : FgLightGreen ,
"lightYellow" : FgLightYellow ,
"lightBlue" : FgLightBlue ,
"lightMagenta" : FgLightMagenta ,
"lightCyan" : FgLightCyan ,
"lightWhite" : FgLightWhite ,
}
var ExBgColors = map [string ]Color {
"darkGray" : BgDarkGray ,
"lightRed" : BgLightRed ,
"lightGreen" : BgLightGreen ,
"lightYellow" : BgLightYellow ,
"lightBlue" : BgLightBlue ,
"lightMagenta" : BgLightMagenta ,
"lightCyan" : BgLightCyan ,
"lightWhite" : BgLightWhite ,
}
var AllOptions = map [string ]Color {
"reset" : OpReset ,
"bold" : OpBold ,
"fuzzy" : OpFuzzy ,
"italic" : OpItalic ,
"underscore" : OpUnderscore ,
"blink" : OpBlink ,
"reverse" : OpReverse ,
"concealed" : OpConcealed ,
}
func Bg2Fg (val uint8 ) uint8 {
if val >= bgBase && val <= 47 {
val = val - 10
} else if val >= hiBgBase && val <= 107 {
val = val - 10
}
return val
}
func Fg2Bg (val uint8 ) uint8 {
if val >= fgBase && val <= 37 {
val = val + 10
} else if val >= hiFgBase && val <= 97 {
val = val + 10
}
return val
}
The pages are generated with Golds v0.8.4 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .