rgbterm

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

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

Go to latest
Published: Sep 6, 2017 License: BSD-3-Clause, MIT Imports: 8 Imported by: 100

README

RGB terminal

Have you had enough of the same 16 colors in your terminal?

Welcome to a new world. A new world where colors are plentyful. A world in 256 colors.

so many colors!

Usage

// pick a color
var r, g, b uint8
r, g, b = 252, 255, 43
// choose a word
word := "=)"
// colorize it!
coloredWord := rgbterm.FgString(word, r, g, b)
// print it!
fmt.Println("Oh!", coloredWord, "hello!")

screen shot 2014-07-13 at 0 56 44

Also, to be correct: there are 216 colors and 16 grey scales. For more details, Xterm.

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

Examples

Constants

This section is empty.

Variables

View Source
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)
)
View Source
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

func BgByte(in byte, r, g, b uint8) []byte

BgByte colorizes the background of the input with the terminal color that matches the closest the RGB color.

func BgBytes

func BgBytes(in []byte, r, g, b uint8) []byte

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

func BgString(in string, r, g, b uint8) string

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

func Bytes(in []byte, fr, fg, fb, br, bg, bb uint8) []byte

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

func FgByte(in byte, r, g, b uint8) []byte

Byte colorizes the input with the terminal color that matches the closest the RGB color.

func FgBytes

func FgBytes(in []byte, r, g, b uint8) []byte

Bytes colorizes the foreground with the terminal color that matches the closest the RGB color.

func FgString

func FgString(in string, r, g, b uint8) string

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 HSLtoRGB

func HSLtoRGB(h, s, l float64) (r, g, b uint8)

HSLtoRGB is a helper that transforms 8bits RGB colors to 8bits HSL colors.

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

func InterpretStr(s string) string

ColorizeTemplate substitutes the color escapes in the string s and returns the resulting string.

See Interpret for a description of color escapes.

func RGBtoHSL

func RGBtoHSL(r, g, b uint8) (h, s, l float64)

RGBtoHSL is a helper that transforms 8bits HSL colors to 8bits RGB colors.

func String

func String(in string, fr, fg, fb, br, bg, bb uint8) 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.

func (InterpretingWriter) Write

func (tw InterpretingWriter) Write(p []byte) (n int, err error)

Write writes to the underlying io.Writer while substituting the color escapes in the input.

See Interpret for a description of color escapes.

Directories

Path Synopsis
Package rainbow makes your io.Writer much more joyful.
Package rainbow makes your io.Writer much more joyful.

Jump to

Keyboard shortcuts

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