xtermcolor

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

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

Go to latest
Published: Apr 28, 2016 License: MIT Imports: 3 Imported by: 12

README

# XtermColor

Find the closest xterm color to anything implementing [color.Color](https://golang.org/pkg/image/color/#Color).

Provides a [color.Palette](https://golang.org/pkg/image/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](https://travis-ci.org/tomnomnom/xtermcolor.svg?branch=master)](https://travis-ci.org/tomnomnom/xtermcolor)


Full documentation can be found on [GoDoc](https://godoc.org/github.com/tomnomnom/xtermcolor).

Basic usage (examples/basic.go):

```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](https://github.com/tomnomnom/xtermcolor/releases).

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
```

Documentation

Overview

Package xtermcolor provides a palette for xterm colors, and conversion to that palette from anything implementing color.Color

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrorEmptyHexStr is returned by FromHexStr when an empty hex string is provided
	ErrorEmptyHexStr = errors.New("Empty hex string provided")

	// ErrorHexParse is returned by FromHexStr when an invalid hex string is provided
	ErrorHexParse = errors.New("Failed to parse string as hex; try something like #CC66FF")
)
View Source
var Colors = color.Palette{}/* 256 elements not displayed */

Colors lists Xterm color codes vs. RGBA; values taken from https://gist.github.com/jasonm23/2868981 color.Palette is really []color.Color and provides .Convert() and .Index()

Functions

func FromColor

func FromColor(target color.Color) uint8

FromColor finds the closest xterm colour to a given color.Color

Example
package main

import (
	"fmt"
	"image/color"

	"github.com/tomnomnom/xtermcolor"
)

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

}
Output:

114

func FromHexStr

func FromHexStr(str string) (uint8, error)

FromHexStr finds the closest xterm color to a given 24 bit hex string, e.g. "#CC66FF" or "FEFEFE" It's mostly useful if you're used to specifying colours as hex in CSS etc

Example
package main

import (
	"fmt"

	"github.com/tomnomnom/xtermcolor"
)

func main() {
	code, err := xtermcolor.FromHexStr("#CC66FF")
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(code)

}
Output:

171

func FromInt

func FromInt(target uint32) uint8

FromInt finds the closest xterm color to a given 32 bit RGBA color (e.g. 0xff00ff00).

Example
package main

import (
	"fmt"

	"github.com/tomnomnom/xtermcolor"
)

func main() {
	fmt.Println(xtermcolor.FromInt(0xCC66FFFF))

}
Output:

171

Types

This section is empty.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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