Added packages for linear grayscale colour varients.

This commit is contained in:
2025-03-27 09:17:35 -04:00
parent a0c520c3ed
commit 2410cce342
24 changed files with 654 additions and 106 deletions

123
lgraya/lgraya.go Normal file
View File

@ -0,0 +1,123 @@
// Provides a [color.Color] type for dealing with premultiplied linear grayscale+alpha colours.
package lgraya
import (
"image/color"
"math"
"smariot.com/color/internal/helper"
)
// Color is a pre-multiplied 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 - b.Y
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 - b.Y
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) {
if c.A <= 0 {
return 0, 0
}
return c.Y / c.A, 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) {
if c.A <= 0 {
return 0, 0, 0, 0
}
x, y, z = helper.LRGBtoXYZ(c.Y/c.A, c.Y/c.A, c.Y/c.A)
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
}
lightness, chromaA, chromaB = helper.LMStoOkLab(helper.LRGBtoLMS(c.Y/c.A, c.Y/c.A, c.Y/c.A))
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,
}
}
// 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{}

53
lgraya/lgraya_test.go Normal file
View File

@ -0,0 +1,53 @@
package lgraya
import (
"math"
"testing"
"smariot.com/color/internal/helper"
)
func eq(c0, c1 Color) bool {
return helper.EqFloat64SliceFuzzy(
[]float64{c0.Y, c0.A},
[]float64{c1.Y, c1.A},
)
}
func midpoint(c0, c1 Color) Color {
return Color{(c0.Y + c1.Y) / 2, (c0.A + c1.A) / 2}
}
func TestModel(t *testing.T) {
helper.TestModel(t, false, true, Model, eq, []helper.ConvertTest[Color]{
// These is a very illegal colour. If it makes it through
// unchanged, we can be reasonably confident no colour space conversions were
// attempted.
{
Name: "passthrough +inf",
In: Color{math.Inf(1), 0},
Out: Color{math.Inf(1), 0},
}, {
Name: "passthrough +inf",
In: Color{math.Inf(-1), math.NaN()},
Out: Color{math.Inf(-1), math.NaN()},
}, {
Name: "passthrough nan",
In: Color{math.NaN(), math.Inf(1)},
Out: Color{math.NaN(), math.Inf(1)},
},
})
}
func distance(a, b Color) float64 {
d := Distance(a, b)
dSqr := DistanceSqr(a, b)
if !helper.EqFloat64Fuzzy(d*d, dSqr) {
panic("Distance and DistanceSqr are not equivalent")
}
return d
}
func TestDistance(t *testing.T) {
helper.TestDistance(t, false, true, midpoint, distance, Model)
}