// Provides a [color.Color] type for dealing with non-premultiplied linear grayscale+alpha colours. package nlgraya import ( "image/color" "math" "smariot.com/color/internal/helper" ) // Color is a non-premultiplied linear grayscale+alpha [color.Color]. type Color struct { Y, A float64 } func sqr(a float64) float64 { return a * a } // DistanceSqr returns the maximum possible euclidean distance squared between two colours, // accounting for the possible backgrounds they might be composited over. func DistanceSqr(a, b Color) float64 { dY := a.Y*a.A - b.Y*b.A dA := a.A - b.A return max(sqr(dY), sqr(dY+dA)) } // Distance returns the maximum possible euclidean distance between two colours, // accounting for the possible backgrounds they might be composited over. func Distance(a, b Color) float64 { dY := a.Y*a.A - b.Y*b.A dA := a.A - b.A return max(math.Abs(dY), math.Abs(dY+dA)) } // YA converts to premultiplied sRGB grayscale+alpha. func (c Color) YA() (y, a uint32) { y, a = c.NYA() y = y * a / 0xffff return } // NYA converts to non-premultiplied sRGB grayscale+alpha. func (c Color) NYA() (y, a uint32) { _y, _a := c.NLYA() return helper.Delinearize(_y), uint32(_a*0xffff + 0.5) } // NLYA converts to non-premultiplied linear grayscale+alpha. func (c Color) NLYA() (y, a float64) { return c.Y, c.A } // RGBA converts to premultiplied RGBA, implementing the [color.Color] interface. func (c Color) RGBA() (r, g, b, a uint32) { y, a := c.YA() return y, y, y, a } // NRGBA converts to non-premultiplied RGBA. func (c Color) NRGBA() (r, g, b, a uint32) { y, a := c.NYA() return y, y, y, a } // NLRGBA converts to non-premultiplied linear RGBA. func (c Color) NLRGBA() (r, g, b, a float64) { y, a := c.NLYA() return y, y, y, a } // NXYZA converts to non-premultiplied XYZ+Alpha. func (c Color) NXYZA() (x, y, z, a float64) { x, y, z = helper.LRGBtoXYZ(c.Y, c.Y, c.Y) return x, y, z, c.A } // NOkLabA converts to non-premultiplied OkLab+Alpha. func (c Color) NOkLabA() (lightness, chromaA, chromaB, a float64) { lightness, chromaA, chromaB = helper.LMStoOkLab(helper.LRGBtoLMS(c.Y, c.Y, c.Y)) return lightness, chromaA, chromaB, c.A } // Convert converts an arbitrary colour type to a premultiplied linear RGBA [Color]. func Convert(c color.Color) Color { if c, ok := c.(Color); ok { return c } r, g, b, a := helper.ColorToNLRGBA(c) // the color.Gray16Model documents that it uses the coefficients 0.299, 0.5867, and 0.114. // however, it does this using integer arithmetic, so the actual coefficients are effectively rounded to the nearest 1/0x10000. return Color{ Y: helper.DelinearizeF( helper.LinearizeF(r)*0x0.4c8bp0 + helper.LinearizeF(g)*0x0.9646p0 + helper.LinearizeF(b)*0x0.1d2fp0, ), A: a, } } // A [color.Model] for converting arbitrary colours to a premultiplied linear RGBA [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{}