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

82
lgray/lgray.go Normal file
View File

@ -0,0 +1,82 @@
// 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{}

47
lgray/lgray_test.go Normal file
View File

@ -0,0 +1,47 @@
package lgray
import (
"math"
"testing"
"smariot.com/color/internal/helper"
)
func eq(c0, c1 Color) bool {
return helper.EqFloat64Fuzzy(c0.Y, c1.Y)
}
func midpoint(c0, c1 Color) Color {
return Color{(c0.Y + c1.Y) / 2}
}
func TestModel(t *testing.T) {
helper.TestModel(t, false, false, Model, eq, []helper.ConvertTest[Color]{
{
Name: "passthrough +inf",
In: Color{math.Inf(1)},
Out: Color{math.Inf(1)},
}, {
Name: "passthrough -inf",
In: Color{math.Inf(-1)},
Out: Color{math.Inf(-1)},
}, {
Name: "passthrough nan",
In: Color{math.NaN()},
Out: Color{math.NaN()},
},
})
}
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, false, midpoint, distance, Model)
}