color

package
v0.0.0-...-d322e89 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2018 License: BSD-2-Clause Imports: 5 Imported by: 24

Documentation

Overview

Package color provides types and functions to manipulate colors.

The types defined here are compatible with the standard library package "image/color", but are designed to work well with the GPU.

The first difference with the standard library is that most of them are based on float32 (instead of uint32), so they can be directly passed to GPU shaders.

The second difference is that they make an explicit distinction between linear and standard (sRGB) color spaces, while the standard library only makes distinction between alpha-premultiplied and alpha-postmultiplied. Both distinction are equally important for correct color handling.

Linear and sRGB

Structs prefixed by "L" are in linear color space, while structs prefixed by "S" are in standard (sRGB) color space.

For the importance of this distinction, see:

http://blog.johnnovak.net/2016/09/21/what-every-coder-should-know-about-gamma/

Alpha Pre-Multipled and Post-Multiplied

Structs ending with "nA" are alpha post-multplied; all others are alpha pre-multiplied.

In an alpha-premultiplied color, the three RGB component have been scaled by alpha; valid values are therefore within [0, alpha]. This is the most useful choice for alpha-blending.

For the importance of alpha pre-multipled, see:

https://blogs.msdn.microsoft.com/shawnhar/2009/11/06/premultiplied-alpha/

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Color

type Color interface {
	// Linear returns the red, green, blue and alpha values in linear color space.
	// The red, gren and blue values have been alpha-premultiplied in linear
	// space. Each value ranges within [0, 1] and can be used directly by GPU
	// shaders.
	Linear() (r, g, b, a float32)

	// Standard returns the red, green, blue and alpha values in standard (sRGB)
	// color space. The red, gren and blue values have been alpha-premultiplied in
	// linear space. Each value ranges within [0, 1].
	Standard() (r, g, b, a float32)
}

Color can convert itself to alpha-premultipled RGBA as 32 bit floats, in both linear and standard (sRGB) color spaces.

type Index

type Index uint8

An Index is used to refer to colors inside a palette.

type LRGB

type LRGB struct {
	R float32
	G float32
	B float32
}

LRGB represents a color in linear color space. Each value ranges within [0, 1], and can be used directly by GPU shaders.

func LRGBof

func LRGBof(c Color) LRGB

LRGBof converts any color to linear color space with no alpha.

func (LRGB) Linear

func (c LRGB) Linear() (r, g, b, a float32)

Linear implements the Colour interface.

func (LRGB) RGBA

func (c LRGB) RGBA() (r, g, b, a uint32)

RGBA implements the image.Color interface: it returns the four components scaled by 0xFFFF.

func (LRGB) Standard

func (c LRGB) Standard() (r, g, b, a float32)

Standard implements the Colour interface.

type LRGBA

type LRGBA struct {
	R float32
	G float32
	B float32
	A float32
}

LRGBA represents a color in alpha-premultiplied linear color space. Each value ranges within [0, 1], and can be used directly by GPU shaders.

An alpha-premultiplied color component c has been scaled by alpha (a), so has valid values 0 <= c <= a.

Note that additive blending can also be achieved when alpha is set to 0 while the color components are non-null.

func LRGBAof

func LRGBAof(c Color) LRGBA

LRGBAof converts any color to alpha-premultiplied, linear color space.

func (LRGBA) Linear

func (c LRGBA) Linear() (r, g, b, a float32)

Linear implements the Colour interface.

func (LRGBA) RGBA

func (c LRGBA) RGBA() (r, g, b, a uint32)

RGBA implements the image.Color interface: it returns the four components scaled by 0xFFFF.

func (LRGBA) Standard

func (c LRGBA) Standard() (r, g, b, a float32)

Standard implements the Colour interface.

type LRGBnA

type LRGBnA struct {
	R float32
	G float32
	B float32
	A float32
}

LRGBnA represents a color in *non* alpha-premultiplied linear color space. Each value ranges within [0, 1], and can be used directly by GPU shaders.

Note: prefer RGBA for use in shaders.

func LRGBnAof

func LRGBnAof(c Color) LRGBnA

LRGBnAof converts any color to non alpha-premultiplied linear color space.

func (LRGBnA) Linear

func (c LRGBnA) Linear() (r, g, b, a float32)

Linear implements the Colour interface.

func (LRGBnA) RGBA

func (c LRGBnA) RGBA() (r, g, b, a uint32)

RGBA implements the image.Color interface: it returns the four components scaled by 0xFFFF.

func (LRGBnA) Standard

func (c LRGBnA) Standard() (r, g, b, a float32)

Standard implements the Colour interface.

type Palette

type Palette struct {
	ByName map[string]Index
	Colors []LRGBA
}

A Palette is an ordered list of colors (defined by their LRGBA values), and a name-to-index dictionary.

func PaletteFrom

func PaletteFrom(path string) Palette

PaletteFrom returns a new Palette created from the file at the specified path.

type SRGB

type SRGB struct {
	R float32
	G float32
	B float32
}

SRGB represents a color in sRGB color space. Each value ranges within [0, 1], and can be used directly by GPU shaders.

func SRGBof

func SRGBof(c Color) SRGB

SRGBof converts any color to sRGB color space with no alpha.

func (SRGB) Linear

func (c SRGB) Linear() (r, g, b, a float32)

Linear implements the Colour interface.

func (SRGB) RGBA

func (c SRGB) RGBA() (r, g, b, a uint32)

RGBA implements the image.Color interface: it returns the four components scaled by 0xFFFF.

func (SRGB) Standard

func (c SRGB) Standard() (r, g, b, a float32)

Standard implements the Colour interface.

type SRGB8

type SRGB8 struct {
	R uint8
	G uint8
	B uint8
}

SRGB8 represents a 24-bit color in sRGB color space. There is 8 bits for each components.

func SRGB8of

func SRGB8of(c Color) SRGB8

SRGB8of converts any color to sRGB color space.

func (SRGB8) Linear

func (c SRGB8) Linear() (r, g, b, a float32)

Linear implements the Colour interface.

func (SRGB8) RGBA

func (c SRGB8) RGBA() (r, g, b, a uint32)

RGBA implements the image.Color interface.

func (SRGB8) Standard

func (c SRGB8) Standard() (r, g, b, a float32)

Standard implements the Colour interface.

type SRGBA

type SRGBA struct {
	R float32
	G float32
	B float32
	A float32
}

SRGBA represents a color in alpha-premultiplied sRGB color space. Each value ranges within [0, 1].

An alpha-premultiplied color component c has been scaled by alpha (a), so has valid values 0 <= c <= a.

Note that additive blending can also be achieved when alpha is set to 0 while the color components are non-null.

func SRGBAof

func SRGBAof(c Color) SRGBA

SRGBAof converts any color to sRGB alpha-premultiplied color space.

func (SRGBA) Linear

func (c SRGBA) Linear() (r, g, b, a float32)

Linear implements the Colour interface: it returns the color converted to linear color space.

func (SRGBA) RGBA

func (c SRGBA) RGBA() (r, g, b, a uint32)

RGBA implements the image.Color interface: it returns the four components scaled by 0xFFFF.

func (SRGBA) Standard

func (c SRGBA) Standard() (r, g, b, a float32)

Standard implements the Colour interface.

type SRGBA8

type SRGBA8 struct {
	R uint8
	G uint8
	B uint8
	A uint8
}

SRGBA8 represents a 32-bit color in alpha-premultiplied sRGB color space. There is 8 bits for each components.

An alpha-premultiplied color component c has been scaled by alpha (a), so has valid values 0 <= c <= a.

Note that additive blending can also be achieved when alpha is set to 0 while the color components are non-null.

func SRGBA8of

func SRGBA8of(c Color) SRGBA8

SRGBA8of converts any color to alpha-premultiplied sRGB color space.

func (SRGBA8) Linear

func (c SRGBA8) Linear() (r, g, b, a float32)

Linear implements the Colour interface.

func (SRGBA8) RGBA

func (c SRGBA8) RGBA() (r, g, b, a uint32)

RGBA implements the image.Color interface.

func (SRGBA8) Standard

func (c SRGBA8) Standard() (r, g, b, a float32)

Standard implements the Colour interface.

type SRGBnA

type SRGBnA struct {
	R float32
	G float32
	B float32
	A float32
}

SRGBnA represents a color in *non* alpha-premultiplied sRGB color space. Each value ranges within [0, 1].

An alpha-premultiplied color component c has been scaled by alpha (a), so has valid values 0 <= c <= a.

Note that additive blending can also be achieved when alpha is set to 0 while the color components are non-null.

func SRGBnAof

func SRGBnAof(c Color) SRGBnA

SRGBnAof converts any color to sRGB non alpha-premultiplied sRGB color space.

func (SRGBnA) Linear

func (c SRGBnA) Linear() (r, g, b, a float32)

Linear implements the Colour interface: it returns the color converted to alpha-premultipled linear color space.

func (SRGBnA) RGBA

func (c SRGBnA) RGBA() (r, g, b, a uint32)

RGBA implements the image.Color interface: it returns the four components scaled by 0xFFFF.

func (SRGBnA) Standard

func (c SRGBnA) Standard() (r, g, b, a float32)

Standard implements the Colour interface.

type SRGBnA8

type SRGBnA8 struct {
	R uint8
	G uint8
	B uint8
	A uint8
}

SRGBnA8 represents a 32-bit color in *non* alpha-premultiplied sRGB color space. There is 8 bits for each compnent.

func SRGBnA8of

func SRGBnA8of(c Color) SRGBnA8

SRGBnA8of converts any color to non alpha-premultiplied sRGB color space.

func (SRGBnA8) Linear

func (c SRGBnA8) Linear() (r, g, b, a float32)

Linear implements the Colour interface.

func (SRGBnA8) RGBA

func (c SRGBnA8) RGBA() (r, g, b, a uint32)

RGBA implements the image.Color interface: it returns the four components scaled by 0xFFFF.

func (SRGBnA8) Standard

func (c SRGBnA8) Standard() (r, g, b, a float32)

Standard implements the Colour interface.

Directories

Path Synopsis
Package c64 provides the color palette of C64 microcomputers.
Package c64 provides the color palette of C64 microcomputers.
Package cpc provides the color palette of CPC microcomputers.
Package cpc provides the color palette of CPC microcomputers.
Package msx provides the color palette of MSX1 microcomputers.
Package msx provides the color palette of MSX1 microcomputers.
Package msx2 provides the color palette of MSX2 microcomputers.
Package msx2 provides the color palette of MSX2 microcomputers.
Package pico8 provides the color palette of the PICO-8 fantasy console.
Package pico8 provides the color palette of the PICO-8 fantasy console.

Jump to

Keyboard shortcuts

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