Golang package and command to convert color.Colour to the nearest xterm/bash/shell color
Go Shell
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
cmd/xtermcolor
examples
script
.gitignore
.travis.yml
CHANGELOG.mkd
CONTRIBUTING.mkd
LICENSE
README.mkd
colors.go
colors_test.go
examples_test.go

README.mkd

XtermColor

Find the closest xterm color to anything implementing color.Color.

Provides a color.Palette as xtermcolor.Colors so you can use .Convert and .Index, but also provides convenience functions to get the index as a uint8 from a color.Color, a 32 bit integer, or a 24 bit hex string.

Build Status

Full documentation can be found on GoDoc.

Basic usage (examples/basic.go):

package main

import (
    "fmt"
    "image/color"

    "github.com/tomnomnom/xtermcolor"
)

func main() {
    fmt.Println(xtermcolor.Colors.Convert(color.RGBA{128, 64, 32, 255}))

    fmt.Println(xtermcolor.FromColor(color.RGBA{120, 210, 120, 255}))

    fmt.Println(xtermcolor.FromInt(0xCC66FFFF))

    code, err := xtermcolor.FromHexStr("#FEFEFE")
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(code)
}
▶ go run examples/basic.go 
{135 95 0 255}
114
171
15

xtermcolor command

There's also an xtermcolor command you can install by running:

▶ go get github.com/tomnomnom/xtermcolor/cmd/xtermcolor

Or you can download a binary from the releases page.

The command returns the color code for a 24 bit hex number:

▶ xtermcolor cc66ff
171

...or for seperate 8 bit red, green and blue components:

▶ xtermcolor 210 128 0
172