// Provides a [color.Color] type for dealing with OkLab colours without alpha. package oklab import ( "image/color" "math" "smariot.com/color/internal/helper" ) // Color is an OkLab [color.Color]. type Color struct { Lightness, ChromaA, ChromaB float64 } // DistanceSqr returns the euclidean distance squared between two colours. func DistanceSqr(a, b Color) float64 { dLightness := a.Lightness - b.Lightness dChromaA := a.ChromaA - b.ChromaA dChromaB := a.ChromaB - b.ChromaB return dLightness*dLightness + dChromaA*dChromaA + dChromaB*dChromaB } // 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(helper.LMStoLRGB(helper.OkLabToLMS(c.Lightness, c.ChromaA, c.ChromaB))) a = 0xffff return } // NRGBA converts to non-premultiplied RGBA. func (c Color) NRGBA() (r, g, b, a uint32) { r, g, b = helper.LRGBtoRGB(helper.LMStoLRGB(helper.OkLabToLMS(c.Lightness, c.ChromaA, c.ChromaB))) a = 0xffff return } // NLRGBA converts to non-premultiplied linear RGBA. func (c Color) NLRGBA() (r, g, b, a float64) { r, g, b = helper.LMStoLRGB(helper.OkLabToLMS(c.Lightness, c.ChromaA, c.ChromaB)) a = 1 return } // NXYZA converts to non-premultiplied XYZ+Alpha. func (c Color) NXYZA() (x, y, z, a float64) { x, y, z = helper.LMStoXYZ(helper.OkLabToLMS(c.Lightness, c.ChromaA, c.ChromaB)) return x, y, z, 1 } // NOkLabA converts to non-premultiplied OkLab+Alpha. func (c Color) NOkLabA() (lightness, chromaA, chromaB, a float64) { return c.Lightness, c.ChromaA, c.ChromaB, 1 } // Convert converts an arbitrary colour type to an OkLab [Color]. func Convert(c color.Color) Color { if c, ok := c.(Color); ok { return c } lightness, chromaA, chromaB, _ := helper.ColorToNOkLabA(c) return Color{lightness, chromaA, chromaB} } // A [color.Model] for converting arbitrary colours to an OkLab [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{}