94 lines
2.6 KiB
Go
94 lines
2.6 KiB
Go
// Provides a [color.Color] type for dealing with non-premultiplied OkLab+Alpha colours.
|
|
package noklaba
|
|
|
|
import (
|
|
"image/color"
|
|
"math"
|
|
|
|
"smariot.com/color/internal/helper"
|
|
)
|
|
|
|
// Color is a non-premultiplied OkLab+Alpha [color.Color].
|
|
type Color struct {
|
|
Lightness, ChromaA, ChromaB, 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 {
|
|
dLightness := a.Lightness*a.A - b.Lightness*b.A
|
|
dChromaA := a.ChromaA*a.A - b.ChromaA*b.A
|
|
dChromaB := a.ChromaB*a.A - b.ChromaB*b.A
|
|
dA := a.A - b.A
|
|
return max(sqr(dLightness), sqr(dLightness+dA)) + max(sqr(dChromaA), sqr(dChromaA+dA)) + max(sqr(dChromaB), sqr(dChromaB+dA))
|
|
}
|
|
|
|
// Distance returns the maximum possible euclidean distance between two colours,
|
|
// accounting for the possible backgrounds they might be composited over.
|
|
//
|
|
// 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) {
|
|
return helper.NLRGBAtoRGBA(c.NLRGBA())
|
|
}
|
|
|
|
// NRGBA converts to non-premultiplied RGBA.
|
|
func (c Color) NRGBA() (r, g, b, a uint32) {
|
|
return helper.NLRGBAtoNRGBA(c.NLRGBA())
|
|
}
|
|
|
|
// NLRGBA converts to non-premultiplied linear RGBA.
|
|
func (c Color) NLRGBA() (r, g, b, a float64) {
|
|
if c.A <= 0 {
|
|
return 0, 0, 0, 0
|
|
}
|
|
|
|
r, g, b = helper.LMStoLRGB(helper.OkLabToLMS(c.Lightness, c.ChromaA, c.ChromaB))
|
|
return r, g, b, c.A
|
|
}
|
|
|
|
// NXYZA converts to non-premultiplied XYZ+Alpha.
|
|
func (c Color) NXYZA() (x, y, z, a float64) {
|
|
if c.A <= 0 {
|
|
return 0, 0, 0, 0
|
|
}
|
|
|
|
x, y, z = helper.LMStoXYZ(helper.OkLabToLMS(c.Lightness, c.ChromaA, c.ChromaB))
|
|
return x, y, z, c.A
|
|
}
|
|
|
|
// NOkLabA converts to non-premultiplied OkLab+Alpha.
|
|
func (c Color) NOkLabA() (lightness, chromaA, chromaB, a float64) {
|
|
if c.A <= 0 {
|
|
return 0, 0, 0, 0
|
|
}
|
|
|
|
return c.Lightness, c.ChromaA, c.ChromaB, c.A
|
|
}
|
|
|
|
// Convert converts an arbitrary colour type to a non-premultiplied OkLab+Alpha [Color].
|
|
func Convert(c color.Color) Color {
|
|
if c, ok := c.(Color); ok {
|
|
return c
|
|
}
|
|
|
|
lightness, chromaA, chromaB, a := helper.ColorToNOkLabA(c)
|
|
return Color{lightness, chromaA, chromaB, a}
|
|
}
|
|
|
|
// A [color.Model] for converting arbitrary colours to a non-premultiplied OkLab+Alpha [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{}
|