colors

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2026 License: BSD-3-Clause Imports: 5 Imported by: 0

Documentation

Overview

Package colors parses colour specifications and maps scalars to colours the way matplotlib does: the same names, the same default property cycle (tab10), and colormaps whose lookup tables are matplotlib's own, generated from it by tools/gen_colormaps.py.

Index

Constants

This section is empty.

Variables

View Source
var Cycle = []string{
	"#1f77b4", "#ff7f0e", "#2ca02c", "#d62728", "#9467bd",
	"#8c564b", "#e377c2", "#7f7f7f", "#bcbd22", "#17becf",
}

Cycle is matplotlib's default property cycle (axes.prop_cycle): the ten Tableau colours, tab:blue first.

Functions

func Index

func Index(x float64) int

Index returns the lookup-table index matplotlib uses for x, or -1 for under, 256 for over and -2 for NaN: x*256, with 1.0 itself mapped to the last entry, truncated toward zero.

func Names

func Names() []string

Names returns the names of the built-in colormaps, sorted.

Types

type Colormap

type Colormap struct {
	Name string

	// Under, Over and Bad are the colours for values below 0, above 1 and
	// NaN. Matplotlib's defaults are the first entry, the last entry and
	// transparent black; Get sets them so.
	Under, Over, Bad RGBA
	// contains filtered or unexported fields
}

A Colormap maps [0, 1] to colours through a 256-entry lookup table, as a matplotlib Colormap with N=256 does.

func Get

func Get(name string) (*Colormap, error)

Get returns the named built-in colormap: viridis, plasma, inferno, magma, cividis, gray, coolwarm or RdBu. A name ending in "_r" returns the map reversed, as in matplotlib.

func MustGet

func MustGet(name string) *Colormap

MustGet is Get that panics on an unknown name.

func (*Colormap) At

func (c *Colormap) At(x float64) RGBA

At returns the colour for x, as matplotlib's Colormap.__call__ does for a float.

func (*Colormap) BytesAt

func (c *Colormap) BytesAt(x float64) [4]uint8

BytesAt returns the colour for x as 8-bit components exactly as matplotlib's Colormap.__call__(x, bytes=True) does, which truncates (lut*255).astype(uint8) rather than rounding.

type LogNorm

type LogNorm struct{ Vmin, Vmax float64 }

LogNorm normalises on a base-10 logarithmic scale. Values <= 0 map to NaN (drawn in the colormap's Bad colour), as matplotlib masks them.

func (LogNorm) Normalize

func (n LogNorm) Normalize(v float64) float64

Normalize implements Norm.

func (LogNorm) Range

func (n LogNorm) Range() (float64, float64)

Range implements Norm.

type Norm

type Norm interface {
	Normalize(v float64) float64
	Range() (vmin, vmax float64)
}

A Norm maps data values to [0, 1] before a colormap is applied.

type Normalize

type Normalize struct{ Vmin, Vmax float64 }

Normalize is matplotlib's linear Normalize: (v - Vmin) / (Vmax - Vmin), and 0 when Vmin == Vmax.

func (Normalize) Normalize

func (n Normalize) Normalize(v float64) float64

Normalize implements Norm.

func (Normalize) Range

func (n Normalize) Range() (float64, float64)

Range implements Norm.

type RGBA

type RGBA struct{ R, G, B, A float64 }

RGBA is a colour with straight (not premultiplied) alpha, each component in [0, 1], as matplotlib holds colours.

func CycleColor

func CycleColor(i int) RGBA

CycleColor returns colour i of the default cycle, wrapping around.

func MustParse

func MustParse(s string) RGBA

MustParse is Parse that panics on an invalid colour, for colours written in source code.

func Parse

func Parse(s string) (RGBA, error)

Parse reads a colour as matplotlib's to_rgba does: a name ("red", "tab:blue", "C3", "k"), a hex string ("#rgb", "#rgba", "#rrggbb", "#rrggbbaa"), a grey level ("0.5") or "none" (transparent).

func (RGBA) Bytes

func (c RGBA) Bytes() [4]uint8

Bytes returns the colour as 8-bit components, rounded to nearest.

func (RGBA) Hex

func (c RGBA) Hex() string

Hex returns "#rrggbb", or "#rrggbbaa" when the colour is not opaque.

func (RGBA) WithAlpha

func (c RGBA) WithAlpha(a float64) RGBA

WithAlpha returns c with its alpha replaced.

Jump to

Keyboard shortcuts

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