Documentation ¶
Overview ¶
Package rgbterm colorizes bytes and strings using RGB colors, for a full range of pretty terminal strings.
Beyond the traditional boring 16 colors of your terminal lie an extended set of 256 pretty colors waiting to be used. However, they are weirdly encoded; simply asking for an RGB color is much more convenient!
It's easy to use, pick an RGB code and just use it!
var r, g, b uint8 // pick a color r, g, b = 252, 255, 43 // choose a word word := "=)" // colorize it! coloredWord := rgbterm.String(word, r, g, b) fmt.Println("Oh!", coloredWord, "hello!")
Alternately, use ColorOut or one of the Interpret functions to output a string with color escape codes:
fmt.Fprintln(rgbterm.ColorOut, "Let's print some {#ff0000}red{} and {#00ff00}green{} text") fmt.Fprintln(rgbterm.ColorOut, "Let's print some {#8080ff}blue,") fmt.Fprintln(rgbterm.ColorOut, "blue, blue{} text.")
The RGB <-> HSL helpers were shamelessly taken from gorilla color, BSD 2 clauses licensed:
https://code.google.com/p/gorilla/source/browse/?r=ef489f63418265a7249b1d53bdc358b09a4a2ea0#hg%2Fcolor
Index ¶
- Variables
- func BgByte(in byte, r, g, b uint8) []byte
- func BgBytes(in []byte, r, g, b uint8) []byte
- func BgString(in string, r, g, b uint8) string
- func Bytes(in []byte, fr, fg, fb, br, bg, bb uint8) []byte
- func FgByte(in byte, r, g, b uint8) []byte
- func FgBytes(in []byte, r, g, b uint8) []byte
- func FgString(in string, r, g, b uint8) string
- func HSLtoRGB(h, s, l float64) (r, g, b uint8)
- func Interpret(r io.ByteReader, w io.Writer) error
- func InterpretStr(s string) string
- func RGBtoHSL(r, g, b uint8) (h, s, l float64)
- func String(in string, fr, fg, fb, br, bg, bb uint8) string
- type InterpretingWriter
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ColorOut is a io.Writer that writes to os.Stdout // while substituting the the color escapes in it's input. // // See Interpret for a description of color escapes. ColorOut io.Writer = NewInterpretingWriter(os.Stdout) // ColorErr is a io.Writer that writes to os.Stderr // while substituting the the color escapes in it's input. // // See Interpret for a description of color escapes. ColorErr io.Writer = NewInterpretingWriter(os.Stderr) )
var MaxEscapeCodeLen uint = 15
MaxEscapeCodeLen is the maximum number of bytes that the contents of an escape code may be. If the escape code is longer than this, it is output verbatim without being replaced. If MaxEscapeCodeLen is set to 0 escape codes may be any length (checking is not performed)
This is used to avoid a possible denial of service.
Functions ¶
func BgByte ¶
BgByte colorizes the background of the input with the terminal color that matches the closest the RGB color.
func BgBytes ¶
BgBytes colorizes the background of the input with the terminal color that matches the closest the RGB color.
Example ¶
package main import ( "fmt" "github.com/aybabtme/rgbterm" ) func main() { data := []byte(" ") h, s, l := rgbterm.RGBtoHSL(252, 255, 43) for i := 0; i < 80; i++ { h += (5.0 / 360.0) if h > 1.0 { h = 0.0 } r, g, b := rgbterm.HSLtoRGB(h, s, l) fmt.Printf("%s", rgbterm.BgBytes(data, r, g, b)) //b, g, r)) } fmt.Println() }
Output: �[48;5;190m �[0;00m�[48;5;190m �[0;00m�[48;5;154m �[0;00m�[48;5;154m �[0;00m�[48;5;154m �[0;00m�[48;5;118m �[0;00m�[48;5;118m �[0;00m�[48;5;118m �[0;00m�[48;5;82m �[0;00m�[48;5;82m �[0;00m�[48;5;82m �[0;00m�[48;5;46m �[0;00m�[48;5;47m �[0;00m�[48;5;47m �[0;00m�[48;5;47m �[0;00m�[48;5;48m �[0;00m�[48;5;48m �[0;00m�[48;5;48m �[0;00m�[48;5;49m �[0;00m�[48;5;49m �[0;00m�[48;5;50m �[0;00m�[48;5;50m �[0;00m�[48;5;50m �[0;00m�[48;5;45m �[0;00m�[48;5;45m �[0;00m�[48;5;45m �[0;00m�[48;5;39m �[0;00m�[48;5;39m �[0;00m�[48;5;39m �[0;00m�[48;5;33m �[0;00m�[48;5;33m �[0;00m�[48;5;33m �[0;00m�[48;5;27m �[0;00m�[48;5;27m �[0;00m�[48;5;27m �[0;00m�[48;5;21m �[0;00m�[48;5;57m �[0;00m�[48;5;57m �[0;00m�[48;5;57m �[0;00m�[48;5;93m �[0;00m�[48;5;93m �[0;00m�[48;5;93m �[0;00m�[48;5;129m �[0;00m�[48;5;129m �[0;00m�[48;5;165m �[0;00m�[48;5;165m �[0;00m�[48;5;165m �[0;00m�[48;5;200m �[0;00m�[48;5;200m �[0;00m�[48;5;200m �[0;00m�[48;5;199m �[0;00m�[48;5;199m �[0;00m�[48;5;199m �[0;00m�[48;5;198m �[0;00m�[48;5;198m �[0;00m�[48;5;198m �[0;00m�[48;5;197m �[0;00m�[48;5;197m �[0;00m�[48;5;197m �[0;00m�[48;5;196m �[0;00m�[48;5;202m �[0;00m�[48;5;202m �[0;00m�[48;5;202m �[0;00m�[48;5;208m �[0;00m�[48;5;208m �[0;00m�[48;5;208m �[0;00m�[48;5;214m �[0;00m�[48;5;214m �[0;00m�[48;5;214m �[0;00m�[48;5;220m �[0;00m�[48;5;220m �[0;00m�[48;5;226m �[0;00m�[48;5;190m �[0;00m�[48;5;190m �[0;00m�[48;5;154m �[0;00m�[48;5;154m �[0;00m�[48;5;154m �[0;00m�[48;5;118m �[0;00m�[48;5;118m �[0;00m�[48;5;118m �[0;00m
func BgString ¶
BgString colorizes the background of the input with the terminal color that matches the closest the RGB color.
This is simply a helper for Bytes.
func Bytes ¶
Bytes colorizes the input with the terminal color that matches the closest the RGB color.
Example ¶
package main import ( "fmt" "github.com/aybabtme/rgbterm" ) func main() { data := []byte("█") h, s, l := rgbterm.RGBtoHSL(252, 255, 43) for i := 0; i < 80; i++ { h += (5.0 / 360.0) if h > 1.0 { h = 0.0 } r, g, b := rgbterm.HSLtoRGB(h, s, l) fmt.Printf("%s", rgbterm.Bytes(data, r, g, b, 0, 0, 0)) //b, g, r)) } fmt.Println() }
Output: �[38;5;190;48;5;16m█�[0;00m�[38;5;190;48;5;16m█�[0;00m�[38;5;154;48;5;16m█�[0;00m�[38;5;154;48;5;16m█�[0;00m�[38;5;154;48;5;16m█�[0;00m�[38;5;118;48;5;16m█�[0;00m�[38;5;118;48;5;16m█�[0;00m�[38;5;118;48;5;16m█�[0;00m�[38;5;82;48;5;16m█�[0;00m�[38;5;82;48;5;16m█�[0;00m�[38;5;82;48;5;16m█�[0;00m�[38;5;46;48;5;16m█�[0;00m�[38;5;47;48;5;16m█�[0;00m�[38;5;47;48;5;16m█�[0;00m�[38;5;47;48;5;16m█�[0;00m�[38;5;48;48;5;16m█�[0;00m�[38;5;48;48;5;16m█�[0;00m�[38;5;48;48;5;16m█�[0;00m�[38;5;49;48;5;16m█�[0;00m�[38;5;49;48;5;16m█�[0;00m�[38;5;50;48;5;16m█�[0;00m�[38;5;50;48;5;16m█�[0;00m�[38;5;50;48;5;16m█�[0;00m�[38;5;45;48;5;16m█�[0;00m�[38;5;45;48;5;16m█�[0;00m�[38;5;45;48;5;16m█�[0;00m�[38;5;39;48;5;16m█�[0;00m�[38;5;39;48;5;16m█�[0;00m�[38;5;39;48;5;16m█�[0;00m�[38;5;33;48;5;16m█�[0;00m�[38;5;33;48;5;16m█�[0;00m�[38;5;33;48;5;16m█�[0;00m�[38;5;27;48;5;16m█�[0;00m�[38;5;27;48;5;16m█�[0;00m�[38;5;27;48;5;16m█�[0;00m�[38;5;21;48;5;16m█�[0;00m�[38;5;57;48;5;16m█�[0;00m�[38;5;57;48;5;16m█�[0;00m�[38;5;57;48;5;16m█�[0;00m�[38;5;93;48;5;16m█�[0;00m�[38;5;93;48;5;16m█�[0;00m�[38;5;93;48;5;16m█�[0;00m�[38;5;129;48;5;16m█�[0;00m�[38;5;129;48;5;16m█�[0;00m�[38;5;165;48;5;16m█�[0;00m�[38;5;165;48;5;16m█�[0;00m�[38;5;165;48;5;16m█�[0;00m�[38;5;200;48;5;16m█�[0;00m�[38;5;200;48;5;16m█�[0;00m�[38;5;200;48;5;16m█�[0;00m�[38;5;199;48;5;16m█�[0;00m�[38;5;199;48;5;16m█�[0;00m�[38;5;199;48;5;16m█�[0;00m�[38;5;198;48;5;16m█�[0;00m�[38;5;198;48;5;16m█�[0;00m�[38;5;198;48;5;16m█�[0;00m�[38;5;197;48;5;16m█�[0;00m�[38;5;197;48;5;16m█�[0;00m�[38;5;197;48;5;16m█�[0;00m�[38;5;196;48;5;16m█�[0;00m�[38;5;202;48;5;16m█�[0;00m�[38;5;202;48;5;16m█�[0;00m�[38;5;202;48;5;16m█�[0;00m�[38;5;208;48;5;16m█�[0;00m�[38;5;208;48;5;16m█�[0;00m�[38;5;208;48;5;16m█�[0;00m�[38;5;214;48;5;16m█�[0;00m�[38;5;214;48;5;16m█�[0;00m�[38;5;214;48;5;16m█�[0;00m�[38;5;220;48;5;16m█�[0;00m�[38;5;220;48;5;16m█�[0;00m�[38;5;226;48;5;16m█�[0;00m�[38;5;190;48;5;16m█�[0;00m�[38;5;190;48;5;16m█�[0;00m�[38;5;154;48;5;16m█�[0;00m�[38;5;154;48;5;16m█�[0;00m�[38;5;154;48;5;16m█�[0;00m�[38;5;118;48;5;16m█�[0;00m�[38;5;118;48;5;16m█�[0;00m�[38;5;118;48;5;16m█�[0;00m
func FgByte ¶
Byte colorizes the input with the terminal color that matches the closest the RGB color.
func FgBytes ¶
Bytes colorizes the foreground with the terminal color that matches the closest the RGB color.
func FgString ¶
FgString colorizes the foreground of the input with the terminal color that matches the closest the RGB color.
This is simply a helper for Bytes.
func Interpret ¶
func Interpret(r io.ByteReader, w io.Writer) error
Interpret reads data from r and writes it to w, while substituting the color escapes in the input.
Color escapes are directives having one of forms on the following lines:
{#RRGGBB} {#RRGGBB,#RRGGBB} {,#RRGGBB} {}
The first form sets the terminal foreground color to the color RR,GG,BB where RR is the red component, GG green and BB blue. Each component is in hex.
The second form sets the foreground and background colors, while the third sets only the background.
The fourth form resets the terminal colors to defaults.
To output a literal { character, use two braces: {{.
func InterpretStr ¶
ColorizeTemplate substitutes the color escapes in the string s and returns the resulting string.
See Interpret for a description of color escapes.
func String ¶
String colorizes the input with the terminal color that matches the closest the RGB color.
This is simply a helper for Bytes.
Example ¶
package main import ( "fmt" "github.com/aybabtme/rgbterm" ) func main() { var r, g, b uint8 // pick a color r, g, b = 252, 255, 43 // choose a word word := "=)" // colorize it! coloredWord := rgbterm.String(word, r, g, b, 0, 0, 0) fmt.Println("Oh!", coloredWord, "hello!") }
Output: Oh! �[38;5;190;48;5;16m=)�[0;00m hello!
Types ¶
type InterpretingWriter ¶
type InterpretingWriter struct {
// contains filtered or unexported fields
}
ColorTemplateWriter writes to an underlying io.Writer while substituting the color escapes in the input.
See Interpret for a description of color escapes.
func NewInterpretingWriter ¶
func NewInterpretingWriter(w io.Writer) InterpretingWriter
NewColorTemplateWriter creates a ColorTemplateWriter that writes to w while substituting the color escapes in the input.
See Interpret for a description of color escapes.