colour

package
v0.0.0-...-7a21720 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2024 License: CC0-1.0, CC0-1.0 Imports: 6 Imported by: 9

README

Package colour

import "gitlab.com/fospathi/wire/colour"

Go package colour implements colour types. The colour types mostly copy the W3C's CSS Color Module Level 4 semantics.

Documentation

Overview

Package colour contains some colour types.

Index

Constants

This section is empty.

Variables

View Source
var (
	// GammaToLin converts a gamma corrected sRGB component to linear-light
	// form.
	//
	// The component shall be in the range 0 to 1.
	GammaToLin = func(val float64) float64 {
		if val < 0.04045 {
			return val / 12.92
		}
		return math.Pow((val+0.055)/1.055, 2.4)
	}

	// LinToGamma converts a linear-light sRGB component to gamma corrected
	// form.
	//
	// The component shall be in the range 0 to 1.
	LinToGamma = func(val float64) float64 {
		if val > 0.0031308 {
			return 1.055*math.Pow(val, 1/2.4) - 0.055
		}
		return 12.92 * val
	}
)

https://www.w3.org/TR/css-color-4/#color-conversion-code

View Source
var NamedColours = []string{}/* 148 elements not displayed */

NamedColours is a list of the 148 possible CSS4 named colours.

Functions

func IsNoSuchArcError

func IsNoSuchArcError(err error) bool

IsNoSuchArcError is whether the argument error indicates that the specified arc does not exist.

This may happen, for example, if two LCh colours are exactly 180 degrees apart in hue and a minor or major hue arc was expected.

Types

type Join

type Join struct {
	// contains filtered or unexported fields
}

Join LCh colour gradients into one gradient.

func NewJoin

func NewJoin() *Join

NewJoin constructs a new Join.

func (*Join) Append

func (j *Join) Append(g LChGrad, w float64)

Append the given gradient to the join and give it the given weighting.

The weight determines the given gradient's proportion of the Join's domain: it is ultimately divided by the sum of all the join's weights.

The minimum weight is 1.

func (*Join) Colour

func (j *Join) Colour(t float64) mech.Colour

Colour takes a parameter that varies in the range 0..1 and returns a linearly interpolated RGB colour with components clamped to the range 0..1.

func (*Join) Colours

func (j *Join) Colours(t1, t2 float64) mech.Colourer

Colours is a slice of the receiver gradient's colours between the argument colour parameters.

func (*Join) Uniform

func (j *Join) Uniform() bool

Uniform reports whether the gradient is a single colour.

type LCh

type LCh struct {
	// L and C represent the colour's lightness (L*) and chroma (C*) values.
	//
	// L* = 0 gives black and L* = 100 gives diffuse white; specular white may
	// be higher.
	//
	// C* = 0 gives one of the true neutral grays. In practice C* does not
	// exceed 230 (since the corresponding a* and b* values in Cartesian
	// coordinates do not in practice exceed ±160).
	L, C float64

	// H represents the colour's hue (h*) value, and is the polar coordinates
	// angle in the range 0..360:
	//
	// * yellow = 90°
	//
	// * green = 180°
	//
	// * blue = 270°
	//
	// * red = 360°
	H float64
}

An LCh (CIELCh colour space) colour.

An LCh colour is a direct cylindrical coordinates equivalent of a Cartesian coordinates Lab colour.

The illuminant is D50 white.

In CIELCh the CIE acronym is a French title which translates as International Commission on Illumination.

func (LCh) Colour

func (lch LCh) Colour() mech.Colour

Colour is an sRGB equivalent of the receiver colour.

Note that the returned colour uses the D65 reference white whereas the receiver colour uses the D50 reference white.

func (LCh) GoString

func (lch LCh) GoString() string

GoString returns a Go parsable string representing the receiver colour. Its components are rounded to two decimal places.

func (LCh) Lab

func (lch LCh) Lab() Lab

Lab returns the Lab equivalent of the receiver colour.

func (LCh) RGB

func (lch LCh) RGB() RGB

RGB returns the sRGB equivalent of the receiver colour.

Note that the returned colour uses the D65 reference white whereas the receiver colour uses the D50 reference white.

type LChGrad

type LChGrad struct {
	Δh float64
	// contains filtered or unexported fields
}

LChGrad is a colour gradient that linearly interpolates between two LCh colours.

func NewHueGrad

func NewHueGrad(from LCh, Δh float64) LChGrad

NewHueGrad constructs a new LChGrad which covers the range of hue values starting from the given colour to the colour whose hue is the sum of the from and delta hues in the direction given by the sign of the delta hue.

The delta hue is measured in degrees and is clamped to the range -360..360.

func NewLChGrad

func NewLChGrad(from LCh, toL float64, toC float64, Δh float64) LChGrad

NewLChGrad constructs a new LChGrad which covers the range from the given colour to the absolute L and C values and the relative hue value.

The delta hue is measured in degrees and is clamped to the range -360..360.

func NewLChMajorGrad

func NewLChMajorGrad(from LCh, to LCh) (LChGrad, error)

NewLChMajorGrad constructs a new LChGrad which covers the (major arc) / (largest range) of hue values between the given colours.

func NewLChMinorGrad

func NewLChMinorGrad(from LCh, to LCh) (LChGrad, error)

NewLChMinorGrad constructs a new LChGrad which covers the (minor arc) / (smallest range) of hue values between the given colours.

func (LChGrad) Colour

func (g LChGrad) Colour(t float64) mech.Colour

Colour takes a parameter that varies in the range 0..1 and returns a linearly interpolated RGB colour with components clamped to the range 0..1.

func (LChGrad) Colours

func (g LChGrad) Colours(t1, t2 float64) mech.Colourer

Colours is a slice of the receiver colour gradient between the given proportions of the interval in the range 0..1.

func (LChGrad) LCh

func (g LChGrad) LCh(t float64) LCh

LCh takes a parameter that varies in the range 0..1 and returns a linearly interpolated colour.

func (LChGrad) Uniform

func (g LChGrad) Uniform() bool

Uniform reports whether the gradient is a single colour.

type Lab

type Lab struct {
	// L, A, and B represent the colour's L*, a*, and b* values.
	//
	// L* = 0 gives black and L* = 100 gives diffuse white; specular white may
	// be higher.
	//
	// a* = positive gives a red; a* = negative gives a green.
	//
	// b* = positive gives a yellow; b* = negative gives a blue.
	//
	// a* = b* = 0 gives one of the true neutral grays. In practice the a* and
	// b* values do not exceed ±160.
	L, A, B float64
}

A Lab (CIELAB colour space) colour.

The illuminant is D50 white.

In CIELAB the CIE acronym is a French title which translates as International Commission on Illumination.

func (Lab) LCh

func (lab Lab) LCh() LCh

LCh returns the LCh equivalent of the receiver colour.

func (Lab) RGB

func (lab Lab) RGB() RGB

RGB returns the RGB equivalent of the receiver colour.

Note that the returned colour uses the D65 reference white whereas the receiver colour uses the D50 reference white.

type MonoGrad

type MonoGrad struct {
	// contains filtered or unexported fields
}

MonoGrad is a constant colour gradient.

func NewMonoGrad

func NewMonoGrad(col LCh) MonoGrad

NewMonoGrad constructs a new MonoGrad with the given constant colour.

func (MonoGrad) Colour

func (g MonoGrad) Colour(t float64) mech.Colour

Colour ignores the parameter and always returns the same colour.

func (MonoGrad) Colours

func (g MonoGrad) Colours(t1, t2 float64) mech.Colourer

Colours ignores the parameters and always returns the receiver colour gradient.

func (MonoGrad) Uniform

func (g MonoGrad) Uniform() bool

Uniform reports whether the gradient is a single colour.

type OkLCh

type OkLCh struct {
	// The lightness L is a percentage between 0 and 100. The chroma C is a
	// positive number that does not exceed 0.5 in practice. The hue H is an
	// angle in degrees from 0 to 360.
	//
	//                                  | (b) / 90 deg / mustard yellow
	//                                  |
	//                                  |
	//  180 deg / greenish cyan - - - - | - - - - (a) / 0 deg / purplish red
	//                                  |
	//                                  |
	//                                  | 270 deg / sky blue
	//
	// https://www.w3.org/TR/css-color-4/#funcdef-oklch
	L, C, H float64
}

An OkLCh colour.

The illuminant is D65 white.

https://www.w3.org/TR/css-color-4/#ok-lab.

func (OkLCh) Colour

func (lch OkLCh) Colour() mech.Colour

Colour is an sRGB equivalent of the receiver colour.

func (OkLCh) GoString

func (lch OkLCh) GoString() string

GoString returns a Go parsable string representing the receiver colour. Its components are rounded to two decimal places.

func (OkLCh) RGB

func (lch OkLCh) RGB() RGB

RGB returns the sRGB equivalent of the receiver colour.

type OkLChGrad

type OkLChGrad struct {
	Δh float64
	// contains filtered or unexported fields
}

OkLChGrad is a colour gradient that linearly interpolates between two OkLCh colours.

func NewOkLChGrad

func NewOkLChGrad(from OkLCh, toL float64, toC float64, Δh float64) OkLChGrad

NewOkLChGrad constructs a new OkLChGrad which covers the range from the given colour to the absolute L and C values and the relative hue value.

The delta hue is measured in degrees and is clamped to the range -360..360.

func (OkLChGrad) Colour

func (g OkLChGrad) Colour(t float64) mech.Colour

Colour takes a parameter that varies in the range 0..1 and returns a linearly interpolated RGB colour with components clamped to the range 0..1.

func (OkLChGrad) Colours

func (g OkLChGrad) Colours(t1, t2 float64) mech.Colourer

Colours is a slice of the receiver colour gradient between the given proportions of the interval in the range 0..1.

func (OkLChGrad) OkLCh

func (g OkLChGrad) OkLCh(t float64) OkLCh

OkLCh takes a parameter that varies in the range 0..1 and returns a linearly interpolated colour.

func (OkLChGrad) Uniform

func (g OkLChGrad) Uniform() bool

Uniform reports whether the gradient is a single colour.

type OkLab

type OkLab struct {
	// L is a percentage between 0 and 100. A and B are coordinates that do not
	// exceed ±0.5 in practice.
	//
	//                                  | (b) / 90 deg / mustard yellow
	//                                  |
	//                                  |
	//  180 deg / greenish cyan - - - - | - - - - (a) / 0 deg / purplish red
	//                                  |
	//                                  |
	//                                  | 270 deg / sky blue
	//
	// https://www.w3.org/TR/css-color-4/#funcdef-oklab
	L, A, B float64
}

An OkLab colour.

The illuminant is D65 white.

https://www.w3.org/TR/css-color-4/#ok-lab.

type RGB

type RGB struct {
	// The amount of the primary additive colours in the range 0..255.
	R, G, B uint8
}

An RGB (sRGB colour space) colour.

The illuminant is D65 white.

func NewRGB

func NewRGB(rgb maf.Vec) RGB

NewRGB creates and initialises a new RGB using the argument RGB value which shall be given in ratios of 1 format, i.e each value is in the range 0..1.

func (RGB) Colour

func (rgb RGB) Colour() mech.Colour

Colour is the standard Mechane colour format.

func (RGB) Colour64

func (rgb RGB) Colour64() maf.Vec

Colour64 returns the receiver colour in a vector format with components in the range 0..1.

func (RGB) LCh

func (rgb RGB) LCh() LCh

LCh returns the LCh equivalent of the receiver colour.

Note that the returned colour uses the D50 reference white whereas the receiver colour uses the D65 reference white.

func (RGB) Lab

func (rgb RGB) Lab() Lab

Lab returns the Lab equivalent of the receiver colour.

Note that the returned colour uses the D50 reference white whereas the receiver colour uses the D65 reference white.

func (RGB) OkLCh

func (rgb RGB) OkLCh() OkLCh

OkLCh returns the OkLCh equivalent of the receiver colour.

func (RGB) Opaque

func (rgb RGB) Opaque() RGBA

Opaque RGBA version of the receiver colour.

func (RGB) String

func (rgb RGB) String() string

String returns a CSS compatible string version of the receiver colour.

func (RGB) ToRGBA

func (rgb RGB) ToRGBA(a float64) RGBA

ToRGBA with the given alpha value and the receiver colour's RGB channels.

type RGBA

type RGBA struct {
	// The amount of the primary additive red colour in the range 0..255.
	R uint8 `json:"r"`
	// The amount of the primary additive green colour in the range 0..255.
	G uint8 `json:"g"`
	// The amount of the primary additive blue colour in the range 0..255.
	B uint8 `json:"b"`
	// Alpha in the range 0..1.
	A float64 `json:"a"`
}

An RGBA colour is an RGB colour with variable opacity.

func (RGBA) String

func (rgba RGBA) String() string

String returns a CSS compatible string version of the receiver colour.

func (RGBA) ToRGB

func (rgba RGBA) ToRGB() RGB

ToRGB colour.

The alpha channel is dropped.

Directories

Path Synopsis
lch
Package lch contains the 148 CSS4 colour names as LCh type colours.
Package lch contains the 148 CSS4 colour names as LCh type colours.
internal
Package main generates the 148 CSS4 named colour LCh type variable declarations in the lch package.
Package main generates the 148 CSS4 named colour LCh type variable declarations in the lch package.
Package rgb contains the 148 CSS4 colour names as RGB type colours as well as some other colours commonly used by Mechane.
Package rgb contains the 148 CSS4 colour names as RGB type colours as well as some other colours commonly used by Mechane.

Jump to

Keyboard shortcuts

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