package helper import ( "image/color" "testing" ) func eq[T comparable](a, b T) bool { return a == b } type nrgba64 struct { color.NRGBA64 } func (c *nrgba64) set(v color.NRGBA64) { c.NRGBA64 = v } func (c nrgba64) NRGBA() (_, _, _, _ uint32) { return uint32(c.R), uint32(c.G), uint32(c.B), uint32(c.A) } func (c nrgba64) NLRGBA() (_, _, _, _ float64) { return NRGBAtoNLRGBA(c.NRGBA()) } func (c nrgba64) NXYZA() (_, _, _, _ float64) { r, g, b, a := c.NLRGBA() x, y, z := LRGBtoXYZ(r, g, b) return x, y, z, a } func (c nrgba64) NOkLabA() (_, _, _, _ float64) { r, g, b, a := c.NLRGBA() lightness, chromaA, chromaB := LMStoOkLab(LRGBtoLMS(r, g, b)) return lightness, chromaA, chromaB, a } func convert[C Color, P interface { *C set(color.NRGBA64) }](c color.Color) C { var result C P(&result).set(color.NRGBA64Model.Convert(c).(color.NRGBA64)) return result } type nrgba64BadRGBA struct { nrgba64 } func (nrgba64BadRGBA) RGBA() (_, _, _, _ uint32) { return 1, 2, 3, 4 } type nrgba64BadNRGBA struct { nrgba64 } func (nrgba64BadNRGBA) NRGBA() (_, _, _, _ uint32) { return 1, 2, 3, 4 } type nrgba64BadNLRGBA struct { nrgba64 } func (nrgba64BadNLRGBA) NLRGBA() (_, _, _, _ float64) { return 1, 2, 3, 4 } type nrgba64BadNXYZA struct { nrgba64 } func (nrgba64BadNXYZA) NXYZA() (_, _, _, _ float64) { return 1, 2, 3, 4 } type nrgba64BadNOkLabA struct { nrgba64 } func (nrgba64BadNOkLabA) NOkLabA() (_, _, _, _ float64) { return 1, 2, 3, 4 } func TestTestModel(t *testing.T) { mt := mockTester{t: t} mt.run("wrong colour type", func(t *mockTest) { TestModel(t, true, color.RGBAModel, eq[nrgba64], nil) }) mt.run("bad RGBA", func(t *mockTest) { TestModel(t, false, Model(convert[nrgba64BadRGBA]), eq[nrgba64BadRGBA], nil) }) mt.run("bad NRGBA", func(t *mockTest) { TestModel(t, false, Model(convert[nrgba64BadNRGBA]), eq[nrgba64BadNRGBA], nil) }) mt.run("bad NLRGBA", func(t *mockTest) { TestModel(t, false, Model(convert[nrgba64BadNLRGBA]), eq[nrgba64BadNLRGBA], nil) }) mt.run("bad NXYZA", func(t *mockTest) { TestModel(t, false, Model(convert[nrgba64BadNXYZA]), eq[nrgba64BadNXYZA], nil) }) mt.run("bad NOkLabA", func(t *mockTest) { TestModel(t, false, Model(convert[nrgba64BadNOkLabA]), eq[nrgba64BadNOkLabA], nil) }) mt.run("working model", func(t *mockTest) { TestModel(t, true, Model(convert[nrgba64]), eq[nrgba64], []ConvertTest[nrgba64]{ {"good", color.NRGBA64{0x0123, 0x4567, 0x89ab, 0xcdef}, nrgba64{color.NRGBA64{0x0123, 0x4567, 0x89ab, 0xcdef}}}, {"bad", color.NRGBA64{0xcafe, 0xf00d, 0x54ac, 0xce55}, nrgba64{color.NRGBA64{0x0123, 0x4567, 0x89ab, 0xcdef}}}, }) }) mt.expectFailedChildren("wrong colour type") mt.expectError( "wrong colour type/legal colours", `model.Convert(color.RGBA64{R:0x0, G:0x0, B:0x0, A:0x0}) returned color.RGBA, expected helper.nrgba64`, ) mt.expectFailedChildren("bad RGBA") mt.expectError( "bad RGBA/legal colours", `helper.nrgba64BadRGBA{nrgba64:helper.nrgba64{NRGBA64:color.NRGBA64{R:0x0, G:0x0, B:0x0, A:0xffff}}}.RGBA() = {1 2 3 4}, want {0 0 0 65535}`, ) mt.expectFailedChildren("bad NRGBA") mt.expectError( "bad NRGBA/legal colours", `helper.nrgba64BadNRGBA{nrgba64:helper.nrgba64{NRGBA64:color.NRGBA64{R:0x0, G:0x0, B:0x0, A:0xffff}}}.NRGBA() = {1 2 3 4}, want {0 0 0 65535}`, ) mt.expectFailedChildren("bad NLRGBA") mt.expectError( "bad NLRGBA/legal colours", `helper.nrgba64BadNLRGBA{nrgba64:helper.nrgba64{NRGBA64:color.NRGBA64{R:0x0, G:0x0, B:0x0, A:0xffff}}}.NLRGBA() = [1 2 3 4], want [0 0 0 1]`, ) mt.expectFailedChildren("bad NXYZA") mt.expectError( "bad NXYZA/legal colours", `helper.nrgba64BadNXYZA{nrgba64:helper.nrgba64{NRGBA64:color.NRGBA64{R:0x0, G:0x0, B:0x0, A:0xffff}}}.NXYZA() = [1 2 3 4] want [0 0 0 1]`, ) mt.expectFailedChildren("bad NOkLabA") mt.expectError( "bad NOkLabA/legal colours", `helper.nrgba64BadNOkLabA{nrgba64:helper.nrgba64{NRGBA64:color.NRGBA64{R:0x0, G:0x0, B:0x0, A:0xffff}}}.NOkLabA()[:3] = [1 2 3 4] want [0 0 0 1]`, ) mt.expectFailedChildren("working model") mt.expectSuccess("working model/legal colours") mt.expectSuccess("working model/good") mt.expectError( "working model/bad", `model.Convert(color.NRGBA64{R:0xcafe, G:0xf00d, B:0x54ac, A:0xce55}) = helper.nrgba64{NRGBA64:color.NRGBA64{R:0xcafe, G:0xf00d, B:0x54ac, A:0xce55}}, want helper.nrgba64{NRGBA64:color.NRGBA64{R:0x123, G:0x4567, B:0x89ab, A:0xcdef}}`, ) mt.expectAllHandled() }