Initial commit.
This commit is contained in:
76
lrgb/lrgb.go
Normal file
76
lrgb/lrgb.go
Normal file
@ -0,0 +1,76 @@
|
||||
// Provides a [color.Color] type for dealing with linear RGB colours without alpha.
|
||||
package lrgb
|
||||
|
||||
import (
|
||||
"image/color"
|
||||
"math"
|
||||
|
||||
"smariot.com/color/internal/helper"
|
||||
)
|
||||
|
||||
// Color is a linear RGBA [color.Color].
|
||||
type Color struct {
|
||||
R, G, B float64
|
||||
}
|
||||
|
||||
// DistanceSqr returns the euclidean distance squared between two colours.
|
||||
func DistanceSqr(a, b Color) float64 {
|
||||
dR := a.R - b.R
|
||||
dG := a.G - b.G
|
||||
dB := a.B - b.B
|
||||
return dR*dR + dG*dG + dB*dB
|
||||
}
|
||||
|
||||
// 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.Sqrt(DistanceSqr(a, b))
|
||||
}
|
||||
|
||||
// RGBA converts to premultiplied RGBA, implementing the [color.Color] interface.
|
||||
func (c Color) RGBA() (r, g, b, a uint32) {
|
||||
_r, _g, _b := helper.LRGBtoRGB(c.R, c.G, c.B)
|
||||
return _r, _g, _b, 0xffff
|
||||
}
|
||||
|
||||
// NRGBA converts to non-premultiplied RGBA.
|
||||
func (c Color) NRGBA() (r, g, b, a uint32) {
|
||||
_r, _g, _b := helper.LRGBtoRGB(c.R, c.G, c.B)
|
||||
return _r, _g, _b, 0xffff
|
||||
}
|
||||
|
||||
// NLRGBA converts to non-premultiplied linear RGBA.
|
||||
func (c Color) NLRGBA() (r, g, b, a float64) {
|
||||
return c.R, c.G, c.B, 1
|
||||
}
|
||||
|
||||
// NXYZA converts to non-premultiplied XYZ+Alpha.
|
||||
func (c Color) NXYZA() (x, y, z, a float64) {
|
||||
x, y, z = helper.LRGBtoXYZ(c.R, c.G, c.B)
|
||||
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.R, c.G, c.B))
|
||||
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)
|
||||
return Color{r, g, b}
|
||||
}
|
||||
|
||||
// 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{}
|
33
lrgb/lrgb_test.go
Normal file
33
lrgb/lrgb_test.go
Normal file
@ -0,0 +1,33 @@
|
||||
package lrgb
|
||||
|
||||
import (
|
||||
"math"
|
||||
"testing"
|
||||
|
||||
"smariot.com/color/internal/helper"
|
||||
)
|
||||
|
||||
func eq(c0, c1 Color) bool {
|
||||
return helper.EqFloat64SliceFuzzy(
|
||||
[]float64{c0.R, c0.G, c0.B},
|
||||
[]float64{c1.R, c1.G, c1.B},
|
||||
)
|
||||
}
|
||||
|
||||
func midpoint(c0, c1 Color) Color {
|
||||
return Color{(c0.R + c1.R) / 2, (c0.G + c1.G) / 2, (c0.B + c1.B) / 2}
|
||||
}
|
||||
|
||||
func TestModel(t *testing.T) {
|
||||
helper.TestModel(t, false, Model, eq, []helper.ConvertTest[Color]{
|
||||
{
|
||||
Name: "passthrough",
|
||||
In: Color{math.Inf(1), math.Inf(-1), math.NaN()},
|
||||
Out: Color{math.Inf(1), math.Inf(-1), math.NaN()},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestDistance(t *testing.T) {
|
||||
helper.TestDistance(t, false, midpoint, Distance, Model)
|
||||
}
|
Reference in New Issue
Block a user