Initial commit.
This commit is contained in:
		
							
								
								
									
										81
									
								
								nlrgba/nlrgba.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										81
									
								
								nlrgba/nlrgba.go
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,81 @@ | ||||
| // Provides a [color.Color] type for dealing with non-premultiplied linear RGBA colours. | ||||
| package nlrgba | ||||
|  | ||||
| import ( | ||||
| 	"image/color" | ||||
| 	"math" | ||||
|  | ||||
| 	"smariot.com/color/internal/helper" | ||||
| ) | ||||
|  | ||||
| // Color is a non-premultiplied linear RGBA [color.Color]. | ||||
| type Color struct { | ||||
| 	R, G, B, 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 { | ||||
| 	dR := a.R*a.A - b.R*b.A | ||||
| 	dG := a.G*a.A - b.G*b.A | ||||
| 	dB := a.B*a.A - b.B*b.A | ||||
| 	dA := a.A - b.A | ||||
| 	return max(sqr(dR), sqr(dR+dA)) + max(sqr(dG), sqr(dG+dA)) + max(sqr(dB), sqr(dB+dA)) | ||||
| } | ||||
|  | ||||
| // Distance returns the maximum possible euclidean distance between two colours, | ||||
| // accounting for the possible backgrounds they might be composited over. | ||||
| // | ||||
| // 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) { | ||||
| 	return helper.NLRGBAtoRGBA(c.NLRGBA()) | ||||
| } | ||||
|  | ||||
| // NRGBA converts to non-premultiplied RGBA. | ||||
| func (c Color) NRGBA() (r, g, b, a uint32) { | ||||
| 	return helper.NLRGBAtoNRGBA(c.NLRGBA()) | ||||
| } | ||||
|  | ||||
| // NLRGBA converts to non-premultiplied linear RGBA. | ||||
| func (c Color) NLRGBA() (r, g, b, a float64) { | ||||
| 	return c.R, c.G, c.B, c.A | ||||
| } | ||||
|  | ||||
| // 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, c.A | ||||
| } | ||||
|  | ||||
| // 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, c.A | ||||
| } | ||||
|  | ||||
| // Convert converts an arbitrary colour type to a linear RGBA [Color]. | ||||
| func Convert(c color.Color) Color { | ||||
| 	if c, ok := c.(Color); ok { | ||||
| 		return c | ||||
| 	} | ||||
|  | ||||
| 	r, g, b, a := helper.ColorToNLRGBA(c) | ||||
| 	return Color{r, g, b, a} | ||||
| } | ||||
|  | ||||
| // A [color.Model] for converting arbitrary colours to a non-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{} | ||||
							
								
								
									
										36
									
								
								nlrgba/nlrgba_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								nlrgba/nlrgba_test.go
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,36 @@ | ||||
| package nlrgba | ||||
|  | ||||
| import ( | ||||
| 	"math" | ||||
| 	"testing" | ||||
|  | ||||
| 	"smariot.com/color/internal/helper" | ||||
| ) | ||||
|  | ||||
| func eq(c0, c1 Color) bool { | ||||
| 	return helper.EqFloat64SliceFuzzy( | ||||
| 		[]float64{c0.R, c0.G, c0.B, c0.A}, | ||||
| 		[]float64{c1.R, c1.G, c1.B, c1.A}, | ||||
| 	) | ||||
| } | ||||
|  | ||||
| func midpoint(c0, c1 Color) Color { | ||||
| 	return Color{(c0.R + c1.R) / 2, (c0.G + c1.G) / 2, (c0.B + c1.B) / 2, (c0.A + c1.A) / 2} | ||||
| } | ||||
|  | ||||
| func TestModel(t *testing.T) { | ||||
| 	helper.TestModel(t, true, Model, eq, []helper.ConvertTest[Color]{ | ||||
| 		{ | ||||
| 			Name: "passthrough", | ||||
| 			// These is a very illegal colour. If it makes it through | ||||
| 			// unchanged, we can be reasonably confident no colour space conversions were | ||||
| 			// attempted. | ||||
| 			In:  Color{math.Inf(1), math.Inf(-1), math.NaN(), 0}, | ||||
| 			Out: Color{math.Inf(1), math.Inf(-1), math.NaN(), 0}, | ||||
| 		}, | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| func TestDistance(t *testing.T) { | ||||
| 	helper.TestDistance(t, true, midpoint, Distance, Model) | ||||
| } | ||||
		Reference in New Issue
	
	Block a user