gookit/color is an command-line color library with true color support, universal API methods and Windows support.

Basic color preview:

Since v1.2.4 , 256 colors and RGB colors have also been supported to work in Windows CMD and PowerShell:

How to works on CMD and PWSH

The go type definition. (more please see https://github.com/gookit/color/blob/master/color_rgb.go )

// RGBColor definition.

//

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

// // 3th: Fg=0, Bg=1, >1: unset value

// RGBColor{30,144,255, 0}

// RGBColor{30,144,255, 1}

//

// NOTICE: now support RGB color on windows CMD, PowerShell

type RGBColor [4]uint8



// create a empty RGBColor

var emptyRGBColor = RGBColor{3: 99}



// RGB color create.

// Usage:

// c := RGB(30,144,255)

// c := RGB(30,144,255, true)

// c.Print("message")

func RGB(r, g, b uint8, isBg ...bool) RGBColor {

rgb := RGBColor{r, g, b}

if len(isBg) > 0 && isBg[0] {

rgb[3] = AsBg

}



return rgb

}

... ...

There are some print API methods of the RGBColor:

... ... // Print print message

func (c RGBColor) Print(a ...interface{}) {

doPrintV2(c.String(), fmt.Sprint(a...))

}



// Printf format and print message

func (c RGBColor) Printf(format string, a ...interface{}) {

doPrintV2(c.String(), fmt.Sprintf(format, a...))

}



// Println print message with newline

func (c RGBColor) Println(a ...interface{}) {

doPrintlnV2(c.String(), a)

} ...

The doPrintlnV2 method:

func doPrintlnV2(code string, args []interface{}) {

str := formatArgsForPrintln(args)

if isLikeInCmd {

renderColorCodeOnCmd(func() {

_, _ = fmt.Fprintln(output, RenderString(code, str))

})

} else {

_, _ = fmt.Fprintln(output, RenderString(code, str))

}

}

If it is currently in CMD or PowerShell, it will execute renderColorCodeOnCmd methods.

The core codes:

/*************************************************************

* render full color code on windows(8,16,24bit color)

*************************************************************/



// docs https://docs.microsoft.com/zh-cn/windows/console/getconsolemode#parameters

const (

// equals to docs page's ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004

EnableVirtualTerminalProcessingMode uint32 = 0x4

)



// EnableVirtualTerminalProcessing Enable virtual terminal processing

//

// ref from github.com/konsorten/go-windows-terminal-sequences

// doc https://docs.microsoft.com/zh-cn/windows/console/console-virtual-terminal-sequences#samples

//

// Usage:

// err := EnableVirtualTerminalProcessing(syscall.Stdout, true)

// // support print color text

// err = EnableVirtualTerminalProcessing(syscall.Stdout, false)

func EnableVirtualTerminalProcessing(stream syscall.Handle, enable bool) error {

var mode uint32

// Check if it is currently in the terminal

err := syscall.GetConsoleMode(syscall.Stdout, &mode)

if err != nil {

return err

}



if enable {

mode |= EnableVirtualTerminalProcessingMode

} else {

mode &^= EnableVirtualTerminalProcessingMode

}



ret, _, err := procSetConsoleMode.Call(uintptr(unsafe.Pointer(stream)), uintptr(mode))

if ret == 0 {

return err

}



return nil

}



// renderColorCodeOnCmd enable cmd color render.

func renderColorCodeOnCmd(fn func()) {

err := EnableVirtualTerminalProcessing(syscall.Stdout, true)

// if is not in terminal, will clear color tag.

if err != nil {

// panic(err)

fn()

return

}



// force open color render

old := ForceOpenColor()

fn()

// revert color setting

isSupportColor = old



err = EnableVirtualTerminalProcessing(syscall.Stdout, false)

if err != nil {

panic(err)

}

}

More usage

examples file at https://github.com/gookit/color/blob/master/_examples/rgbcolor.go