Golang下的命令行色彩使用库, 拥有丰富的色彩(16/256/True)渲染输出,通用的API方法,兼容Windows系统
基本颜色预览:

现在,256色和RGB色彩也已经支持windows CMD和PowerShell中工作:

cmd.exev1.2.4 起 256色(8bit),RGB色彩(24bit)均支持Windows CMD和PowerShell终端HEX HSL 等为RGB色彩Print Printf Println Sprint Sprintfthis an <green>message</> <fg=red;bg=blue>text</> 标签内部文本将会渲染对应色彩Bold Black White Gray Red Green Yellow Blue Magenta CyanInfo Note Light Error Danger Notice Success Comment Primary Warning Question SecondaryNO_COLOR 来禁用色彩,或者使用 FORCE_COLOR 来强制使用色彩渲染.go get github.com/gookit/color
如下,引入当前包就可以快速的使用
package main
import (
"fmt"
"github.com/gookit/color"
)
func main() {
// 简单快速的使用,跟 fmt.Print* 类似
color.Redp("Simple to use color")
color.Redln("Simple to use color")
color.Greenp("Simple to use color\n")
color.Cyanln("Simple to use color")
color.Yellowln("Simple to use color")
// 简单快速的使用,跟 fmt.Print* 类似
color.Red.Println("Simple to use color")
color.Green.Print("Simple to use color\n")
color.Cyan.Printf("Simple to use %s\n", "color")
color.Yellow.Printf("Simple to use %s\n", "color")
// use like func
red := color.FgRed.Render
green := color.FgGreen.Render
fmt.Printf("%s line %s library\n", red("Command"), green("color"))
// 自定义颜色
color.New(color.FgWhite, color.BgBlack).Println("custom color style")
// 也可以:
color.Style{color.FgCyan, color.OpBold}.Println("custom color style")
// internal style:
color.Info.Println("message")
color.Warn.Println("message")
color.Error.Println("message")
// 使用内置颜色标签
color.Print("<suc>he</><comment>llo</>, <cyan>wel</><red>come</>\n")
// 自定义标签: 支持使用16色彩名称,256色彩值,rgb色彩值以及hex色彩值
color.Println("<fg=11aa23>he</><bg=120,35,156>llo</>, <fg=167;bg=232>wel</><fg=red>come</>")
// apply a style tag
color.Tag("info").Println("info style text")
// prompt message
color.Info.Prompt("prompt style message")
color.Warn.Prompt("prompt style message")
// tips message
color.Info.Tips("tips style message")
color.Warn.Tips("tips style message")
}
运行 demo:
go run ./_examples/demo.go

提供通用的API方法:Print Printf Println Sprint Sprintf
支持在windows
cmd.exepowerShell等终端使用
color.Bold.Println("bold message")
color.Black.Println("bold message")
color.White.Println("bold message")
// ...
// Only use foreground color
color.FgCyan.Printf("Simple to use %s\n", "color")
// Only use background color
color.BgRed.Printf("Simple to use %s\n", "color")
运行demo:
go run ./_examples/color_16.go

// 仅设置前景色
color.FgCyan.Printf("Simple to use %s\n", "color")
// 仅设置背景色
color.BgRed.Printf("Simple to use %s\n", "color")
// 完全自定义: 前景色 背景色 选项
style := color.New(color.FgWhite, color.BgBlack, color.OpBold)
style.Println("custom color style")
// 也可以:
color.Style{color.FgCyan, color.OpBold}.Println("custom color style")
直接设置控制台属性:
// 设置console颜色
color.Set(color.FgCyan)
// 输出信息
fmt.Print("message")
// 重置console颜色
color.Reset()
当然,color已经内置丰富的色彩风格支持
提供通用的API方法:Print Printf Println Sprint Sprintf
支持在windows
cmd.exepowerShell等终端使用
基础使用:
// print message
color.Info.Println("Info message")
color.Note.Println("Note message")
color.Notice.Println("Notice message")
// ...
Run demo: go run ./_examples/theme_basic.go

简约提示风格
color.Info.Tips("Info tips message")
color.Notice.Tips("Notice tips message")
color.Error.Tips("Error tips message")
// ...
Run demo: go run ./_examples/theme_tips.go

着重提示风格
color.Info.Prompt("Info prompt message")
color.Error.Prompt("Error prompt message")
color.Danger.Prompt("Danger prompt message")
Run demo: go run ./_examples/theme_prompt.go

强调提示风格
color.Warn.Block("Warn block message")
color.Debug.Block("Debug block message")
color.Question.Block("Question block message")
Run demo: go run ./_examples/theme_block.go

256色彩在
v1.2.4后支持Windows CMD,PowerShell 环境
color.C256(val uint8, isBg ...bool) Color256c := color.C256(132) // fg color
c.Println("message")
c.Printf("format %s", "message")
c := color.C256(132, true) // bg color
c.Println("message")
c.Printf("format %s", "message")
可同时设置前景和背景色
color.S256(fgAndBg ...uint8) *Style256s := color.S256(32, 203)
s.Println("message")
s.Printf("format %s", "message")
可以同时添加选项设置:
s := color.S256(32, 203)
s.SetOpts(color.Opts{color.OpBold})
s.Println("style with options")
s.Printf("style with %s\n", "options")
运行 demo:
go run ./_examples/color_256.go

RGB色彩在
v1.2.4后支持 WindowsCMD,PowerShell环境
效果预览:
运行 demo:
Run demo: go run ./_examples/color_rgb.go

代码示例:
color.RGB(30, 144, 255).Println("message. use RGB number")
color.HEX("#1976D2").Println("blue-darken")
color.HEX("#D50000", true).Println("red-accent. use HEX style")
color.RGBStyleFromString("213,0,0").Println("red-accent. use RGB number")
color.HEXStyle("eee", "D50000").Println("deep-purple color")
color.RGB(r, g, b uint8, isBg ...bool) RGBColorc := color.RGB(30,144,255) // fg color
c.Println("message")
c.Printf("format %s", "message")
c := color.RGB(30,144,255, true) // bg color
c.Println("message")
c.Printf("format %s", "message")
color.HEX(hex string, isBg ...bool) RGBColor 从16进制颜色创建c := color.HEX("ccc") // 也可以写为: "cccccc" "#cccccc"
c.Println("message")
c.Printf("format %s", "message")
c = color.HEX("aabbcc", true) // as bg color
c.Println("message")
c.Printf("format %s", "message")
TIP: 可同时设置前景和背景色
color.NewRGBStyle(fg RGBColor, bg ...RGBColor) *RGBStyles := color.NewRGBStyle(RGB(20, 144, 234), RGB(234, 78, 23))
s.Println("message")
s.Printf("format %s", "message")
color.HEXStyle(fg string, bg ...string) *RGBStyle 从16进制颜色创建s := color.HEXStyle("11aa23", "eee")
s.Println("message")
s.Printf("format %s", "message")
s := color.HEXStyle("11aa23", "eee")
s.SetOpts(color.Opts{color.OpBold})
s.Println("style with options")
s.Printf("style with %s\n", "options")
Print,Printf,Println 等方法支持自动解析并渲染 HTML 风格的颜色标签
支持 在windows
cmd.exePowerShell使用
简单示例:
text := `
<mga1>gookit/color:</>
A <green>command-line</>
<cyan>color library</> with <fg=167;bg=232>256-color</>
and <fg=11aa23;op=bold>True-color</> support,
<fg=mga;op=i>universal API</> methods
and <cyan>Windows</> support.
`
color.Print(text)
输出效果, 示例代码请看 _examples/demo_tag.go:

颜色标签格式:
<TAG_NAME>CONTENT</> e.g: <info>message</><fg=VALUE;bg=VALUE;op=VALUES>CONTENT</> e.g: <fg=167;bg=232>wel</>使用内置的颜色标签,可以非常方便简单的构建自己需要的任何格式
同时支持自定义颜色属性: 支持使用16色彩名称,256色彩值,rgb色彩值以及hex色彩值
// 使用内置的 color tag
color.Print("<suc>he</><comment>llo</>, <cyan>wel</><red>come</>")
color.Println("<suc>hello</>")
color.Println("<error>hello</>")
color.Println("<warning>hello</>")
// 自定义颜色属性
color.Print("<fg=yellow;bg=black;op=underscore;>hello, welcome</>\n")
// 自定义颜色属性: 支持使用16色彩名称,256色彩值,rgb色彩值以及hex色彩值
color.Println("<fg=11aa23>he</><bg=120,35,156>llo</>, <fg=167;bg=232>wel</><fg=red>come</>")
标签属性格式:
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"
tag attributes parse please see
func ParseCodeFromAttr()
内置标签请参见变量 colorTags 定义, 源文件 color_tag.go
// use style tag
color.Print("<suc>he</><comment>llo</>, <cyan>wel</><red>come</>")
color.Println("<suc>hello</>")
color.Println("<error>hello</>")
运行 demo:
go run ./_examples/color_tag.go

使用 color.Tag 包装标签:
可以使用通用的输出API方法,给后面输出的文本信息加上给定的颜色风格标签
// set a style tag
color.Tag("info").Print("info style text")
color.Tag("info").Printf("%s style text", "info")
color.Tag("info").Println("info style text")
支持 Rgb, 256, 16 色彩之间的互相转换 Rgb <=> 256 <=> 16
basic := color.Red
basic.Println("basic color")
c256 := color.Red.C256()
c256.Println("256 color")
c256.C16().Println("basic color")
rgb := color.Red.RGB()
rgb.Println("rgb color")
rgb.C256().Println("256 color")
color 内置了许多颜色转换工具方法
func Basic2hex(val uint8) string
func Bg2Fg(val uint8) uint8
func Fg2Bg(val uint8) uint8
func C256ToRgb(val uint8) (rgb []uint8)
func C256ToRgbV1(val uint8) (rgb []uint8)
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 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
转换为 RGBColor:
func RGBFromSlice(rgb []uint8, isBg ...bool) RGBColorfunc RGBFromString(rgb string, isBg ...bool) RGBColorfunc HEX(hex string, isBg ...bool) RGBColorfunc HSL(h, s, l float64, isBg ...bool) RGBColorfunc HSLInt(h, s, l int, isBg ...bool) RGBColor一些有用的工具方法参考
Disable() 禁用颜色渲染输出SetOutput(io.Writer) 自定义设置渲染后的彩色文本输出位置ForceOpenColor() 强制开启颜色渲染ClearCode(str string) string Use for clear color codesColors2code(colors ...Color) string Convert colors to code. return like "32;45;3"ClearTag(s string) string clear all color html-tag for a stringIsConsole(w io.Writer) Determine whether w is one of stderr, stdout, stdincolor 会自动检查当前环境支持的颜色级别
// Level is the color level supported by a terminal.
type Level = terminfo.ColorLevel
// terminal color available level alias of the terminfo.ColorLevel*
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
)
func SupportColor() bool 当前环境是否支持色彩输出func Support256Color() bool 当前环境是否支持256色彩输出func SupportTrueColor() bool 当前环境是否支持(RGB)True色彩输出func TermColorLevel() Level 获取当前支持的颜色级别看看这些使用了 https://github.com/gookit/color 的项目:
$ claude mcp add color \
-- python -m otcore.mcp_server <graph>