Initial commit.
This commit is contained in:
		
							
								
								
									
										79
									
								
								oklab/oklab.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										79
									
								
								oklab/oklab.go
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,79 @@ | ||||
| // Provides a [color.Color] type for dealing with OkLab colours without alpha. | ||||
| package oklab | ||||
|  | ||||
| import ( | ||||
| 	"image/color" | ||||
| 	"math" | ||||
|  | ||||
| 	"smariot.com/color/internal/helper" | ||||
| ) | ||||
|  | ||||
| // Color is an OkLab [color.Color]. | ||||
| type Color struct { | ||||
| 	Lightness, ChromaA, ChromaB float64 | ||||
| } | ||||
|  | ||||
| // DistanceSqr returns the euclidean distance squared between two colours. | ||||
| func DistanceSqr(a, b Color) float64 { | ||||
| 	dLightness := a.Lightness - b.Lightness | ||||
| 	dChromaA := a.ChromaA - b.ChromaA | ||||
| 	dChromaB := a.ChromaB - b.ChromaB | ||||
| 	return dLightness*dLightness + dChromaA*dChromaA + dChromaB*dChromaB | ||||
| } | ||||
|  | ||||
| // 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(helper.LMStoLRGB(helper.OkLabToLMS(c.Lightness, c.ChromaA, c.ChromaB))) | ||||
| 	a = 0xffff | ||||
| 	return | ||||
| } | ||||
|  | ||||
| // NRGBA converts to non-premultiplied RGBA. | ||||
| func (c Color) NRGBA() (r, g, b, a uint32) { | ||||
| 	r, g, b = helper.LRGBtoRGB(helper.LMStoLRGB(helper.OkLabToLMS(c.Lightness, c.ChromaA, c.ChromaB))) | ||||
| 	a = 0xffff | ||||
| 	return | ||||
| } | ||||
|  | ||||
| // NLRGBA converts to non-premultiplied linear RGBA. | ||||
| func (c Color) NLRGBA() (r, g, b, a float64) { | ||||
| 	r, g, b = helper.LMStoLRGB(helper.OkLabToLMS(c.Lightness, c.ChromaA, c.ChromaB)) | ||||
| 	a = 1 | ||||
| 	return | ||||
| } | ||||
|  | ||||
| // NXYZA converts to non-premultiplied XYZ+Alpha. | ||||
| func (c Color) NXYZA() (x, y, z, a float64) { | ||||
| 	x, y, z = helper.LMStoXYZ(helper.OkLabToLMS(c.Lightness, c.ChromaA, c.ChromaB)) | ||||
| 	return x, y, z, 1 | ||||
| } | ||||
|  | ||||
| // NOkLabA converts to non-premultiplied OkLab+Alpha. | ||||
| func (c Color) NOkLabA() (lightness, chromaA, chromaB, a float64) { | ||||
| 	return c.Lightness, c.ChromaA, c.ChromaB, 1 | ||||
| } | ||||
|  | ||||
| // Convert converts an arbitrary colour type to an OkLab [Color]. | ||||
| func Convert(c color.Color) Color { | ||||
| 	if c, ok := c.(Color); ok { | ||||
| 		return c | ||||
| 	} | ||||
|  | ||||
| 	lightness, chromaA, chromaB, _ := helper.ColorToNOkLabA(c) | ||||
| 	return Color{lightness, chromaA, chromaB} | ||||
| } | ||||
|  | ||||
| // A [color.Model] for converting arbitrary colours to an OkLab [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
									
								
								oklab/oklab_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								oklab/oklab_test.go
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,33 @@ | ||||
| package oklab | ||||
|  | ||||
| import ( | ||||
| 	"math" | ||||
| 	"testing" | ||||
|  | ||||
| 	"smariot.com/color/internal/helper" | ||||
| ) | ||||
|  | ||||
| func eq(c0, c1 Color) bool { | ||||
| 	return helper.EqFloat64SliceFuzzy( | ||||
| 		[]float64{c0.Lightness, c0.ChromaA, c0.ChromaB}, | ||||
| 		[]float64{c1.Lightness, c1.ChromaA, c1.ChromaB}, | ||||
| 	) | ||||
| } | ||||
|  | ||||
| func midpoint(c0, c1 Color) Color { | ||||
| 	return Color{(c0.Lightness + c1.Lightness) / 2, (c0.ChromaA + c1.ChromaA) / 2, (c0.ChromaB + c1.ChromaB) / 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