colorspace

package
v0.1.122 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MinId = -1
	MaxId = 63
)
View Source
const (
	ColorSpaceSrgb               = 0
	ColorSpaceLinearSrgb         = 1
	ColorSpaceExtendedSrgb       = 2
	ColorSpaceLinearExtendedSrgb = 3
	ColorSpaceBt709              = 4
	ColorSpaceBt2020             = 5
	ColorSpaceDciP3              = 6
	ColorSpaceDisplayP3          = 7
	ColorSpaceNtsc1953           = 8
	ColorSpaceSmpteC             = 9
	ColorSpaceAdobeRgb           = 10
	ColorSpaceProPhotoRgb        = 11
	ColorSpaceAces               = 12
	ColorSpaceAcescg             = 13
	ColorSpaceCieXyz             = 14
	ColorSpaceCieLab             = 15
	ColorSpaceOklab              = 16 // Check ID mapping
)

Predefined ColorSpace IDs

View Source
const (
	LabA = 216.0 / 24389.0
	LabB = 841.0 / 108.0
	LabC = 4.0 / 29.0
	LabD = 6.0 / 29.0
)
View Source
const (
	TypePQish  = -2.0
	TypeHLGish = -3.0
)

Variables

View Source
var (
	// AdaptationBradford is the Bradford chromatic adaptation transform, as defined in the CIECAM97s color appearance model.
	AdaptationBradford = Adaptation{
		Transform: []float32{
			0.8951, -0.7502, 0.0389,
			0.2664, 1.7135, -0.0685,
			-0.1614, 0.0367, 1.0296,
		},
	}

	// AdaptationVonKries is the von Kries chromatic adaptation transform.
	AdaptationVonKries = Adaptation{
		Transform: []float32{
			0.40024, -0.22630, 0.00000,
			0.70760, 1.16532, 0.00000,
			-0.08081, 0.04570, 0.91822,
		},
	}

	// AdaptationCiecat02 is the CIECAT02 chromatic adaption transform, as defined in the CIECAM02 color appearance model.
	AdaptationCiecat02 = Adaptation{
		Transform: []float32{
			0.7328, -0.7036, 0.0030,
			0.4296, 1.6975, 0.0136,
			-0.1624, 0.0061, 0.9834,
		},
	}
)
View Source
var (
	// Rgb is a color model with 3 components that refer to the three additive
	// primaries: red, green and blue.
	ColorModelRgb = ColorModel{util.PackInts(3, 0)}

	// Xyz is a color model with 3 components that are used to model human color
	// vision on a basic sensory level.
	ColorModelXyz = ColorModel{util.PackInts(3, 1)}

	// Lab is a color model with 3 components used to describe a color space that is
	// more perceptually uniform than XYZ.
	ColorModelLab = ColorModel{util.PackInts(3, 2)}

	// Cmyk is a color model with 4 components that refer to four inks used in color
	// printing: cyan, magenta, yellow and black (or key). CMYK is a subtractive color model.
	ColorModelCmyk = ColorModel{util.PackInts(4, 3)}
)
View Source
var (
	// SrgbTransferParameters for sRGB.
	// gamma=2.4, a=1/1.055, b=0.055/1.055, c=1/12.92, d=0.04045
	SrgbTransferParameters = &TransferParameters{
		Gamma: 2.4,
		A:     1.0 / 1.055,
		B:     0.055 / 1.055,
		C:     1.0 / 12.92,
		D:     0.04045,
		E:     0.0,
		F:     0.0,
	}

	SrgbPrimaries = []float32{0.640, 0.330, 0.300, 0.600, 0.150, 0.060}

	SrgbTransform = []float32{
		0.412391, 0.357584, 0.180481,
		0.212639, 0.715169, 0.072192,
		0.019331, 0.119195, 0.950532,
	}

	Srgb = NewRgb(
		"sRGB IEC61966-2.1",
		SrgbPrimaries,
		IlluminantD65,
		SrgbTransform,
		func(x float64) float64 {
			return RcpResponse(x, SrgbTransferParameters.A, SrgbTransferParameters.B, SrgbTransferParameters.C, SrgbTransferParameters.D, SrgbTransferParameters.Gamma)
		},
		func(x float64) float64 {
			return Response(x, SrgbTransferParameters.A, SrgbTransferParameters.B, SrgbTransferParameters.C, SrgbTransferParameters.D, SrgbTransferParameters.Gamma)
		},
		0.0, 1.0,
		SrgbTransferParameters,
		ColorSpaceSrgb,
	)

	LinearSrgb = NewRgb(
		"sRGB IEC61966-2.1 (Linear)",
		SrgbPrimaries,
		IlluminantD65,
		SrgbTransform,
		func(x float64) float64 { return x },
		func(x float64) float64 { return x },
		0.0, 1.0,
		&TransferParameters{Gamma: 1.0, A: 1.0, B: 0.0, C: 0.0, D: 0.0, E: 0.0, F: 0.0},
		ColorSpaceLinearSrgb,
	)

	// Extended sRGB (scRGB)
	ExtendedSrgb = NewRgb(
		"scRGB-nl IEC 61966-2-2:2003",
		SrgbPrimaries,
		IlluminantD65,
		SrgbTransform,
		func(x float64) float64 {
			return RcpResponseExtended(x, SrgbTransferParameters.A, SrgbTransferParameters.B, SrgbTransferParameters.C, SrgbTransferParameters.D, SrgbTransferParameters.E, SrgbTransferParameters.F, SrgbTransferParameters.Gamma)
		},
		func(x float64) float64 {
			return ResponseExtended(x, SrgbTransferParameters.A, SrgbTransferParameters.B, SrgbTransferParameters.C, SrgbTransferParameters.D, SrgbTransferParameters.E, SrgbTransferParameters.F, SrgbTransferParameters.Gamma)
		},
		-0.799, 2.399,
		SrgbTransferParameters,
		ColorSpaceExtendedSrgb,
	)

	// Linear Extended sRGB
	LinearExtendedSrgb = NewRgb(
		"scRGB IEC 61966-2-2:2003",
		SrgbPrimaries,
		IlluminantD65,
		SrgbTransform,
		func(x float64) float64 { return x },
		func(x float64) float64 { return x },
		-0.5, 7.499,
		&TransferParameters{Gamma: 1.0, A: 1.0, B: 0.0, C: 0.0, D: 0.0, E: 0.0, F: 0.0},
		ColorSpaceLinearExtendedSrgb,
	)

	DisplayP3 = NewRgb(
		"Display P3",
		[]float32{0.680, 0.320, 0.265, 0.690, 0.150, 0.060},
		IlluminantD65,
		[]float32{
			0.486570948965, 0.265667693169, 0.198217285234,
			0.228974564069, 0.691738521836, 0.079286914094,
			0.000000000000, 0.045113381846, 1.043944368900,
		},
		Srgb.Oetf,
		Srgb.Eotf,
		0.0, 1.0,
		SrgbTransferParameters,
		ColorSpaceDisplayP3,
	)

	// Oklab instance
	OklabInstance = NewOklab("Oklab", ColorSpaceOklab)

	Unspecified = &unspecifiedColorSpace{
		BaseColorSpace: NewBaseColorSpace("Unspecified", ColorModelRgb, MinId),
	}
)
View Source
var (
	// IlluminantA is Standard CIE 1931 2° illuminant A, encoded in xyY. This illuminant has a color temperature of 2856K.
	IlluminantA = NewWhitePoint(0.44757, 0.40745)

	// IlluminantB is Standard CIE 1931 2° illuminant B, encoded in xyY. This illuminant has a color temperature of 4874K.
	IlluminantB = NewWhitePoint(0.34842, 0.35161)

	// IlluminantC is Standard CIE 1931 2° illuminant C, encoded in xyY. This illuminant has a color temperature of 6774K.
	IlluminantC = NewWhitePoint(0.31006, 0.31616)

	// IlluminantD50 is Standard CIE 1931 2° illuminant D50, encoded in xyY. This illuminant has a color temperature of 5003K.
	IlluminantD50 = NewWhitePoint(0.34567, 0.35850)

	// IlluminantD55 is Standard CIE 1931 2° illuminant D55, encoded in xyY. This illuminant has a color temperature of 5503K.
	IlluminantD55 = NewWhitePoint(0.33242, 0.34743)

	// IlluminantD60 is Standard CIE 1931 2° illuminant D60, encoded in xyY. This illuminant has a color temperature of 6004K.
	IlluminantD60 = NewWhitePoint(0.32168, 0.33767)

	// IlluminantD65 is Standard CIE 1931 2° illuminant D65, encoded in xyY. This illuminant has a color temperature of 6504K.
	IlluminantD65 = NewWhitePoint(0.31271, 0.32902)

	// IlluminantD75 is Standard CIE 1931 2° illuminant D75, encoded in xyY. This illuminant has a color temperature of 7504K.
	IlluminantD75 = NewWhitePoint(0.29902, 0.31485)

	// IlluminantE is Standard CIE 1931 2° illuminant E, encoded in xyY. This illuminant has a color temperature of 5454K.
	IlluminantE = NewWhitePoint(0.33333, 0.33333)

	// IlluminantD50Xyz is the XYZ representation of D50.
	IlluminantD50Xyz = []float32{0.964212, 1.0, 0.825188}
)

Illuminant contains standard CIE WhitePoints.

View Source
var (
	// Matrices for M1 and M2 in Oklab spec
	M1 = []float32{
		0.8189330101, 0.3618667424, -0.1288597137,
		0.0329845436, 0.9293118715, 0.0361456387,
		0.0482003018, 0.2643662691, 0.6338517070,
	}
	M2 = []float32{
		0.2104542553, 1.9779984951, 0.0259040371,
		0.7936177850, -2.4285922050, 0.7827717662,
		-0.0040720468, 0.4505937, -0.8086757660,
	}
	InverseM1 = util.Inverse3x3(M1)
	InverseM2 = util.Inverse3x3(M2)
)

Functions

func AbsRcpResponse

func AbsRcpResponse(x, a, b, c, d, g float64) float64

AbsRcpResponse handles negative values by processing abs(x) and restoring sign

func AbsResponse

func AbsResponse(x, a, b, c, d, g float64) float64

AbsResponse handles negative values by processing abs(x) and restoring sign

func CompareWhitePoint

func CompareWhitePoint(a, b WhitePoint) bool

Compare compares two WhitePoints with a precision of 1e-3.

func RcpResponse

func RcpResponse(x, a, b, c, d, g float64) float64

RcpResponse returns the reciprocal parametric response (OETF key). if x >= d*c: (x^(1/g) - b) / a else: x / c

func RcpResponseExtended

func RcpResponseExtended(x, a, b, c, d, e, f, g float64) float64

RcpResponse extended with e and f

func Response

func Response(x, a, b, c, d, g float64) float64

Response returns the parametric response (EOTF key). if x >= d: (a*x + b)^g else: c*x

func ResponseExtended

func ResponseExtended(x, a, b, c, d, e, f, g float64) float64

Response extended with e and f

func XyzaToColorHelper

func XyzaToColorHelper(cs ColorSpace, x, y, z, a float32, destColorSpace ColorSpace) (uint64, error)

Helper for XyzaToColor that can be used by implementations

Types

type Adaptation

type Adaptation struct {
	Transform []float32
}

Adaptation represents a chromatic adaptation matrix used for the von Kries transform. These matrices are used to convert values in the CIE XYZ space to values in the LMS space.

func (Adaptation) String

func (a Adaptation) String() string

type BaseColorSpace

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

BaseColorSpace provides common fields for ColorSpace implementations. Embedding this struct allows reusing Name, Model, Id fields and basic methods.

func NewBaseColorSpace

func NewBaseColorSpace(name string, model ColorModel, id int) BaseColorSpace

func (BaseColorSpace) ComponentCount

func (b BaseColorSpace) ComponentCount() int

func (BaseColorSpace) Id

func (b BaseColorSpace) Id() int

func (BaseColorSpace) IsSrgb

func (b BaseColorSpace) IsSrgb() bool

Default implementation returns false.

func (BaseColorSpace) Model

func (b BaseColorSpace) Model() ColorModel

func (BaseColorSpace) Name

func (b BaseColorSpace) Name() string

func (BaseColorSpace) ToXy

func (b BaseColorSpace) ToXy(v0, v1, v2 float32) int64

func (BaseColorSpace) ToZ

func (b BaseColorSpace) ToZ(v0, v1, v2 float32) float32

func (BaseColorSpace) XyzaToColor

func (b BaseColorSpace) XyzaToColor(x, y, z, a float32, colorSpace ColorSpace) (uint64, error)

type ColorModel

type ColorModel struct {
	// pack both the number of components and an ordinal value to distinguish between different
	// ColorModel types that have the same number of components
	PackedValue int64
}

ColorModel is a value class in Kotlin, ported as a struct wrapping a packed int64. A color model is required by a ColorSpace to describe the way colors can be represented as tuples of numbers. A common color model is the [RGB]Rgb color model which defines a color as represented by a tuple of 3 numbers (red, green and blue).

func (ColorModel) ComponentCount

func (cm ColorModel) ComponentCount() int

ComponentCount returns the number of components for this color model. An integer between 1 and 4.

func (ColorModel) String

func (cm ColorModel) String() string

type ColorSpace

type ColorSpace interface {
	Name() string
	Model() ColorModel
	Id() int
	ComponentCount() int
	IsWideGamut() bool
	IsSrgb() bool
	MinValue(component int) float32
	MaxValue(component int) float32
	ToXyz(v []float32) []float32
	FromXyz(v []float32) []float32
	ToXy(v0, v1, v2 float32) int64
	ToZ(v0, v1, v2 float32) float32
	XyzaToColor(x, y, z, a float32, colorSpace ColorSpace) (uint64, error) // Returns packed Color
}

ColorSpace identifies a specific organization of colors.

func Get

func Get(id int) ColorSpace

Get returns the ColorSpace with the given ID.

type Connector

type Connector struct {
	Source               ColorSpace
	Destination          ColorSpace
	Intent               RenderIntent
	TransformMatrix      []float32
	TransformSource      []float32
	TransformDestination []float32
}

Connector connects two color spaces to allow conversion from the source color space to the destination color space.

func NewConnector

func NewConnector(source, destination ColorSpace, intent RenderIntent) *Connector

NewConnector creates a new Connector.

func (*Connector) Transform

func (c *Connector) Transform(v []float32) []float32

Transform converts a color from source space to destination space.

func (*Connector) TransformToColor

func (c *Connector) TransformToColor(v []float32) (uint64, error)

type Lab

type Lab struct {
	BaseColorSpace
}

func NewLab

func NewLab(name string, id int) *Lab

func (*Lab) FromXyz

func (l *Lab) FromXyz(v []float32) []float32

func (*Lab) IsWideGamut

func (l *Lab) IsWideGamut() bool

func (*Lab) MaxValue

func (l *Lab) MaxValue(component int) float32

func (*Lab) MinValue

func (l *Lab) MinValue(component int) float32

func (*Lab) ToXy

func (l *Lab) ToXy(v0, v1, v2 float32) int64

func (*Lab) ToXyz

func (l *Lab) ToXyz(v []float32) []float32

func (*Lab) ToZ

func (l *Lab) ToZ(v0, v1, v2 float32) float32

func (*Lab) XyzaToColor

func (l *Lab) XyzaToColor(x, y, z, a float32, colorSpace ColorSpace) (uint64, error)

type Oklab

type Oklab struct {
	BaseColorSpace
}

func NewOklab

func NewOklab(name string, id int) *Oklab

func (*Oklab) FromXyz

func (o *Oklab) FromXyz(v []float32) []float32

func (*Oklab) IsWideGamut

func (o *Oklab) IsWideGamut() bool

func (*Oklab) MaxValue

func (o *Oklab) MaxValue(component int) float32

func (*Oklab) MinValue

func (o *Oklab) MinValue(component int) float32

func (*Oklab) ToXy

func (o *Oklab) ToXy(v0, v1, v2 float32) int64

func (*Oklab) ToXyz

func (o *Oklab) ToXyz(v []float32) []float32

func (*Oklab) ToZ

func (o *Oklab) ToZ(v0, v1, v2 float32) float32

func (*Oklab) XyzaToColor

func (o *Oklab) XyzaToColor(x, y, z, a float32, colorSpace ColorSpace) (uint64, error)

type RenderIntent

type RenderIntent int

RenderIntent enum

const (
	RenderIntentPerceptual RenderIntent = iota
	RenderIntentRelative
	RenderIntentSaturation
	RenderIntentAbsolute
)

type Rgb

type Rgb struct {
	BaseColorSpace
	WhitePoint         WhitePoint
	Primaries          []float32
	Transform          []float32
	InverseTransform   []float32
	TransferParameters *TransferParameters
	Oetf               func(float64) float64
	Eotf               func(float64) float64
	Min                float32
	Max                float32
	// contains filtered or unexported fields
}

Rgb represents an RGB color space.

func NewRgb

func NewRgb(name string, primaries []float32, whitePoint WhitePoint, transform []float32, oetf func(float64) float64, eotf func(float64) float64, min, max float32, transferParameters *TransferParameters, id int) *Rgb

NewRgb creates a new RGB color space.

func (*Rgb) FromXyz

func (r *Rgb) FromXyz(v []float32) []float32

func (*Rgb) FromXyzToDouble

func (r *Rgb) FromXyzToDouble(v []float32) []float64

FromXyz but with double result for internal use

func (*Rgb) IsSrgb

func (r *Rgb) IsSrgb() bool

func (*Rgb) IsWideGamut

func (r *Rgb) IsWideGamut() bool

func (*Rgb) MaxValue

func (r *Rgb) MaxValue(component int) float32

func (*Rgb) MinValue

func (r *Rgb) MinValue(component int) float32

func (*Rgb) ToXyz

func (r *Rgb) ToXyz(v []float32) []float32

type TransferParameters

type TransferParameters struct {
	// Value g in the equation of the EOTF
	Gamma float64
	// Value a in the equation of the EOTF
	A float64
	// Value b in the equation of the EOTF
	B float64
	// Value c in the equation of the EOTF
	C float64
	// Value d in the equation of the EOTF
	D float64
	// Value e in the equation of the EOTF
	E float64
	// Value f in the equation of the EOTF
	F float64
}

TransferParameters defines the parameters for the ICC parametric curve type 4, as defined in ICC.1:2004-10, section 10.15. The corresponding OETF is simply the inverse function.

func NewTransferParameters

func NewTransferParameters(gamma, a, b, c, d, e, f float64) (TransferParameters, error)

NewTransferParameters creates a new TransferParameters instance. e and f default to 0.0 if not provided in the Kotlin version, but here we require checking them or use a constructor. Since Go doesn't have default arguments, we'll provider helper or just struct literal if safe, but validation is complex.

type WhitePoint

type WhitePoint struct {
	X float32
	Y float32
}

WhitePoint represents a white point in the CIE xy chromaticity diagram.

func NewWhitePoint

func NewWhitePoint(x, y float32) WhitePoint

NewWhitePoint creates a new WhitePoint.

func (WhitePoint) ToXyz

func (wp WhitePoint) ToXyz() []float32

ToXyz converts this white point to reduced XYZ values (luminance Y = 1).

type Xyz

type Xyz struct {
	BaseColorSpace
}

func NewXyz

func NewXyz(name string, id int) *Xyz

func (*Xyz) FromXyz

func (x *Xyz) FromXyz(v []float32) []float32

func (*Xyz) IsWideGamut

func (x *Xyz) IsWideGamut() bool

func (*Xyz) MaxValue

func (x *Xyz) MaxValue(component int) float32

func (*Xyz) MinValue

func (x *Xyz) MinValue(component int) float32

func (*Xyz) ToXyz

func (x *Xyz) ToXyz(v []float32) []float32

Jump to

Keyboard shortcuts

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