// Provides a [color.Color] type for dealing with linear RGB colours without alpha. package lrgb import ( "image/color" "math" "smariot.com/color/internal/helper" ) // Color is a linear RGBA [color.Color]. type Color struct { R, G, B float64 } // DistanceSqr returns the euclidean distance squared between two colours. func DistanceSqr(a, b Color) float64 { dR := a.R - b.R dG := a.G - b.G dB := a.B - b.B return dR*dR + dG*dG + dB*dB } // Distance returns the euclidean distance between two colours, // // If you just want to compare relative distances, use [DistanceSqr] instead. func Distance(a, b Color) float64 { return math.Sqrt(DistanceSqr(a, b)) } // RGBA converts to premultiplied RGBA, implementing the [color.Color] interface. func (c Color) RGBA() (r, g, b, a uint32) { _r, _g, _b := helper.LRGBtoRGB(c.R, c.G, c.B) return _r, _g, _b, 0xffff } // NRGBA converts to non-premultiplied RGBA. func (c Color) NRGBA() (r, g, b, a uint32) { _r, _g, _b := helper.LRGBtoRGB(c.R, c.G, c.B) return _r, _g, _b, 0xffff } // NLRGBA converts to non-premultiplied linear RGBA. func (c Color) NLRGBA() (r, g, b, a float64) { return c.R, c.G, c.B, 1 } // NXYZA converts to non-premultiplied XYZ+Alpha. func (c Color) NXYZA() (x, y, z, a float64) { x, y, z = helper.LRGBtoXYZ(c.R, c.G, c.B) return x, y, z, 1 } // NOkLabA converts to non-premultiplied OkLab+Alpha. func (c Color) NOkLabA() (lightness, chromaA, chromaB, a float64) { lightness, chromaA, chromaB = helper.LMStoOkLab(helper.LRGBtoLMS(c.R, c.G, c.B)) return lightness, chromaA, chromaB, 1 } // Convert converts an arbitrary colour type to a linear RGB [Color]. func Convert(c color.Color) Color { if c, ok := c.(Color); ok { return c } r, g, b, _ := helper.ColorToNLRGBA(c) return Color{r, g, b} } // A [color.Model] for converting arbitrary colours to a linear RGB [Color]. // // Wraps the [Convert] function, returning a [color.Color] interface rather than the [Color] type. var Model = helper.Model(Convert) // Type assertion. var _ color.Color = Color{}