color/lgray/lgray.go

83 lines
2.3 KiB
Go

// Provides a [color.Color] type for dealing with linear grayscale colours without alpha.
package lgray
import (
"image/color"
"math"
"smariot.com/color/internal/helper"
)
// Color is a linear grayscale [color.Color].
type Color struct {
Y float64
}
// DistanceSqr returns the euclidean distance squared between two colours.
func DistanceSqr(a, b Color) float64 {
dY := a.Y - b.Y
return dY * dY
}
// 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.Abs(a.Y - b.Y)
}
// RGBA converts to premultiplied RGBA, implementing the [color.Color] interface.
func (c Color) RGBA() (r, g, b, a uint32) {
_y := helper.Delinearize(c.Y)
return _y, _y, _y, 0xffff
}
// NRGBA converts to non-premultiplied RGBA.
func (c Color) NRGBA() (r, g, b, a uint32) {
_y := helper.Delinearize(c.Y)
return _y, _y, _y, 0xffff
}
// NLRGBA converts to non-premultiplied linear RGBA.
func (c Color) NLRGBA() (r, g, b, a float64) {
return c.Y, c.Y, c.Y, 1
}
// 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, 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.Y, c.Y, c.Y))
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)
// 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 [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{}