Documentation ¶
Overview ¶
Package color is command line color library. Support rich color rendering output, universal API method, compatible with Windows system
Source code and other details for the project are available at GitHub:
https://github.com/gookit/color
More usage please see README and tests.
Example ¶
// quick use like fmt.Print* Red.Println("Simple to use color") Green.Print("Simple to use color") Cyan.Printf("Simple to use %s\n", "color") Gray.Printf("Simple to use %s\n", "color") Blue.Printf("Simple to use %s\n", "color") Yellow.Printf("Simple to use %s\n", "color") Magenta.Printf("Simple to use %s\n", "color") // use like func red := FgRed.Render green := FgGreen.Render fmt.Printf("%s line %s library\n", red("Command"), green("color")) // custom color New(FgWhite, BgBlack).Println("custom color style") // can also: Style{FgCyan, OpBold}.Println("custom color style") // internal theme/style: Info.Tips("message") Info.Prompt("message") Info.Println("info message") Warn.Println("warning message") Error.Println("error message") Danger.Println("danger message") // use style tag Print("<suc>he</><comment>llo</>, <cyan>wel</><red>come</>\n") // apply a style tag Tag("info").Println("info style text") // prompt message Info.Prompt("prompt style message") Warn.Prompt("prompt style message") // tips message Info.Tips("tips style message") Warn.Tips("tips style message")
Output:
Index ¶
- Constants
- Variables
- func AddStyle(name string, s Style)
- func AddTheme(name string, style Style)
- func ApplyTag(tag string, a ...any) string
- func Basic2hex(val uint8) string
- func Basic2nameMap() map[uint8]string
- func BasicToHex(val uint8) string
- func Bg2Fg(val uint8) uint8
- func Bluef(format string, a ...any)
- func Blueln(a ...any)
- func Bluep(a ...any)
- func C256ToRgb(val uint8) (rgb []uint8)
- func C256ToRgbV1(val uint8) (rgb []uint8)
- func ClearCode(str string) string
- func ClearTag(s string) string
- func Colors2code(colors ...Color) string
- func Cyanf(format string, a ...any)
- func Cyanln(a ...any)
- func Cyanp(a ...any)
- func Disable() bool
- func Errorf(format string, a ...any)
- func Errorln(a ...any)
- func Errorp(a ...any)
- func Fg2Bg(val uint8) uint8
- func ForceColor() terminfo.ColorLevel
- func ForceOpenColor() terminfo.ColorLevel
- func ForceSetColorLevel(level terminfo.ColorLevel) terminfo.ColorLevel
- func Fprint(w io.Writer, a ...any)
- func Fprintf(w io.Writer, format string, a ...any)
- func Fprintln(w io.Writer, a ...any)
- func GetColorTags() map[string]string
- func GetTagCode(name string) string
- func Grayf(format string, a ...any)
- func Grayln(a ...any)
- func Grayp(a ...any)
- func Greenf(format string, a ...any)
- func Greenln(a ...any)
- func Greenp(a ...any)
- func Hex2basic(hex string, asBg ...bool) uint8
- func Hex2rgb(hex string) []int
- func HexToRGB(hex string) []int
- func HexToRgb(hex string) (rgb []int)
- func HslIntToRgb(h, s, l int) (rgb []uint8)
- func HslToRgb(h, s, l float64) (rgb []uint8)
- func HsvToRgb(h, s, v int) (rgb []uint8)
- func Infof(format string, a ...any)
- func Infoln(a ...any)
- func Infop(a ...any)
- func InnerErrs() []error
- func IsConsole(w io.Writer) bool
- func IsDefinedTag(name string) bool
- func IsLikeInCmd() booldeprecated
- func IsMSys() bool
- func IsSupport16Color() bool
- func IsSupport256Color() bool
- func IsSupportColor() bool
- func IsSupportRGBColor() bool
- func IsSupportTrueColor() bool
- func IsTerminal(fd uintptr) bool
- func IsWindows() bool
- func Lprint(l *log.Logger, a ...any)
- func Magentaf(format string, a ...any)
- func Magentaln(a ...any)
- func Magentap(a ...any)
- func NotRenderTag()
- func ParseCodeFromAttr(attr string) (code string)
- func Print(a ...any)
- func Printf(format string, a ...any)
- func Println(a ...any)
- func Redf(format string, a ...any)
- func Redln(a ...any)
- func Redp(a ...any)
- func Render(a ...any) string
- func RenderCode(code string, args ...any) string
- func RenderString(code string, str string) string
- func RenderWithSpaces(code string, args ...any) string
- func ReplaceTag(str string) string
- func Reset() (int, error)
- func ResetOptions()
- func ResetOutput()
- func ResetTerminal() error
- func Rgb2ansi(r, g, b uint8, isBg bool) uint8
- func Rgb2basic(r, g, b uint8, isBg bool) uint8
- func Rgb2hex(rgb []int) string
- func Rgb2short(r, g, b uint8) uint8
- func RgbTo256(r, g, b uint8) uint8
- func RgbTo256Table() map[string]uint8
- func RgbToAnsi(r, g, b uint8, isBg bool) uint8
- func RgbToHex(rgb []int) string
- func RgbToHsl(r, g, b uint8) []float64
- func RgbToHslInt(r, g, b uint8) []int
- func Set(colors ...Color) (int, error)
- func SetOutput(w io.Writer)
- func SetTerminal(code string) error
- func Sprint(a ...any) string
- func Sprintf(format string, a ...any) string
- func String(s string) string
- func Successf(format string, a ...any)
- func Successln(a ...any)
- func Successp(a ...any)
- func Support256Color() bool
- func SupportColor() bool
- func SupportTrueColor() bool
- func Text(s string) string
- func Warnf(format string, a ...any)
- func Warnln(a ...any)
- func Warnp(a ...any)
- func WrapTag(s string, tag string) string
- func Yellowf(format string, a ...any)
- func Yellowln(a ...any)
- func Yellowp(a ...any)
- type Basic
- type Bit8Color
- type Color
- func (c Color) C256() Color256
- func (c Color) Code() string
- func (c Color) Darken() Color
- func (c Color) IsBg() bool
- func (c Color) IsFg() bool
- func (c Color) IsOption() bool
- func (c Color) IsValid() bool
- func (c Color) Light() Color
- func (c Color) Name() string
- func (c Color) Print(args ...any)
- func (c Color) Printf(format string, a ...any)
- func (c Color) Println(a ...any)
- func (c Color) RGB() RGBColor
- func (c Color) Render(a ...any) string
- func (c Color) Renderln(a ...any) string
- func (c Color) Sprint(a ...any) string
- func (c Color) Sprintf(format string, args ...any) string
- func (c Color) String() string
- func (c Color) Text(message string) string
- func (c Color) ToBg() Color
- func (c Color) ToFg() Color
- type Color256
- func (c Color256) Basic() Color
- func (c Color256) C16() Color
- func (c Color256) Code() string
- func (c Color256) FullCode() string
- func (c Color256) IsBg() bool
- func (c Color256) IsEmpty() bool
- func (c Color256) IsFg() bool
- func (c Color256) Print(a ...any)
- func (c Color256) Printf(format string, a ...any)
- func (c Color256) Println(a ...any)
- func (c Color256) RGB() RGBColor
- func (c Color256) RGBColor() RGBColor
- func (c Color256) Reset() error
- func (c Color256) Set() error
- func (c Color256) Sprint(a ...any) string
- func (c Color256) Sprintf(format string, a ...any) string
- func (c Color256) String() string
- func (c Color256) ToBg() Color256
- func (c Color256) ToFg() Color256
- func (c Color256) Value() uint8
- type Level
- type Opts
- type Printer
- type PrinterFace
- type RGBColor
- func Bit24(r, g, b uint8, isBg ...bool) RGBColor
- func HEX(hex string, isBg ...bool) RGBColor
- func HSL(h, s, l float64, isBg ...bool) RGBColor
- func HSLInt(h, s, l int, isBg ...bool) RGBColor
- func Hex(hex string, isBg ...bool) RGBColor
- func Hsl(h, s, l float64, isBg ...bool) RGBColor
- func HslInt(h, s, l int, isBg ...bool) RGBColor
- func RGB(r, g, b uint8, isBg ...bool) RGBColor
- func RGBFromHEX(hex string, isBg ...bool) RGBColor
- func RGBFromSlice(rgb []uint8, isBg ...bool) RGBColor
- func RGBFromString(rgb string, isBg ...bool) RGBColor
- func Rgb(r, g, b uint8, isBg ...bool) RGBColor
- func RgbFromInt(r, g, b int, isBg ...bool) RGBColor
- func RgbFromInts(rgb []int, isBg ...bool) RGBColor
- func (c RGBColor) Basic() Color
- func (c RGBColor) C16() Color
- func (c RGBColor) C256() Color256
- func (c RGBColor) Code() string
- func (c RGBColor) Color() Color
- func (c RGBColor) FullCode() string
- func (c RGBColor) Hex() string
- func (c RGBColor) IsEmpty() bool
- func (c RGBColor) Print(a ...any)
- func (c RGBColor) Printf(format string, a ...any)
- func (c RGBColor) Println(a ...any)
- func (c RGBColor) Reset() error
- func (c RGBColor) RgbString() string
- func (c RGBColor) Set() error
- func (c RGBColor) Sprint(a ...any) string
- func (c RGBColor) Sprintf(format string, a ...any) string
- func (c RGBColor) String() string
- func (c RGBColor) ToBg() RGBColor
- func (c RGBColor) ToFg() RGBColor
- func (c RGBColor) Values() []int
- type RGBStyle
- func (s *RGBStyle) AddOpts(opts ...Color) *RGBStyle
- func (s *RGBStyle) Code() string
- func (s *RGBStyle) FullCode() string
- func (s *RGBStyle) IsEmpty() bool
- func (s *RGBStyle) Print(a ...any)
- func (s *RGBStyle) Printf(format string, a ...any)
- func (s *RGBStyle) Println(a ...any)
- func (s *RGBStyle) Set(fg, bg RGBColor, opts ...Color) *RGBStyle
- func (s *RGBStyle) SetBg(bg RGBColor) *RGBStyle
- func (s *RGBStyle) SetFg(fg RGBColor) *RGBStyle
- func (s *RGBStyle) SetOpts(opts Opts) *RGBStyle
- func (s *RGBStyle) Sprint(a ...any) string
- func (s *RGBStyle) Sprintf(format string, a ...any) string
- func (s *RGBStyle) String() string
- type Scheme
- type SimplePrinter
- func (s *SimplePrinter) Errorf(format string, a ...any)
- func (s *SimplePrinter) Errorln(a ...any)
- func (s *SimplePrinter) Infof(format string, a ...any)
- func (s *SimplePrinter) Infoln(a ...any)
- func (s *SimplePrinter) Print(v ...any)
- func (s *SimplePrinter) Printf(format string, v ...any)
- func (s *SimplePrinter) Println(v ...any)
- func (s *SimplePrinter) Successf(format string, a ...any)
- func (s *SimplePrinter) Successln(a ...any)
- func (s *SimplePrinter) Warnf(format string, a ...any)
- func (s *SimplePrinter) Warnln(a ...any)
- type Style
- func (s *Style) Add(cs ...Color)
- func (s Style) Code() string
- func (s Style) IsEmpty() bool
- func (s Style) Print(a ...any)
- func (s Style) Printf(format string, a ...any)
- func (s Style) Println(a ...any)
- func (s Style) Render(a ...any) string
- func (s Style) Renderln(a ...any) string
- func (s Style) Save(name string)
- func (s Style) Sprint(a ...any) string
- func (s Style) Sprintf(format string, a ...any) string
- func (s Style) String() string
- type Style256
- func (s *Style256) AddOpts(opts ...Color) *Style256
- func (s *Style256) Code() string
- func (s *Style256) Print(a ...any)
- func (s *Style256) Printf(format string, a ...any)
- func (s *Style256) Println(a ...any)
- func (s *Style256) Set(fgVal, bgVal uint8, opts ...Color) *Style256
- func (s *Style256) SetBg(bgVal uint8) *Style256
- func (s *Style256) SetFg(fgVal uint8) *Style256
- func (s *Style256) SetOpts(opts Opts) *Style256
- func (s *Style256) Sprint(a ...any) string
- func (s *Style256) Sprintf(format string, a ...any) string
- func (s *Style256) String() string
- type Tag
- type TagParser
- type Theme
Examples ¶
Constants ¶
const ( // StartSet chars StartSet = "\x1b[" // ResetSet close all properties. ResetSet = "\x1b[0m" // SettingTpl string. SettingTpl = "\x1b[%sm" // FullColorTpl for build color code FullColorTpl = "\x1b[%sm%s\x1b[0m" // CodeSuffix string for color code. CodeSuffix = "[0m" )
color render templates
ESC 操作的表示:
"\033"(Octal 8进制) = "\x1b"(Hexadecimal 16进制) = 27 (10进制)
const ( // OptMax max option value. range: 0 - 9 OptMax = 10 // DiffFgBg diff foreground and background color DiffFgBg = 10 )
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 )
Boundary value for foreground/background color 16
- base: fg 30~37, bg 40~47
- light: fg 90~97, bg 100~107
const ( Red = FgRed Cyan = FgCyan Gray = FgDarkGray // is light Black 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 )
There are basic and light foreground color aliases
const ( TplFg256 = "38;5;%d" TplBg256 = "48;5;%d" Fg256Pfx = "38;5;" Bg256Pfx = "48;5;" )
tpl for 8 bit 256 color(`2^8`)
format:
ESC[ … 38;5;<n> … m // 选择前景色 ESC[ … 48;5;<n> … m // 选择背景色
example:
fg "\x1b[38;5;242m" bg "\x1b[48;5;208m" both "\x1b[38;5;242;48;5;208m"
links:
https://zh.wikipedia.org/wiki/ANSI%E8%BD%AC%E4%B9%89%E5%BA%8F%E5%88%97#8位
const ( TplFgRGB = "38;2;%d;%d;%d" TplBgRGB = "48;2;%d;%d;%d" FgRGBPfx = "38;2;" BgRGBPfx = "48;2;" )
24 bit RGB color RGB:
R 0-255 G 0-255 B 0-255 R 00-FF G 00-FF B 00-FF (16进制)
Format:
ESC[ … 38;2;<r>;<g>;<b> … m // Select RGB foreground color ESC[ … 48;2;<r>;<g>;<b> … m // Choose RGB background color
links:
https://zh.wikipedia.org/wiki/ANSI%E8%BD%AC%E4%B9%89%E5%BA%8F%E5%88%97#24位
example:
fg: \x1b[38;2;30;144;255mMESSAGE\x1b[0m bg: \x1b[48;2;30;144;255mMESSAGE\x1b[0m both: \x1b[38;2;233;90;203;48;2;30;144;255mMESSAGE\x1b[0m
const ( AsFg uint8 = iota AsBg )
mark color is fg or bg.
const ( // MatchExpr regex to match color tags // // Notice: golang 不支持反向引用. 即不支持使用 \1 引用第一个匹配 ([a-z=;]+) // MatchExpr = `<([a-z=;]+)>(.*?)<\/\1>` // 所以调整一下 统一使用 `</>` 来结束标签,例如 "<info>some text</>" // // allow custom attrs, eg: "<fg=white;bg=blue;op=bold>content</>" // (?s:...) s - 让 "." 匹配换行 MatchExpr = `<([0-9a-zA-Z_=,;]+)>(?s:(.*?))<\/>` // AttrExpr regex to match custom color attributes // eg: "<fg=white;bg=blue;op=bold>content</>" AttrExpr = `(fg|bg|op)[\s]*=[\s]*([0-9a-zA-Z,]+);?` // StripExpr regex used for removing color tags // StripExpr = `<[\/]?[a-zA-Z=;]+>` // 随着上面的做一些调整 StripExpr = `<[\/]?[0-9a-zA-Z_=,;]*>` )
output colored text like use html tag. (not support windows cmd)
const ( LevelNo = terminfo.ColorLevelNone // not support color. Level16 = terminfo.ColorLevelBasic // basic - 3/4 bit color supported Level256 = terminfo.ColorLevelHundreds // hundreds - 8-bit color supported LevelRgb = terminfo.ColorLevelMillions // millions - (24 bit)true color supported )
terminal color available level alias of the terminfo.ColorLevel*
const CodeExpr = `\033\[[\d;?]+m`
CodeExpr regex to clear color codes eg "\033[1;36mText\x1b[0m"
Variables ¶
var ( // Enable switch color render and display // // NOTICE: // if ENV: NO_COLOR is not empty, will disable color render. Enable = os.Getenv("NO_COLOR") == "" // RenderTag render HTML tag on call color.Xprint, color.PrintX RenderTag = true )
var ( // Info color style Info = &Theme{"info", Style{OpReset, FgGreen}} // Note color style Note = &Theme{"note", Style{OpBold, FgLightCyan}} // Warn color style Warn = &Theme{"warning", Style{OpBold, FgYellow}} // Light color style Light = &Theme{"light", Style{FgLightWhite, BgBlack}} // Error color style Error = &Theme{"error", Style{FgLightWhite, BgRed}} // Danger color style Danger = &Theme{"danger", Style{OpBold, FgRed}} // Debug color style Debug = &Theme{"debug", Style{OpReset, FgCyan}} // Notice color style Notice = &Theme{"notice", Style{OpBold, FgCyan}} // Comment color style Comment = &Theme{"comment", Style{OpReset, FgYellow}} // Success color style Success = &Theme{"success", Style{OpBold, FgGreen}} // Primary color style Primary = &Theme{"primary", Style{OpReset, FgBlue}} // Question color style Question = &Theme{"question", Style{OpReset, FgMagenta}} // Secondary color style Secondary = &Theme{"secondary", Style{FgDarkGray}} )
internal themes(like bootstrap style) Usage:
color.Info.Print("message") color.Info.Printf("a %s message", "test") color.Warn.Println("message") color.Error.Println("message")
var AllOptions = map[string]Color{ "reset": OpReset, "bold": OpBold, "fuzzy": OpFuzzy, "italic": OpItalic, "underscore": OpUnderscore, "blink": OpBlink, "reverse": OpReverse, "concealed": OpConcealed, }
AllOptions color options map
var BgColors = map[string]Color{ "black": BgBlack, "red": BgRed, "green": BgGreen, "yellow": BgYellow, "blue": BgBlue, "magenta": BgMagenta, "cyan": BgCyan, "white": BgWhite, "default": BgDefault, }
BgColors background colors map
var ExBgColors = map[string]Color{ "darkGray": BgDarkGray, "lightRed": BgLightRed, "lightGreen": BgLightGreen, "lightYellow": BgLightYellow, "lightBlue": BgLightBlue, "lightMagenta": BgLightMagenta, "lightCyan": BgLightCyan, "lightWhite": BgLightWhite, }
ExBgColors extra background colors map
var ExFgColors = map[string]Color{ "darkGray": FgDarkGray, "lightRed": FgLightRed, "lightGreen": FgLightGreen, "lightYellow": FgLightYellow, "lightBlue": FgLightBlue, "lightMagenta": FgLightMagenta, "lightCyan": FgLightCyan, "lightWhite": FgLightWhite, }
ExFgColors extra foreground colors map
var FgColors = map[string]Color{ "black": FgBlack, "red": FgRed, "green": FgGreen, "yellow": FgYellow, "blue": FgBlue, "magenta": FgMagenta, "cyan": FgCyan, "white": FgWhite, "default": FgDefault, }
FgColors foreground colors map
var Options = AllOptions
Options color options map
Deprecated: please use AllOptions instead.
var Styles = map[string]Style{ "info": {OpReset, FgGreen}, "note": {OpBold, FgLightCyan}, "light": {FgLightWhite, BgRed}, "error": {FgLightWhite, BgRed}, "danger": {OpBold, FgRed}, "notice": {OpBold, FgCyan}, "success": {OpBold, FgGreen}, "comment": {OpReset, FgMagenta}, "primary": {OpReset, FgBlue}, "warning": {OpBold, FgYellow}, "question": {OpReset, FgMagenta}, "secondary": {FgDarkGray}, }
Styles internal defined styles, like bootstrap styles. Usage:
color.Styles["info"].Println("message")
var Themes = map[string]*Theme{ "info": Info, "note": Note, "light": Light, "error": Error, "debug": Debug, "danger": Danger, "notice": Notice, "success": Success, "comment": Comment, "primary": Primary, "warning": Warn, "question": Question, "secondary": Secondary, }
Themes internal defined themes. Usage:
color.Themes["info"].Println("message")
Functions ¶
func BasicToHex ¶ added in v1.5.4
BasicToHex convert basic color to hex string.
func C256ToRgbV1 ¶ added in v1.3.4
C256ToRgbV1 convert an 256 color code to RGB numbers refer https://github.com/torvalds/linux/commit/cec5b2a97a11ade56a701e83044d0a2a984c67b4
func Colors2code ¶ added in v1.3.3
Colors2code convert colors to code. return like "32;45;3"
func ForceColor ¶ added in v1.2.8
func ForceColor() terminfo.ColorLevel
ForceColor force open color render
func ForceOpenColor ¶ added in v1.1.11
func ForceOpenColor() terminfo.ColorLevel
ForceOpenColor force open color render
func ForceSetColorLevel ¶ added in v1.4.0
func ForceSetColorLevel(level terminfo.ColorLevel) terminfo.ColorLevel
ForceSetColorLevel force open color render
func Fprintf ¶
Fprintf print format and rendered messages to writer. Notice: will ignore print error
func GetTagCode ¶ added in v1.1.2
GetTagCode get color code by tag name
func HexToRgb ¶ added in v1.2.6
HexToRgb convert hex color string to RGB numbers
Usage:
rgb := HexToRgb("ccc") // rgb: [204 204 204] rgb := HexToRgb("aabbcc") // rgb: [170 187 204] rgb := HexToRgb("#aabbcc") // rgb: [170 187 204] rgb := HexToRgb("0xad99c0") // rgb: [170 187 204]
func HslIntToRgb ¶ added in v1.5.0
HslIntToRgb Converts an HSL color value to RGB Assumes h: 0-360, s: 0-100, l: 0-100 returns r, g, and b in the set [0, 255].
Usage:
HslIntToRgb(0, 100, 50) // red HslIntToRgb(120, 100, 50) // lime HslIntToRgb(120, 100, 25) // dark green HslIntToRgb(120, 100, 75) // light green
func HslToRgb ¶ added in v1.5.0
HslToRgb Converts an HSL color value to RGB. Conversion formula adapted from http://en.wikipedia.org/wiki/HSL_color_space. Assumes h, s, and l are contained in the set [0, 1] returns r, g, and b in the set [0, 255].
Usage:
rgbVals := HslToRgb(0, 1, 0.5) // red
func HsvToRgb ¶ added in v1.5.0
HsvToRgb Converts an HSL color value to RGB. Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSV_to_RGB Assumes h: 0-360, s: 0-100, l: 0-100 returns r, g, and b in the set [0, 255].
func IsLikeInCmd
deprecated
added in
v1.2.6
func IsMSys ¶ added in v1.1.2
func IsMSys() bool
IsMSys msys(MINGW64) environment, does not necessarily support color
func IsSupport16Color ¶ added in v1.4.0
func IsSupport16Color() bool
IsSupport16Color check current console is support color.
NOTICE: The method will detect terminal info each times,
if only want get current color level, please direct call SupportColor() or TermColorLevel()
func IsSupport256Color ¶ added in v1.1.2
func IsSupport256Color() bool
IsSupport256Color render check
NOTICE: The method will detect terminal info each times,
if only want to get current color level, please direct call SupportColor() or TermColorLevel()
func IsSupportColor ¶ added in v1.1.2
func IsSupportColor() bool
IsSupportColor check current console is support color.
NOTICE: The method will detect terminal info each times,
if only want get current color level, please direct call SupportColor() or TermColorLevel()
func IsSupportRGBColor ¶ added in v1.3.8
func IsSupportRGBColor() bool
IsSupportRGBColor check. alias of the IsSupportTrueColor()
NOTICE: The method will detect terminal info each times,
if only want get current color level, please direct call SupportColor() or TermColorLevel()
func IsSupportTrueColor ¶ added in v1.2.1
func IsSupportTrueColor() bool
IsSupportTrueColor render check.
NOTICE: The method will detect terminal info each times,
if only want get current color level, please direct call SupportColor() or TermColorLevel()
ENV: "COLORTERM=truecolor" "COLORTERM=24bit"
func IsTerminal ¶ added in v1.1.11
IsTerminal returns true if the given file descriptor is a terminal.
Usage:
IsTerminal(os.Stdout.Fd())
func Lprint ¶ added in v1.2.3
Lprint passes colored messages to a log.Logger for printing. Notice: should be goroutine safe
func Magentaln ¶ added in v1.0.1
func Magentaln(a ...any)
Magentaln print message line with Magenta color
func NotRenderTag ¶ added in v1.2.8
func NotRenderTag()
NotRenderTag on call color.Xprint, color.PrintX
func ParseCodeFromAttr ¶
ParseCodeFromAttr parse color attributes.
attr format:
// VALUE please see var: FgColors, BgColors, AllOptions "fg=VALUE;bg=VALUE;op=VALUE"
16 color:
"fg=yellow" "bg=red" "op=bold,underscore" // option is allow multi value "fg=white;bg=blue;op=bold" "fg=white;op=bold,underscore"
256 color:
"fg=167" "fg=167;bg=23" "fg=167;bg=23;op=bold"
True color:
// hex "fg=fc1cac" "fg=fc1cac;bg=c2c3c4" // r,g,b "fg=23,45,214" "fg=23,45,214;bg=109,99,88"
func Render ¶
Render parse color tags, return rendered string.
Usage:
text := Render("<info>hello</> <cyan>world</>!") fmt.Println(text)
func RenderCode ¶ added in v1.1.2
RenderCode render message by color code.
Usage:
msg := RenderCode("3;32;45", "some", "message")
func RenderString ¶ added in v1.1.2
RenderString render a string with color code.
Usage:
msg := RenderString("3;32;45", "a message")
func RenderWithSpaces ¶ added in v1.1.11
RenderWithSpaces Render code with spaces. If the number of args is > 1, a space will be added between the args
func ReplaceTag ¶
ReplaceTag parse string, replace color tag and return rendered string
func ResetOptions ¶ added in v1.2.8
func ResetOptions()
ResetOptions reset all package option setting
func RgbTo256Table ¶ added in v1.3.4
RgbTo256Table mapping data
func RgbToAnsi ¶ added in v1.3.3
RgbToAnsi convert RGB-code to 16-code refer https://github.com/radareorg/radare2/blob/master/libr/cons/rgb.c#L249-L271
func RgbToHex ¶ added in v1.2.6
RgbToHex convert RGB-code to hex-code
Usage:
hex := RgbToHex([]int{170, 187, 204}) // hex: "aabbcc"
func RgbToHsl ¶ added in v1.5.0
RgbToHsl Converts an RGB color value to HSL. Conversion formula
adapted from http://en.wikipedia.org/wiki/HSL_color_space.
Assumes r, g, and b are contained in the set [0, 255] and returns h, s, and l in the set [0, 1].
func RgbToHslInt ¶ added in v1.5.0
RgbToHslInt Converts an RGB color value to HSL. Conversion formula Assumes r, g, and b are contained in the set [0, 255] and returns [h,s,l] h: 0-360, s: 0-100, l: 0-100.
func Successln ¶ added in v1.0.1
func Successln(a ...any)
Successln print message with success style
func Support256Color ¶ added in v1.4.0
func Support256Color() bool
Support256Color Whether the current environment supports 256-color output
func SupportColor ¶ added in v1.3.5
func SupportColor() bool
SupportColor Whether the current environment supports color output
func SupportTrueColor ¶ added in v1.4.0
func SupportTrueColor() bool
SupportTrueColor Whether the current environment supports (RGB)True-color output
Types ¶
type Color ¶
type Color uint8
Color Color16, 16 color value type 3(2^3=8) OR 4(2^4=16) bite color.
const ( FgBlack Color = iota + 30 FgRed FgGreen FgYellow FgBlue FgMagenta // 品红 FgCyan // 青色 FgWhite // FgDefault revert default FG FgDefault Color = 39 )
Foreground colors. basic foreground colors 30 - 37
const ( FgDarkGray Color = iota + 90 // 亮黑(灰) FgLightRed FgLightGreen FgLightYellow FgLightBlue FgLightMagenta FgLightCyan FgLightWhite // FgGray is alias of FgDarkGray FgGray Color = 90 // 亮黑(灰) )
Extra foreground color 90 - 97(非标准)
const ( BgBlack Color = iota + 40 BgRed BgGreen BgYellow // BgBrown like yellow BgBlue BgMagenta BgCyan BgWhite // BgDefault revert default BG BgDefault Color = 49 )
Background colors. basic background colors 40 - 47
const ( BgDarkGray Color = iota + 100 BgLightRed BgLightGreen BgLightYellow BgLightBlue BgLightMagenta BgLightCyan BgLightWhite // BgGray is alias of BgDarkGray BgGray Color = 100 )
Extra background color 100 - 107 (non-standard)
const ( OpReset Color = iota // 0 重置所有设置 OpBold // 1 加粗 OpFuzzy // 2 模糊(不是所有的终端仿真器都支持) OpItalic // 3 斜体(不是所有的终端仿真器都支持) OpUnderscore // 4 下划线 OpBlink // 5 闪烁 OpFastBlink // 6 快速闪烁(未广泛支持) OpReverse // 7 颠倒的 交换背景色与前景色 OpConcealed // 8 隐匿的 OpStrikethrough // 9 删除的,删除线(未广泛支持) )
Option settings. range: 0 - 9
func (Color) Darken ¶ added in v1.1.4
Darken current color. eg. 96(FgLightCyan) -> 36(FgCyan)
Usage:
cyan := LightCyan.Darken() cyan.Print("message")
func (Color) Light ¶ added in v1.1.4
Light current color. eg: 36(FgCyan) -> 96(FgLightCyan).
Usage:
lightCyan := Cyan.Light() lightCyan.Print("message")
func (Color) Print ¶
Print messages.
Usage:
color.Green.Print("message")
OR:
green := color.FgGreen.Print green("message")
func (Color) Printf ¶
Printf format and print messages.
Usage:
color.Cyan.Printf("string %s", "arg0")
func (Color) Render ¶
Render messages by color setting
Usage:
green := color.FgGreen.Render fmt.Println(green("message"))
func (Color) Renderln ¶ added in v1.1.11
Renderln messages by color setting. like Println, will add spaces for each argument
Usage:
green := color.FgGreen.Renderln fmt.Println(green("message"))
func (Color) Sprint ¶ added in v1.1.2
Sprint render messages by color setting. is alias of the Render()
func (Color) Sprintf ¶ added in v1.1.2
Sprintf format and render message.
Usage:
green := color.Green.Sprintf colored := green("message")
type Color256 ¶ added in v1.1.2
type Color256 [2]uint8
Color256 256 color (8 bit), uint8 range at 0 - 255. Support 256 color on windows CMD, PowerShell
颜色值使用10进制和16进制都可 0x98 = 152
The color consists of two uint8:
0: color value 1: color type; Fg=0, Bg=1, >1: unset value
example:
fg color: [152, 0] bg color: [152, 1]
lint warn - Name starts with package name
func (Color256) FullCode ¶ added in v1.4.2
FullCode convert to color code string with prefix. eg: "38;5;12"
type Level ¶ added in v1.3.8
type Level = terminfo.ColorLevel
Level is the color level supported by a terminal.
func DetectColorLevel ¶ added in v1.4.0
func DetectColorLevel() Level
DetectColorLevel for current env
NOTICE: The method will detect terminal info each times,
if only want to get current color level, please direct call SupportColor() or TermColorLevel()
func TermColorLevel ¶ added in v1.4.0
func TermColorLevel() Level
TermColorLevel Get the currently supported color level
type Printer ¶ added in v1.1.3
type Printer struct { // NoColor disable color. NoColor bool // Code color code string. eg "32;45;3" Code string }
Printer a generic color message printer.
Usage:
p := &Printer{Code: "32;45;3"} p.Print("message")
type PrinterFace ¶ added in v1.1.3
type PrinterFace interface { fmt.Stringer Sprint(a ...any) string Sprintf(format string, a ...any) string Print(a ...any) Printf(format string, a ...any) Println(a ...any) }
PrinterFace interface
type RGBColor ¶ added in v1.1.2
type RGBColor [4]uint8
RGBColor definition. Support RGB color on Windows CMD, PowerShell
The first to third digits represent the color value. The last digit represents the foreground(0), background(1), >1 is unset value
Usage:
// 0, 1, 2 is R,G,B. // 3rd: Fg=0, Bg=1, >1: unset value RGBColor{30,144,255, 0} RGBColor{30,144,255, 1}
func HEX ¶ added in v1.1.2
HEX create RGB color from a HEX color string.
Usage:
c := HEX("ccc") // rgb: [204 204 204] c := HEX("aabbcc") // rgb: [170 187 204] c := HEX("#aabbcc") c := HEX("0xaabbcc") c.Print("message")
func RGB ¶ added in v1.1.2
RGB color create.
Usage:
c := RGB(30,144,255) c := RGB(30,144,255, true) c.Print("message")
func RGBFromHEX ¶ added in v1.5.2
RGBFromHEX quick RGBColor from hex string, alias of HEX()
func RGBFromSlice ¶ added in v1.3.4
RGBFromSlice quick RGBColor from slice
func RGBFromString ¶ added in v1.1.3
RGBFromString create RGB color from a string. Support use color name in the {namedRgbMap}
Usage:
c := RGBFromString("170,187,204") c.Print("message") c := RGBFromString("brown") c.Print("message with color brown")
func RgbFromInt ¶ added in v1.5.0
RgbFromInt create instance from int r,g,b value
func RgbFromInts ¶ added in v1.5.0
RgbFromInts create instance from []int r,g,b value
func (RGBColor) RgbString ¶ added in v1.5.0
RgbString to color code string without prefix. eg: "204,123,56"
func (RGBColor) String ¶ added in v1.1.2
String to color code string with prefix. eg: "38;2;204;123;56"
type RGBStyle ¶ added in v1.1.2
type RGBStyle struct { // Name of the style Name string // contains filtered or unexported fields }
RGBStyle supports set foreground and background color
All are composed of 4 digits uint8, the first three digits are the color value; The last bit is different from RGBColor, here it indicates whether the value is set.
1 Has been set ^1 Not set
func HEXStyle ¶ added in v1.1.3
HEXStyle create a RGBStyle from HEX color string.
Usage:
s := HEXStyle("aabbcc", "eee") s.Print("message")
func NewRGBStyle ¶ added in v1.1.3
NewRGBStyle create a RGBStyle.
func RGBStyleFromString ¶ added in v1.1.3
RGBStyleFromString create a RGBStyle from color value string.
Usage:
s := RGBStyleFromString("170,187,204", "70,87,4") s.Print("message")
type Scheme ¶ added in v1.3.8
Scheme struct
func NewDefaultScheme ¶ added in v1.3.8
NewDefaultScheme create an defuault color Scheme
type SimplePrinter ¶ added in v1.3.8
type SimplePrinter struct{}
SimplePrinter use for quick use color print on inject to struct
func (*SimplePrinter) Errorf ¶ added in v1.3.8
func (s *SimplePrinter) Errorf(format string, a ...any)
Errorf message
func (*SimplePrinter) Errorln ¶ added in v1.3.8
func (s *SimplePrinter) Errorln(a ...any)
Errorln message
func (*SimplePrinter) Infof ¶ added in v1.3.8
func (s *SimplePrinter) Infof(format string, a ...any)
Infof message
func (*SimplePrinter) Infoln ¶ added in v1.3.8
func (s *SimplePrinter) Infoln(a ...any)
Infoln message
func (*SimplePrinter) Printf ¶ added in v1.3.8
func (s *SimplePrinter) Printf(format string, v ...any)
Printf message
func (*SimplePrinter) Println ¶ added in v1.3.8
func (s *SimplePrinter) Println(v ...any)
Println message
func (*SimplePrinter) Successf ¶ added in v1.5.3
func (s *SimplePrinter) Successf(format string, a ...any)
Successf message
func (*SimplePrinter) Successln ¶ added in v1.5.3
func (s *SimplePrinter) Successln(a ...any)
Successln message
func (*SimplePrinter) Warnf ¶ added in v1.3.8
func (s *SimplePrinter) Warnf(format string, a ...any)
Warnf message
func (*SimplePrinter) Warnln ¶ added in v1.3.8
func (s *SimplePrinter) Warnln(a ...any)
Warnln message
type Style ¶
type Style []Color
Style a 16 color style. can add: fg color, bg color, color options
Example:
color.Style{color.FgGreen}.Print("message")
func New ¶
New create a custom style
Usage:
color.New(color.FgGreen).Print("message") equals to: color.Style{color.FgGreen}.Print("message")
func (Style) Render ¶
Render colored text
Usage:
color.New(color.FgGreen).Render("text") color.New(color.FgGreen, color.BgBlack, color.OpBold).Render("text")
func (Style) Renderln ¶ added in v1.1.11
Renderln render text with newline. like Println, will add spaces for each argument
Usage:
color.New(color.FgGreen).Renderln("text", "more") color.New(color.FgGreen, color.BgBlack, color.OpBold).Render("text", "more")
type Style256 ¶ added in v1.1.2
type Style256 struct { // Name of the style Name string // contains filtered or unexported fields }
Style256 definition
前/背景色 都是由两位uint8组成, 第一位是色彩值; 第二位与 Bit8Color 不一样的是,在这里表示是否设置了值 0 未设置 !=0 已设置
func S256 ¶ added in v1.1.2
S256 create a color256 style
Usage:
s := color.S256() s := color.S256(132) // fg s := color.S256(132, 203) // fg and bg
type TagParser ¶ added in v1.4.2
type TagParser struct {
// contains filtered or unexported fields
}
TagParser struct
func (*TagParser) Parse ¶ added in v1.4.2
Parse given string, replace color tag and return rendered string
Use built in tags:
<TAG_NAME>CONTENT</> // e.g: `<info>message</>`
Custom tag attributes:
`<fg=VALUE;bg=VALUE;op=VALUES>CONTENT</>` // e.g: `<fg=167;bg=232>wel</>`
func (*TagParser) ParseByEnv ¶ added in v1.4.2
ParseByEnv parse given string. will check package setting.
type Theme ¶ added in v1.1.1
Theme definition. extends from Style