brush

package module
v0.0.0-...-0221367 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 28, 2025 License: MIT Imports: 7 Imported by: 4

README

brush logo

Language License Go Report Card Coverage Status Go Reference

Brush is a simple and light library to help you paint your terminal outputs

Heavily inspired by termenv and lipgloss

Philosopy

  • Simple and Lightway - it's a brush not a space shuttle!

"Less is exponentially better" I do believe in it and I tried to apply it on this library, no many complex data structures or even just functions/methods, no external libraries.

  • Safe - 16 != 256

When handling special char sequence is very common to commit mistakes, for this reason when you paint something you don't have a string but a Painted item with a String() string method (The same applied to Highlighted items). Another very common problem, when handling ANSI color codes, is mixing the original 16 palette with the extended (256) version. When creating a brush or painting / hightlighting something font and background colors must be of the same ColorType

  • Idiomatic - mybrush := brush.New(...)

Naming convention helps to make the library easy to use and intuitive throw the use of functions/methods like Paint, Paintln, Paintf to color stuffs that acts very similarly to the one defined on the fmt package

Usage

Paint Use the Paint function to "paint" a given string with a foreground and background color, if the background is nil then the background will be the default of your terminal.

fmt.Println("I", brush.Paint(brush.Red, nil, "love"), "go")

Create your own brush via the New function so you don't have to pass the style every time you want to paint something.

myBrush := brush.New(brush.Red, nil)
fmt.Println("I", myBrush.Paint("love"), "go")

// You can use different methods to change the style of the brush like UseFontColor, UseBgColor
myBrush.UseBgColor(brush.Black).UseFontColor(brush.Yellow) // you can chain them!

fmt.Println(myBrush.Paint("Hello"), myBrush.Swap().Paint("World"), "!") // Swap will invert font and bg color
fmt.Println(
	myBrush.UseBgTransparent().Paint("I"), // UseBgTransparent will remove the bg
	myBrush.UseDefaultColor().Paint("love"), // UseDefaultColor will reset the colors to the ones on brush declaration
	"go",
)

Use the Highlight method to color just the matching part of the string

fmt.Println(myBrush.Hightlight("I love go", regexp.MustCompile("love")))

Examples

If you need more examples, you can find more here

Colors

The library uses the ANSI Color codes in 3 different format:

I like to keep things separated for safety so when you declare a new brush or painting something be sure to use both colors (font and background) from the same ColorType.

ANSIColor

For the original ANSI (16 colors) you can simply use the already defined constants:

Black, Red, Green, Yellow, Blue, Magenta, Cyan, White
and their "bright" version:
BrightBlack, BrightRed, BrightGreen, BrightYellow, BrightBlue, BrightMagenta, BrightCyan, BrightWhite

Or use a simple interger (int8)

ExtendedANSIColor

For the extended ANSI (256 colors) you can or use one of the first 16 colors by using the method: ToExtended()

ex. font := brush.Red.ToExtended()

As for the ANSIColor scheme you use a simple interger (uint8)

For the last 24 colors (the grayscale) + black and white, you can use the the GrayScale function

White is the maximum value: myWhite := brush.GrayScale(25), you can also use the constant MaxGrayScale
Black is the minimum value: myBlack := brush.GrayScale(0)
All the other values in between represents the grayscale (the last 24 colors of the ANSI table): myGray := brush.GrayScale(16)

For the central part of the colors codes table you can compose them using their RGB values with the RGB function

You simply need to put the value or red, green and blue myBlue := brush.RGB(0, 0, 5).
Keep in mind throw that the scale goes from 0 to 5. To help with that there is a set of constant you can use:
ZeroIntensity (0), LowIntensity (95), ModerateIntensity (135), MediumIntensity (175), HightIntensity (215), MaxIntensity (255)
If you need all the possible colors with rgb values ranging from 0 to 255 then...

TrueColor

Not supported in every terminal but it allows to use all the red, green and blue values ranging from 0 to 255. For the previous colors you can use the method ToTrueColor() provided for ANSIColor and ExtendedANSIColor ColorType

ex. font := brush.Red.ToTrueColor()

For the RGB is very easy: TrueColor is a struct with the Red, Green and Blue fields (uint16), so just initialize one

ex. yellow := brush.TrueColor{255, 165, 0}

If you need to convert an hexadecimal color, you can use the ParseHex function, it accept both #RRGGBB and #RGB formats, the # is totally optional

ex. yellowPtr, err := brush.ParseHex("FFA500")

Documentation

Index

Examples

Constants

View Source
const MaxGrayScale uint8 = 25

MaxGrayScale represents the max value you can pass to the GrayScale

Variables

View Source
var (

	// DisableIfNotTTY disables Painted and Brush if it detects application is
	// not in a tty at a global level (for this library)
	DisableIfNotTTY = true

	// Disable colored output. By default it depends by DisableIfNotTTY
	Disable = DisableIfNotTTY && !isATTY
)

Functions

func PickColor

func PickColor[color ColorType](opt Optional[color], def color) color

PickColor is an utility that lets you take the color referenced on the first argument or in case it's nil the second as a default case

Example
package main

import (
	"github.com/DazFather/brush"
)

func main() {
	brush.Disable, brush.DisableIfNotTTY = false, false // probably you don't want to override these variable

	myBrush := brush.New(brush.Yellow, nil)
	reversed := brush.New(
		brush.PickColor(myBrush.Background, brush.Black),
		brush.UseColor(myBrush.Foreground),
	)

	myBrush.Println("default brush example")
	reversed.Println("reversed brush example")
}
Output:

�[33mdefault brush example
�[0m�[30;43mreversed brush example
�[0m

Types

type ANSIColor

type ANSIColor int8

ANSIColor represents a color from the first 16 colors in the ANSI table

const (
	Black ANSIColor = iota
	Red
	Green
	Yellow
	Blue
	Magenta
	Cyan
	White
	BrightBlack
	BrightRed
	BrightGreen
	BrightYellow
	BrightBlue
	BrightMagenta
	BrightCyan
	BrightWhite
)

All the different 16 different colors on the ANSIColor table

func (ANSIColor) ToExtended

func (color ANSIColor) ToExtended() ExtendedANSIColor

ToExtended transforms an ANSIColor to an ExtendedANSIColor representation

func (ANSIColor) ToTrueColor

func (c ANSIColor) ToTrueColor() (tc TrueColor)

ToTrueColor transforms an ANSIColor to a standard TrueColor representation. Be aware that the actual color might be different from the original, because the visible color might be different from the one of your terminal.

type Brush

type Brush[color ColorType] struct {
	Foreground color
	Background Optional[color]
	Disable    bool
	// contains filtered or unexported fields
}

Brush lets you paint some strings and change styling more freely

func New

func New[color ColorType](font color, background Optional[color]) Brush[color]

New creates a new Brush with the given default colors of a specified set. Disable will be set as false unless it detects application is not in a tty and DisableIfNotTTY is true

func (Brush[color]) Embed

func (b Brush[color]) Embed(values ...any) Highlighted

Embed lets create an Highlighted item by joining the given values. Painted items will maintain their styling. Highlighted items will maintain their styling only on the subset that contains info about it, for other values (and the subset of Highlighted items that do not specify info) the brush style will be enforced

Example
package main

import (
	"fmt"
	"regexp"

	"github.com/DazFather/brush"
)

func main() {
	brush.Disable, brush.DisableIfNotTTY = false, false // probably you don't want to override these variables

	var (
		green  = brush.New(brush.Green, nil)
		blue   = brush.New(brush.Blue, nil)
		yellow = brush.New(brush.Yellow, nil)
	)

	fmt.Print(blue.Embed(
		yellow.Highlight("Sun is yellow\n", regexp.MustCompile(`Sun|yellow`)),
		green.Paint("Grass"), " is ", green.Paint("green"),
		"\nAll the rest is blue",
	))
}
Output:

�[33mSun�[0m�[34m is �[0m�[33myellow�[0m�[34m
�[0m�[32mGrass�[0m�[34m is �[0m�[32mgreen�[0m�[34m
All the rest is blue�[0m

func (Brush[color]) Highlight

func (b Brush[color]) Highlight(s string, find *regexp.Regexp) Highlighted

Highlight only the matching part of the given string with the color of the brush

Example
package main

import (
	"fmt"
	"regexp"

	"github.com/DazFather/brush"
)

func main() {
	brush.Disable, brush.DisableIfNotTTY = false, false // probably you don't want to override these variables

	var (
		marker = brush.New(brush.Black, brush.UseColor(brush.Yellow))
		text   = "bla bla something intresting, something intresting blah bla"
		res    = marker.Highlight(text, regexp.MustCompile("something intresting"))
	)

	fmt.Print(res)
}
Output:

bla bla �[30;43msomething intresting�[0m, �[30;43msomething intresting�[0m blah bla

func (Brush[color]) HighlightFunc

func (b Brush[color]) HighlightFunc(s string, find *regexp.Regexp, repl func(string) string) Highlighted

HighlightFunc is like Highlight but allows you to replace the part that will match the returned string of the repl function will then be highlighted using brush styling

Example
package main

import (
	"fmt"
	"regexp"
	"strings"

	"github.com/DazFather/brush"
)

func main() {
	brush.Disable, brush.DisableIfNotTTY = false, false // probably you don't want to override these variables

	marker := brush.New(brush.Black, brush.UseColor(brush.BrightYellow))

	fmt.Print(marker.HighlightFunc(
		"this is uppercase and yellow",
		regexp.MustCompile("uppercase"),
		strings.ToUpper,
	))
}
Output:

this is �[30;103mUPPERCASE�[0m and yellow

func (Brush[color]) Paint

func (b Brush[color]) Paint(values ...any) Painted

Paint some values (joined without separator) with the current brush colors. If a Painted and/or an Highlighted item is given, they will lose their previous style and the current styling of the brush will be enforced

Example
package main

import (
	"fmt"

	"github.com/DazFather/brush"
)

func main() {
	brush.Disable, brush.DisableIfNotTTY = false, false // probably you don't want to override these variables

	myBrush := brush.New(brush.Red, nil)
	fmt.Println("I", myBrush.Paint("love"), "go")

}
Output:

I �[31mlove�[0m go

func (Brush[color]) Paintf

func (b Brush[color]) Paintf(model string, values ...any) Painted

Paintf is like Paint but similarly to fmt.Sprintf it allows to use a model with some placeholders that will be replaced by the values. If a Painted and/or an Highlighted item is given, they will lose their previous style and the current styling of the brush will be enforced

Example
package main

import (
	"fmt"

	"github.com/DazFather/brush"
)

func main() {
	brush.Disable, brush.DisableIfNotTTY = false, false // probably you don't want to override these variables

	myBrush := brush.New(brush.White, brush.UseColor(brush.Blue))
	fmt.Println("The sky is", myBrush.Paintf("%s", "blue"))

}
Output:

The sky is �[37;44mblue�[0m

func (Brush[color]) Paintln

func (b Brush[color]) Paintln(values ...any) Painted

Paintln like Paint but similarly to fmt.Sprintln it separates values with " " and it adds a "\n" at the end of all. If a Painted and/or an Highlighted item is given, they will lose their previous style and the current styling of the brush will be enforced

Example
package main

import (
	"fmt"

	"github.com/DazFather/brush"
)

func main() {
	brush.Disable, brush.DisableIfNotTTY = false, false // probably you don't want to override these variables

	myBrush := brush.New(brush.Black, brush.UseColor(brush.White))
	fmt.Print(myBrush.Paintln("Hello", "World", "!"))

}
Output:

�[30;47mHello World !
�[0m

func (Brush[color]) Print

func (b Brush[color]) Print(values ...any)

Print shows on stdout some values (joined without separator) enforcing the current font and background color of the brush

Example
package main

import (
	"github.com/DazFather/brush"
)

func main() {
	brush.Disable, brush.DisableIfNotTTY = false, false // probably you don't want to override these variables

	myBrush := brush.New(brush.Black, brush.UseColor(brush.White))

	myBrush.Print("Hello", " World")
}
Output:

�[30;47mHello World�[0m

func (Brush[color]) Printf

func (b Brush[color]) Printf(model string, values ...any)

Printf shows on stdout some values by replacing the placeholders in the given model with the corresponding values enforcing the current font and background color of the brush

Example
package main

import (
	"github.com/DazFather/brush"
)

func main() {
	brush.Disable, brush.DisableIfNotTTY = false, false // probably you don't want to override these variables

	myBrush := brush.New(brush.Black, brush.UseColor(brush.White))

	myBrush.Printf("%s %s", "Hello", "World")
}
Output:

�[30;47mHello World�[0m

func (Brush[color]) Println

func (b Brush[color]) Println(values ...any)

Println shows on stdout some values (joined with " ", and adding a "\n" at the end) enforcing the current font and background color of the brush

Example
package main

import (
	"github.com/DazFather/brush"
)

func main() {
	brush.Disable, brush.DisableIfNotTTY = false, false // probably you don't want to override these variables

	myBrush := brush.New(brush.Black, brush.UseColor(brush.White))

	myBrush.Println("Hello", "World")
}
Output:

�[30;47mHello World
�[0m

func (*Brush[color]) Swap

func (b *Brush[color]) Swap() *Brush[color]

Swap overrides font and background color by inverting them and gives back the same (now modified) brush If background was unset then the default foreground color will be used as foreground

Example
package main

import (
	"github.com/DazFather/brush"
)

func main() {
	brush.Disable, brush.DisableIfNotTTY = false, false // probably you don't want to override these variables

	myBrush := brush.New(brush.Black, brush.UseColor(brush.White))

	myBrush.Println("something")
	myBrush.Swap().Println("something else")
}
Output:

�[30;47msomething
�[0m�[37;40msomething else
�[0m

func (*Brush[color]) UseBgColor

func (b *Brush[color]) UseBgColor(c color) *Brush[color]

UseFontColor overrides the background color and gives back the same (now modified) brush

Example
package main

import (
	"github.com/DazFather/brush"
)

func main() {
	brush.Disable, brush.DisableIfNotTTY = false, false // probably you don't want to override these variables

	myBrush := brush.New(brush.Black, brush.UseColor(brush.White))

	myBrush.Println("something")
	myBrush.UseBgColor(brush.Cyan).Println("something else")
}
Output:

�[30;47msomething
�[0m�[30;46msomething else
�[0m

func (*Brush[color]) UseBgTransparent

func (b *Brush[color]) UseBgTransparent() *Brush[color]

UseFontColor overrides the background color removing it and gives back the same (now modified) brush

Example
package main

import (
	"github.com/DazFather/brush"
)

func main() {
	brush.Disable, brush.DisableIfNotTTY = false, false // probably you don't want to override these variables

	myBrush := brush.New(brush.Black, brush.UseColor(brush.White))

	myBrush.Println("something")
	myBrush.UseBgTransparent().Println("something else")
}
Output:

�[30;47msomething
�[0m�[30msomething else
�[0m

func (*Brush[color]) UseDefaultColor

func (b *Brush[color]) UseDefaultColor() *Brush[color]

UseDefaultColor overrides font and background color by using the default values and gives back the same (now modified) brush

func (*Brush[color]) UseFontColor

func (b *Brush[color]) UseFontColor(c color) *Brush[color]

UseFontColor overrides the font color and gives back the same (now modified) brush

Example
package main

import (
	"github.com/DazFather/brush"
)

func main() {
	brush.Disable, brush.DisableIfNotTTY = false, false // probably you don't want to override these variables

	myBrush := brush.New(brush.Black, brush.UseColor(brush.White))

	myBrush.Println("something")
	myBrush.UseFontColor(brush.Blue).Println("something else")
}
Output:

�[30;47msomething
�[0m�[34;47msomething else
�[0m

type ColorIntensity

type ColorIntensity uint8

ColorIntensity rapresent a level on the scale of color color intensity from 0 to 5

const (
	ZeroIntensity     ColorIntensity = iota // Intensity lv. 0 = 0
	LowIntensity                            // Intensity lv. 1 = 95
	ModerateIntensity                       // Intensity lv. 2 = 135
	MediumIntensity                         // Intensity lv. 3 = 175
	HightIntensity                          // Intensity lv. 4 = 215
	MaxIntensity                            // Intensity lv. 5 = 255
)

type ColorType

type ColorType interface {
	ANSIColor | ExtendedANSIColor | TrueColor
	// contains filtered or unexported methods
}

ColorType represents a color from any set

type ExtendedANSIColor

type ExtendedANSIColor uint8

ExtendedANSIColor represents a color from the extended ANSI table (256 colors)

func GrayScale

func GrayScale(grayScale uint8) ExtendedANSIColor

GrayScale pick a color from the ExtendedANSIColor table on the grayscale (1-24) plus the values of Black (0) and White (25)

Example
package main

import (
	"github.com/DazFather/brush"
)

func main() {
	brush.Disable, brush.DisableIfNotTTY = false, false // probably you don't want to override these variables

	var (
		white     = brush.GrayScale(brush.MaxGrayScale)
		lightGray = brush.GrayScale(brush.MaxGrayScale - 5)
		gray      = brush.GrayScale(brush.MaxGrayScale / 2)
		black     = brush.GrayScale(0)

		myBrush = brush.New(brush.Black.ToExtended(), nil)
	)

	myBrush.UseBgColor(white).Println("Sunny")
	myBrush.UseBgColor(lightGray).Println("Cloudy")
	myBrush.UseBgColor(gray).Println("Rainy")
	myBrush.UseBgColor(black).Println("WTF")

}
Output:

�[38;5;0;48;5;15mSunny
�[0m�[38;5;0;48;5;251mCloudy
�[0m�[38;5;0;48;5;243mRainy
�[0m�[38;5;0;48;5;0mWTF
�[0m

func RGB

func RGB(red, green, blue ColorIntensity) ExtendedANSIColor

RGB picks a color from the ExtendedANSIColor table by mixing for each one of the primary colors, different levels of intensity from 0 to 5.

If you want all range from 0 to 255 use TrueColor (might not be supported in your terminal)

A set of contant is declared on the package as helper, in order: ZeroIntensity, LowIntensity, ModerateIntensity, MediumIntensity, HightIntensity, MaxIntensity

Example
package main

import (
	"fmt"

	"github.com/DazFather/brush"
)

func main() {
	brush.Disable, brush.DisableIfNotTTY = false, false // probably you don't want to override these variables

	pink := brush.RGB(
		brush.MaxIntensity,
		brush.MediumIntensity,
		brush.HightIntensity,
	)

	fmt.Println(brush.Paint(0, &pink, "Flamingo"))
}
Output:

�[38;5;0;48;5;218mFlamingo�[0m

func (ExtendedANSIColor) ToTrueColor

func (c ExtendedANSIColor) ToTrueColor() TrueColor

ToTrueColor transforms an ExtendedANSIColor to a TrueColor representation

type Highlighted

type Highlighted struct {
	// contains filtered or unexported fields
}

Highlighted represents a string that contains informations about the foreground and background color output of some parts of it. the styling can also be different for different subset of the content

func Join

func Join(values ...any) Highlighted

Join different values together into a single item maintaining all styling

Example
package main

import (
	"fmt"

	"github.com/DazFather/brush"
)

func main() {
	brush.Disable, brush.DisableIfNotTTY = false, false // probably you don't want to override these variables

	fmt.Print(brush.Join(
		brush.Paint(brush.Red, nil, "Roses are red"),
		",\n",
		brush.Paint(brush.Blue, nil, "Violets are blue"),
		",\nSugar is sweet,\nAnd so are you.",
	))
}
Output:

�[31mRoses are red�[0m,
�[34mViolets are blue�[0m,
Sugar is sweet,
And so are you.

func (*Highlighted) Append

func (h *Highlighted) Append(values ...any) *Highlighted

Append lets you add some items at the end of the Highlighted content

Example
package main

import (
	"fmt"
	"regexp"

	"github.com/DazFather/brush"
)

func main() {
	brush.Disable, brush.DisableIfNotTTY = false, false // probably you don't want to override these variables

	var (
		marker = brush.New(brush.Black, brush.UseColor(brush.Yellow))
		h      = marker.Highlight("Hello world!", regexp.MustCompile("Hello"))
	)

	fmt.Print(h.Append(" ", marker.Paint("Hi"), " everyone!"))
}
Output:

�[30;43mHello�[0m world! �[30;43mHi�[0m everyone!

func (Highlighted) String

func (h Highlighted) String() string

String evaluates the content by applying the different styling where specified

type Optional

type Optional[color ColorType] *color

Optional represents an optional color

func UseColor

func UseColor[color ColorType](c color) Optional[color]

UseColor is an utility that lets you transform a color in an Optional this can be especially useful on the New or Paint function when selecting a background

Example
package main

import (
	"fmt"

	"github.com/DazFather/brush"
)

func main() {
	brush.Disable, brush.DisableIfNotTTY = false, false // probably you don't want to override these variable

	selectedBg := brush.UseColor(brush.Magenta)

	fmt.Println(brush.Paint(brush.BrightMagenta, selectedBg, "Magenta"), "is cool")
}
Output:

�[95;45mMagenta�[0m is cool

type Painted

type Painted struct {
	// contains filtered or unexported fields
}

Painted represents a string that contains information about it's foreground and background color output

func Paint

func Paint[color ColorType](font color, background Optional[color], values ...any) Painted

Paint some values (joined without separator) with the specified font and background color. If a Painted and/or an Highlighted item is given, they will lose their previous style and provided font and background colors will be enforced

Example
package main

import (
	"fmt"

	"github.com/DazFather/brush"
)

func main() {
	brush.Disable, brush.DisableIfNotTTY = false, false // probably you don't want to override these variables

	fmt.Println("I", brush.Paint(brush.Red, nil, "love"), "go")

}
Output:

I �[31mlove�[0m go

func Paintf

func Paintf[color ColorType](font color, background Optional[color], model string, values ...any) Painted

Paintf is like Paint but similarly to fmt.Sprintf it allows to use a model with some placeholders that will be replaced by the values. If a Painted and/or an Highlighted item is given, they will lose their previous style and provided font and background colors will be enforced

Example
package main

import (
	"fmt"

	"github.com/DazFather/brush"
)

func main() {
	brush.Disable, brush.DisableIfNotTTY = false, false // probably you don't want to override these variables

	blue := brush.Paintf(brush.White, brush.UseColor(brush.Blue), "%s", "blue")
	fmt.Println("The sky is", blue)

}
Output:

The sky is �[37;44mblue�[0m

func Paintln

func Paintln[color ColorType](font color, background Optional[color], values ...any) Painted

Paintln is like Paint but similarly to fmt.Sprintln it separates values with " " and it adds a "\n" at the end of all. If a Painted and/or an Highlighted item is given, they will lose their previous style and provided font and background colors will be enforced

Example
package main

import (
	"fmt"

	"github.com/DazFather/brush"
)

func main() {
	brush.Disable, brush.DisableIfNotTTY = false, false // probably you don't want to override these variables

	painted := brush.Paintln(brush.Black, brush.UseColor(brush.White),
		"Hello", "World", "!",
	)
	fmt.Print(painted)

}
Output:

�[30;47mHello World !
�[0m

func (*Painted) Append

func (p *Painted) Append(s string) *Painted

Append a string at the end of the content of the painted item Warning: Do not use string containing styling

Example
package main

import (
	"fmt"

	"github.com/DazFather/brush"
)

func main() {
	brush.Disable, brush.DisableIfNotTTY = false, false // probably you don't want to override these variables

	banans := brush.Paint(brush.Yellow, nil, "banana")
	fmt.Println(banans.Append("s"))

}
Output:

�[33mbananas�[0m

func (*Painted) Prepend

func (p *Painted) Prepend(s string) *Painted

Prepend a string at the start of the content of the painted item Warning: Do not use string containing styling

Example

Prepend a string at the start of the content of the painted item Warning: Do not use string containing styling

package main

import (
	"fmt"

	"github.com/DazFather/brush"
)

func main() {
	brush.Disable, brush.DisableIfNotTTY = false, false // probably you don't want to override these variables

	banana := brush.Paint(brush.Yellow, nil, "banana")
	fmt.Println(banana.Prepend("yellow "))

}
Output:

�[33myellow banana�[0m

func (*Painted) Replace

func (p *Painted) Replace(s string) *Painted

Replace the content of the painted item with another string Is possible to use the %s to refer to embed previous content Warning: Do not use string containing styling

Example

Replace the content of the painted item with another string Is possible to use the %s to refer to embed previous content Warning: Do not use string containing styling

package main

import (
	"fmt"

	"github.com/DazFather/brush"
)

func main() {
	brush.Disable, brush.DisableIfNotTTY = false, false // probably you don't want to override these variables

	banana := brush.Paint(brush.Yellow, nil, "banana")
	fmt.Println(banana.Replace(`The name "%s", is funny`))

}
Output:

�[33mThe name "banana", is funny�[0m

func (Painted) String

func (p Painted) String() string

String gives a string that contains some special sequence that will apply styling

type TrueColor

type TrueColor struct {
	Red, Green, Blue uint8
}

TrueColor is a true RGB color representation. Be aware that not all terminal support this format

Example
package main

import (
	"github.com/DazFather/brush"
)

func main() {
	brush.Disable, brush.DisableIfNotTTY = false, false // probably you don't want to override these variable

	var (
		pinkish  = brush.TrueColor{Red: 255, Green: 82, Blue: 197}
		brownish = brush.TrueColor{155, 106, 0}
		myBrush  = brush.New(pinkish, &brownish)
	)

	myBrush.Println("HELLO WORLD")
}
Output:

�[38;2;255;82;197;48;2;155;106;0mHELLO WORLD
�[0m

func ParseHex

func ParseHex(hex string) (*TrueColor, error)

ParseHex parses a hexadecimal string representing a color and returns a TrueColor pointer representing that color and an error if the provided string cannot be parsed. The hex string can be in various formats (the "#" prefix is optional):

  • "RRGGBB": Represents the red, green, and blue components respectively in the range 00 to FF.
  • "RGB": Represents a shorthand version of "#RRGGBB" where each component is a single digit and duplicated, e.g, "ABC" is equivalent to "AABBCC"
Example
package main

import (
	"fmt"

	"github.com/DazFather/brush"
)

func main() {
	brush.Disable, brush.DisableIfNotTTY = false, false // probably you don't want to override these variable

	var (
		color *brush.TrueColor
		err   error
	)

	// Example with full RGB format (# is optional)
	if color, err = brush.ParseHex("#FFA500"); err == nil {
		fmt.Println("Yellow:", *color)
	}

	// Example with shorthand RGB format (# is optional)
	if color, err = brush.ParseHex("F00"); err == nil {
		fmt.Println("Red:", *color)
	}

	// Bad examples
	_, err = brush.ParseHex("blue")
	fmt.Println(err)
	_, err = brush.ParseHex("daz")
	fmt.Println(err)

}
Output:

Yellow: {255 165 0}
Red: {255 0 0}
Cannot parse blue color: Invalid hex length, must be 3 or 6 digits long (excluding optional prefix '#')
Cannot parse daz color: strconv.ParseUint: parsing "z": invalid syntax

Directories

Path Synopsis
examples
RGBscale command
finder command
logo command
test command
trueColor command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL