mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-25 03:08:54 +01:00
internal/affine: Add coloMImpl interface and colorMImplScale
Closes #1658
This commit is contained in:
parent
7e5b259dd2
commit
5f03f4f195
@ -45,7 +45,21 @@ var (
|
|||||||
//
|
//
|
||||||
// The nil and initial value is identity.
|
// The nil and initial value is identity.
|
||||||
type ColorM struct {
|
type ColorM struct {
|
||||||
impl *colorMImplBodyTranslate
|
impl colorMImpl
|
||||||
|
}
|
||||||
|
|
||||||
|
type colorMImpl interface {
|
||||||
|
IsIdentity() bool
|
||||||
|
ScaleOnly() bool
|
||||||
|
UnsafeScaleElements() *[4]float32
|
||||||
|
UnsafeElements() (*[16]float32, *[4]float32)
|
||||||
|
Apply(r, g, b, a float32) color.Color
|
||||||
|
IsInvertible() bool
|
||||||
|
Invert() *ColorM
|
||||||
|
Equals(other *ColorM) bool
|
||||||
|
Concat(other *ColorM) *ColorM
|
||||||
|
Scale(r, g, b, a float32) *ColorM
|
||||||
|
Translate(r, g, b, a float32) *ColorM
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ColorM) String() string {
|
func (c *ColorM) String() string {
|
||||||
@ -57,6 +71,10 @@ func (c *ColorM) String() string {
|
|||||||
b[3], b[7], b[11], b[15], t[3])
|
b[3], b[7], b[11], b[15], t[3])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type colorMImplScale struct {
|
||||||
|
scale [4]float32
|
||||||
|
}
|
||||||
|
|
||||||
type colorMImplBodyTranslate struct {
|
type colorMImplBodyTranslate struct {
|
||||||
body [16]float32
|
body [16]float32
|
||||||
translate [4]float32
|
translate [4]float32
|
||||||
@ -82,6 +100,10 @@ func (c *ColorM) isIdentity() bool {
|
|||||||
return c.impl.IsIdentity()
|
return c.impl.IsIdentity()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *colorMImplScale) IsIdentity() bool {
|
||||||
|
return c.scale == [4]float32{1, 1, 1, 1}
|
||||||
|
}
|
||||||
|
|
||||||
func (c *colorMImplBodyTranslate) IsIdentity() bool {
|
func (c *colorMImplBodyTranslate) IsIdentity() bool {
|
||||||
return c.body == colorMIdentityBody && c.translate == colorMIdentityTranslate
|
return c.body == colorMIdentityBody && c.translate == colorMIdentityTranslate
|
||||||
}
|
}
|
||||||
@ -93,6 +115,10 @@ func (c *ColorM) ScaleOnly() bool {
|
|||||||
return c.impl.ScaleOnly()
|
return c.impl.ScaleOnly()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *colorMImplScale) ScaleOnly() bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
func (c *colorMImplBodyTranslate) ScaleOnly() bool {
|
func (c *colorMImplBodyTranslate) ScaleOnly() bool {
|
||||||
if c.body[1] != 0 {
|
if c.body[1] != 0 {
|
||||||
return false
|
return false
|
||||||
@ -138,14 +164,19 @@ func (c *colorMImplBodyTranslate) ScaleOnly() bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *colorMImplScale) UnsafeScaleElements() *[4]float32 {
|
||||||
|
return &c.scale
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *colorMImplBodyTranslate) UnsafeScaleElements() *[4]float32 {
|
||||||
|
return &[...]float32{c.body[0], c.body[5], c.body[10], c.body[15]}
|
||||||
|
}
|
||||||
|
|
||||||
func (c *ColorM) Apply(clr color.Color) color.Color {
|
func (c *ColorM) Apply(clr color.Color) color.Color {
|
||||||
if c.isIdentity() {
|
if c.isIdentity() {
|
||||||
return clr
|
return clr
|
||||||
}
|
}
|
||||||
return c.impl.Apply(clr)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *colorMImplBodyTranslate) Apply(clr color.Color) color.Color {
|
|
||||||
r, g, b, a := clr.RGBA()
|
r, g, b, a := clr.RGBA()
|
||||||
rf, gf, bf, af := float32(0.0), float32(0.0), float32(0.0), float32(0.0)
|
rf, gf, bf, af := float32(0.0), float32(0.0), float32(0.0), float32(0.0)
|
||||||
// Unmultiply alpha
|
// Unmultiply alpha
|
||||||
@ -155,6 +186,27 @@ func (c *colorMImplBodyTranslate) Apply(clr color.Color) color.Color {
|
|||||||
bf = float32(b) / float32(a)
|
bf = float32(b) / float32(a)
|
||||||
af = float32(a) / 0xffff
|
af = float32(a) / 0xffff
|
||||||
}
|
}
|
||||||
|
return c.impl.Apply(rf, gf, bf, af)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *colorMImplScale) Apply(rf, gf, bf, af float32) color.Color {
|
||||||
|
rf *= c.scale[0]
|
||||||
|
gf *= c.scale[1]
|
||||||
|
bf *= c.scale[2]
|
||||||
|
af *= c.scale[3]
|
||||||
|
rf = clamp(rf)
|
||||||
|
gf = clamp(gf)
|
||||||
|
bf = clamp(bf)
|
||||||
|
af = clamp(af)
|
||||||
|
return color.NRGBA64{
|
||||||
|
R: uint16(rf * 0xffff),
|
||||||
|
G: uint16(gf * 0xffff),
|
||||||
|
B: uint16(bf * 0xffff),
|
||||||
|
A: uint16(af * 0xffff),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *colorMImplBodyTranslate) Apply(rf, gf, bf, af float32) color.Color {
|
||||||
eb := &c.body
|
eb := &c.body
|
||||||
et := &c.translate
|
et := &c.translate
|
||||||
rf2 := eb[0]*rf + eb[4]*gf + eb[8]*bf + eb[12]*af + et[0]
|
rf2 := eb[0]*rf + eb[4]*gf + eb[8]*bf + eb[12]*af + et[0]
|
||||||
@ -180,15 +232,17 @@ func (c *ColorM) UnsafeElements() (*[16]float32, *[4]float32) {
|
|||||||
return c.impl.UnsafeElements()
|
return c.impl.UnsafeElements()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *colorMImplBodyTranslate) UnsafeElements() (*[16]float32, *[4]float32) {
|
func (c *colorMImplScale) UnsafeElements() (*[16]float32, *[4]float32) {
|
||||||
return &c.body, &c.translate
|
return &[...]float32{
|
||||||
|
c.scale[0], 0, 0, 0,
|
||||||
|
0, c.scale[1], 0, 0,
|
||||||
|
0, 0, c.scale[2], 0,
|
||||||
|
0, 0, 0, c.scale[3],
|
||||||
|
}, &colorMIdentityTranslate
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ColorM) det() float32 {
|
func (c *colorMImplBodyTranslate) UnsafeElements() (*[16]float32, *[4]float32) {
|
||||||
if c.isIdentity() {
|
return &c.body, &c.translate
|
||||||
return 1
|
|
||||||
}
|
|
||||||
return c.impl.det()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *colorMImplBodyTranslate) det() float32 {
|
func (c *colorMImplBodyTranslate) det() float32 {
|
||||||
@ -225,6 +279,17 @@ func (c *colorMImplBodyTranslate) det() float32 {
|
|||||||
// IsInvertible returns a boolean value indicating
|
// IsInvertible returns a boolean value indicating
|
||||||
// whether the matrix c is invertible or not.
|
// whether the matrix c is invertible or not.
|
||||||
func (c *ColorM) IsInvertible() bool {
|
func (c *ColorM) IsInvertible() bool {
|
||||||
|
if c.isIdentity() {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return c.impl.IsInvertible()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *colorMImplScale) IsInvertible() bool {
|
||||||
|
return c.scale[0] != 0 && c.scale[1] != 0 && c.scale[2] != 0 && c.scale[3] != 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *colorMImplBodyTranslate) IsInvertible() bool {
|
||||||
return c.det() != 0
|
return c.det() != 0
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -237,6 +302,19 @@ func (c *ColorM) Invert() *ColorM {
|
|||||||
return c.impl.Invert()
|
return c.impl.Invert()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *colorMImplScale) Invert() *ColorM {
|
||||||
|
return &ColorM{
|
||||||
|
impl: &colorMImplScale{
|
||||||
|
scale: [4]float32{
|
||||||
|
1 / c.scale[0],
|
||||||
|
1 / c.scale[1],
|
||||||
|
1 / c.scale[2],
|
||||||
|
1 / c.scale[3],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (c *colorMImplBodyTranslate) Invert() *ColorM {
|
func (c *colorMImplBodyTranslate) Invert() *ColorM {
|
||||||
det := c.det()
|
det := c.det()
|
||||||
if det == 0 {
|
if det == 0 {
|
||||||
@ -359,7 +437,9 @@ func (c *ColorM) SetElement(i, j int, element float32) *ColorM {
|
|||||||
body: colorMIdentityBody,
|
body: colorMIdentityBody,
|
||||||
}
|
}
|
||||||
if !c.isIdentity() {
|
if !c.isIdentity() {
|
||||||
*newImpl = *c.impl
|
b, t := c.impl.UnsafeElements()
|
||||||
|
newImpl.body = *b
|
||||||
|
newImpl.translate = *t
|
||||||
}
|
}
|
||||||
if j < (ColorMDim - 1) {
|
if j < (ColorMDim - 1) {
|
||||||
newImpl.body[i+j*(ColorMDim-1)] = element
|
newImpl.body[i+j*(ColorMDim-1)] = element
|
||||||
@ -381,9 +461,20 @@ func (c *ColorM) Equals(other *ColorM) bool {
|
|||||||
return c.impl.Equals(other)
|
return c.impl.Equals(other)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *colorMImplScale) Equals(other *ColorM) bool {
|
||||||
|
if !other.impl.ScaleOnly() {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for i, s := range other.impl.UnsafeScaleElements() {
|
||||||
|
if c.scale[i] != s {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
func (c *colorMImplBodyTranslate) Equals(other *ColorM) bool {
|
func (c *colorMImplBodyTranslate) Equals(other *ColorM) bool {
|
||||||
lhsb := &other.impl.body
|
lhsb, lhst := other.impl.UnsafeElements()
|
||||||
lhst := &other.impl.translate
|
|
||||||
rhsb := &c.body
|
rhsb := &c.body
|
||||||
rhst := &c.translate
|
rhst := &c.translate
|
||||||
return *lhsb == *rhsb && *lhst == *rhst
|
return *lhsb == *rhsb && *lhst == *rhst
|
||||||
@ -401,9 +492,29 @@ func (c *ColorM) Concat(other *ColorM) *ColorM {
|
|||||||
return c.impl.Concat(other)
|
return c.impl.Concat(other)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *colorMImplScale) Concat(other *ColorM) *ColorM {
|
||||||
|
if other.ScaleOnly() {
|
||||||
|
s := other.impl.UnsafeScaleElements()
|
||||||
|
return c.Scale(s[0], s[1], s[2], s[3])
|
||||||
|
}
|
||||||
|
|
||||||
|
lhsb, lhst := other.impl.UnsafeElements()
|
||||||
|
s := &c.scale
|
||||||
|
return &ColorM{
|
||||||
|
impl: &colorMImplBodyTranslate{
|
||||||
|
body: [...]float32{
|
||||||
|
lhsb[0] * s[0], lhsb[1] * s[0], lhsb[2] * s[0], lhsb[3] * s[0],
|
||||||
|
lhsb[4] * s[1], lhsb[5] * s[1], lhsb[6] * s[1], lhsb[7] * s[1],
|
||||||
|
lhsb[8] * s[2], lhsb[9] * s[2], lhsb[10] * s[2], lhsb[11] * s[2],
|
||||||
|
lhsb[12] * s[3], lhsb[13] * s[3], lhsb[14] * s[3], lhsb[15] * s[3],
|
||||||
|
},
|
||||||
|
translate: *lhst,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (c *colorMImplBodyTranslate) Concat(other *ColorM) *ColorM {
|
func (c *colorMImplBodyTranslate) Concat(other *ColorM) *ColorM {
|
||||||
lhsb := &other.impl.body
|
lhsb, lhst := other.impl.UnsafeElements()
|
||||||
lhst := &other.impl.translate
|
|
||||||
rhsb := &c.body
|
rhsb := &c.body
|
||||||
rhst := &c.translate
|
rhst := &c.translate
|
||||||
|
|
||||||
@ -428,11 +539,25 @@ func (c *ColorM) Scale(r, g, b, a float32) *ColorM {
|
|||||||
return getCachedScalingColorM(r, g, b, a)
|
return getCachedScalingColorM(r, g, b, a)
|
||||||
}
|
}
|
||||||
if c.ScaleOnly() {
|
if c.ScaleOnly() {
|
||||||
return getCachedScalingColorM(r*c.impl.body[0], g*c.impl.body[5], b*c.impl.body[10], a*c.impl.body[15])
|
s := c.impl.UnsafeScaleElements()
|
||||||
|
return getCachedScalingColorM(r*s[0], g*s[1], b*s[2], a*s[3])
|
||||||
}
|
}
|
||||||
return c.impl.Scale(r, g, b, a)
|
return c.impl.Scale(r, g, b, a)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *colorMImplScale) Scale(r, g, b, a float32) *ColorM {
|
||||||
|
return &ColorM{
|
||||||
|
impl: &colorMImplScale{
|
||||||
|
scale: [...]float32{
|
||||||
|
c.scale[0] * r,
|
||||||
|
c.scale[1] * g,
|
||||||
|
c.scale[2] * b,
|
||||||
|
c.scale[3] * a,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (c *colorMImplBodyTranslate) Scale(r, g, b, a float32) *ColorM {
|
func (c *colorMImplBodyTranslate) Scale(r, g, b, a float32) *ColorM {
|
||||||
eb := c.body
|
eb := c.body
|
||||||
for i := 0; i < ColorMDim-1; i++ {
|
for i := 0; i < ColorMDim-1; i++ {
|
||||||
@ -470,6 +595,20 @@ func (c *ColorM) Translate(r, g, b, a float32) *ColorM {
|
|||||||
return c.impl.Translate(r, g, b, a)
|
return c.impl.Translate(r, g, b, a)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *colorMImplScale) Translate(r, g, b, a float32) *ColorM {
|
||||||
|
return &ColorM{
|
||||||
|
impl: &colorMImplBodyTranslate{
|
||||||
|
body: [...]float32{
|
||||||
|
c.scale[0], 0, 0, 0,
|
||||||
|
0, c.scale[1], 0, 0,
|
||||||
|
0, 0, c.scale[2], 0,
|
||||||
|
0, 0, 0, c.scale[3],
|
||||||
|
},
|
||||||
|
translate: [...]float32{r, g, b, a},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (c *colorMImplBodyTranslate) Translate(r, g, b, a float32) *ColorM {
|
func (c *colorMImplBodyTranslate) Translate(r, g, b, a float32) *ColorM {
|
||||||
es := c.translate
|
es := c.translate
|
||||||
es[0] += r
|
es[0] += r
|
||||||
@ -584,13 +723,8 @@ func getCachedScalingColorM(r, g, b, a float32) *ColorM {
|
|||||||
|
|
||||||
v := &cachedScalingColorMValue{
|
v := &cachedScalingColorMValue{
|
||||||
c: &ColorM{
|
c: &ColorM{
|
||||||
impl: &colorMImplBodyTranslate{
|
impl: &colorMImplScale{
|
||||||
body: [...]float32{
|
scale: [...]float32{r, g, b, a},
|
||||||
r, 0, 0, 0,
|
|
||||||
0, g, 0, 0,
|
|
||||||
0, 0, b, 0,
|
|
||||||
0, 0, 0, a,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
atime: now,
|
atime: now,
|
||||||
|
@ -192,6 +192,29 @@ func TestColorMInvert(t *testing.T) {
|
|||||||
In: nil,
|
In: nil,
|
||||||
Out: nil,
|
Out: nil,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
In: arrayToColorM([4][5]float32{
|
||||||
|
{1, 0, 0, 0, 0},
|
||||||
|
{0, 1, 0, 0, 0},
|
||||||
|
{0, 0, 1, 0, 0},
|
||||||
|
{0, 0, 0, 1, 0},
|
||||||
|
}),
|
||||||
|
Out: arrayToColorM([4][5]float32{
|
||||||
|
{1, 0, 0, 0, 0},
|
||||||
|
{0, 1, 0, 0, 0},
|
||||||
|
{0, 0, 1, 0, 0},
|
||||||
|
{0, 0, 0, 1, 0},
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
In: (*ColorM)(nil).Scale(1, 2, 4, 8),
|
||||||
|
Out: arrayToColorM([4][5]float32{
|
||||||
|
{1, 0, 0, 0, 0},
|
||||||
|
{0, 0.5, 0, 0, 0},
|
||||||
|
{0, 0, 0.25, 0, 0},
|
||||||
|
{0, 0, 0, 0.125, 0},
|
||||||
|
}),
|
||||||
|
},
|
||||||
{
|
{
|
||||||
In: arrayToColorM([4][5]float32{
|
In: arrayToColorM([4][5]float32{
|
||||||
{1, 0, 0, 0, 0},
|
{1, 0, 0, 0, 0},
|
||||||
@ -248,3 +271,64 @@ func BenchmarkColorMInvert(b *testing.B) {
|
|||||||
m = m.Invert()
|
m = m.Invert()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestColorMConcat(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
In0 *ColorM
|
||||||
|
In1 *ColorM
|
||||||
|
Out *ColorM
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
(*ColorM)(nil).Scale(1, 2, 3, 4),
|
||||||
|
(*ColorM)(nil).Scale(5, 6, 7, 8),
|
||||||
|
(*ColorM)(nil).Scale(5, 12, 21, 32),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
(*ColorM)(nil).Scale(5, 6, 7, 8),
|
||||||
|
(*ColorM)(nil).Scale(1, 2, 3, 4),
|
||||||
|
(*ColorM)(nil).Scale(5, 12, 21, 32),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
arrayToColorM([4][5]float32{
|
||||||
|
{1, 2, 3, 4, 5},
|
||||||
|
{5, 1, 2, 3, 4},
|
||||||
|
{4, 5, 1, 2, 3},
|
||||||
|
{3, 4, 5, 1, 2},
|
||||||
|
}),
|
||||||
|
(*ColorM)(nil).Scale(1, 2, 3, 4),
|
||||||
|
arrayToColorM([4][5]float32{
|
||||||
|
{1, 2, 3, 4, 5},
|
||||||
|
{10, 2, 4, 6, 8},
|
||||||
|
{12, 15, 3, 6, 9},
|
||||||
|
{12, 16, 20, 4, 8},
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
(*ColorM)(nil).Scale(1, 2, 3, 4),
|
||||||
|
arrayToColorM([4][5]float32{
|
||||||
|
{1, 2, 3, 4, 5},
|
||||||
|
{5, 1, 2, 3, 4},
|
||||||
|
{4, 5, 1, 2, 3},
|
||||||
|
{3, 4, 5, 1, 2},
|
||||||
|
}),
|
||||||
|
arrayToColorM([4][5]float32{
|
||||||
|
{1, 4, 9, 16, 5},
|
||||||
|
{5, 2, 6, 12, 4},
|
||||||
|
{4, 10, 3, 8, 3},
|
||||||
|
{3, 8, 15, 4, 2},
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, c := range cases {
|
||||||
|
got := c.In0.Concat(c.In1)
|
||||||
|
want := c.Out
|
||||||
|
if !equalWithDelta(got, want, 1e-6) {
|
||||||
|
t.Errorf("%s.Concat(%s): got: %v, want: %v", c.In0, c.In1, got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user