diff --git a/colormatrix.go b/colormatrix.go index e04bda607..37b988097 100644 --- a/colormatrix.go +++ b/colormatrix.go @@ -30,12 +30,12 @@ const ColorMatrixDim = 5 // // The initial value is identity. type ColorMatrix struct { - // es represents elements. - es *[ColorMatrixDim - 1][ColorMatrixDim]float64 + initialized bool + es [ColorMatrixDim - 1][ColorMatrixDim]float64 } -func colorMatrixEsI() *[ColorMatrixDim - 1][ColorMatrixDim]float64 { - return &[ColorMatrixDim - 1][ColorMatrixDim]float64{ +func colorMatrixEsI() [ColorMatrixDim - 1][ColorMatrixDim]float64 { + return [ColorMatrixDim - 1][ColorMatrixDim]float64{ {1, 0, 0, 0, 0}, {0, 1, 0, 0, 0}, {0, 0, 1, 0, 0}, @@ -49,7 +49,7 @@ func (c ColorMatrix) dim() int { // Element returns a value of a matrix at (i, j). func (c ColorMatrix) Element(i, j int) float64 { - if c.es == nil { + if !c.initialized { if i == j { return 1 } @@ -60,8 +60,9 @@ func (c ColorMatrix) Element(i, j int) float64 { // Concat multiplies a color matrix with the other color matrix. func (c *ColorMatrix) Concat(other ColorMatrix) { - if c.es == nil { + if !c.initialized { c.es = colorMatrixEsI() + c.initialized = true } result := ColorMatrix{} mul(&other, c, &result) @@ -70,8 +71,9 @@ func (c *ColorMatrix) Concat(other ColorMatrix) { // Add adds a color matrix with the other color matrix. func (c *ColorMatrix) Add(other ColorMatrix) { - if c.es == nil { + if !c.initialized { c.es = colorMatrixEsI() + c.initialized = true } result := ColorMatrix{} add(&other, c, &result) @@ -80,8 +82,9 @@ func (c *ColorMatrix) Add(other ColorMatrix) { // SetElement sets an element at (i, j). func (c *ColorMatrix) SetElement(i, j int, element float64) { - if c.es == nil { + if !c.initialized { c.es = colorMatrixEsI() + c.initialized = true } c.es[i][j] = element } @@ -92,7 +95,8 @@ func Monochrome() ColorMatrix { const g = 23434.0 / 32768.0 const b = 2366.0 / 32768.0 return ColorMatrix{ - &[ColorMatrixDim - 1][ColorMatrixDim]float64{ + initialized: true, + es: [ColorMatrixDim - 1][ColorMatrixDim]float64{ {r, g, b, 0, 0}, {r, g, b, 0, 0}, {r, g, b, 0, 0}, @@ -104,7 +108,8 @@ func Monochrome() ColorMatrix { // ScaleColor returns a color matrix that scales a color matrix by the given color (r, g, b, a). func ScaleColor(r, g, b, a float64) ColorMatrix { return ColorMatrix{ - &[ColorMatrixDim - 1][ColorMatrixDim]float64{ + initialized: true, + es: [ColorMatrixDim - 1][ColorMatrixDim]float64{ {r, 0, 0, 0, 0}, {0, g, 0, 0, 0}, {0, 0, b, 0, 0}, @@ -116,7 +121,8 @@ func ScaleColor(r, g, b, a float64) ColorMatrix { // TranslateColor returns a color matrix that translates a color matrix by the given color (r, g, b, a). func TranslateColor(r, g, b, a float64) ColorMatrix { return ColorMatrix{ - &[ColorMatrixDim - 1][ColorMatrixDim]float64{ + initialized: true, + es: [ColorMatrixDim - 1][ColorMatrixDim]float64{ {1, 0, 0, 0, r}, {0, 1, 0, 0, g}, {0, 0, 1, 0, b}, @@ -132,7 +138,8 @@ func RotateHue(theta float64) ColorMatrix { v2 := (1.0/3.0)*(1.0-cos) - math.Sqrt(1.0/3.0)*sin v3 := (1.0/3.0)*(1.0-cos) + math.Sqrt(1.0/3.0)*sin return ColorMatrix{ - &[ColorMatrixDim - 1][ColorMatrixDim]float64{ + initialized: true, + es: [ColorMatrixDim - 1][ColorMatrixDim]float64{ {v1, v2, v3, 0, 0}, {v3, v1, v2, 0, 0}, {v2, v3, v1, 0, 0}, diff --git a/colormatrix_test.go b/colormatrix_test.go index 701726f54..a3f9d316c 100644 --- a/colormatrix_test.go +++ b/colormatrix_test.go @@ -48,3 +48,14 @@ func TestColorInit(t *testing.T) { } } } + +func TestColorAssign(t *testing.T) { + m := ScaleColor(1, 1, 1, 1) // Create elements explicitly + m2 := m + m.SetElement(0, 0, 0) + got := m2.Element(0, 0) + want := 1.0 + if want != got { + t.Errorf("m2.Element(%d, %d) = %f, want %f", 0, 0, got, want) + } +} diff --git a/geometrymatrix.go b/geometrymatrix.go index a54d72c8b..fc2a75de5 100644 --- a/geometrymatrix.go +++ b/geometrymatrix.go @@ -25,12 +25,12 @@ const GeometryMatrixDim = 3 // // The initial value is identity. type GeometryMatrix struct { - // es represents elements. - es *[GeometryMatrixDim - 1][GeometryMatrixDim]float64 + initialized bool + es [GeometryMatrixDim - 1][GeometryMatrixDim]float64 } -func geometryMatrixEsI() *[GeometryMatrixDim - 1][GeometryMatrixDim]float64 { - return &[GeometryMatrixDim - 1][GeometryMatrixDim]float64{ +func geometryMatrixEsI() [GeometryMatrixDim - 1][GeometryMatrixDim]float64 { + return [GeometryMatrixDim - 1][GeometryMatrixDim]float64{ {1, 0, 0}, {0, 1, 0}, } @@ -42,7 +42,7 @@ func (g GeometryMatrix) dim() int { // Element returns a value of a matrix at (i, j). func (g GeometryMatrix) Element(i, j int) float64 { - if g.es == nil { + if !g.initialized { if i == j { return 1 } @@ -53,8 +53,9 @@ func (g GeometryMatrix) Element(i, j int) float64 { // Concat multiplies a geometry matrix with the other geometry matrix. func (g *GeometryMatrix) Concat(other GeometryMatrix) { - if g.es == nil { + if !g.initialized { g.es = geometryMatrixEsI() + g.initialized = true } result := GeometryMatrix{} mul(&other, g, &result) @@ -63,8 +64,9 @@ func (g *GeometryMatrix) Concat(other GeometryMatrix) { // Add adds a geometry matrix with the other geometry matrix. func (g *GeometryMatrix) Add(other GeometryMatrix) { - if g.es == nil { + if !g.initialized { g.es = geometryMatrixEsI() + g.initialized = true } result := GeometryMatrix{} add(&other, g, &result) @@ -73,8 +75,9 @@ func (g *GeometryMatrix) Add(other GeometryMatrix) { // SetElement sets an element at (i, j). func (g *GeometryMatrix) SetElement(i, j int, element float64) { - if g.es == nil { + if !g.initialized { g.es = geometryMatrixEsI() + g.initialized = true } g.es[i][j] = element } @@ -82,7 +85,8 @@ func (g *GeometryMatrix) SetElement(i, j int, element float64) { // ScaleGeometry returns a matrix that scales a geometry matrix by (x, y). func ScaleGeometry(x, y float64) GeometryMatrix { return GeometryMatrix{ - es: &[2][3]float64{ + initialized: true, + es: [2][3]float64{ {x, 0, 0}, {0, y, 0}, }, @@ -92,7 +96,8 @@ func ScaleGeometry(x, y float64) GeometryMatrix { // TranslateGeometry returns a matrix taht translates a geometry matrix by (tx, ty). func TranslateGeometry(tx, ty float64) GeometryMatrix { return GeometryMatrix{ - es: &[2][3]float64{ + initialized: true, + es: [2][3]float64{ {1, 0, tx}, {0, 1, ty}, }, @@ -103,7 +108,8 @@ func TranslateGeometry(tx, ty float64) GeometryMatrix { func RotateGeometry(theta float64) GeometryMatrix { sin, cos := math.Sincos(theta) return GeometryMatrix{ - es: &[2][3]float64{ + initialized: true, + es: [2][3]float64{ {cos, -sin, 0}, {sin, cos, 0}, }, diff --git a/geometrymatrix_test.go b/geometrymatrix_test.go index 60be8740e..d90e8d7f5 100644 --- a/geometrymatrix_test.go +++ b/geometrymatrix_test.go @@ -49,6 +49,17 @@ func TestGeometryInit(t *testing.T) { } } +func TestGeometryAssign(t *testing.T) { + m := ScaleGeometry(1, 1) // Create elements explicitly + m2 := m + m.SetElement(0, 0, 0) + got := m2.Element(0, 0) + want := 1.0 + if want != got { + t.Errorf("m2.Element(%d, %d) = %f, want %f", 0, 0, got, want) + } +} + func TestGeometryConcat(t *testing.T) { matrix1 := ScaleGeometry(2, 2) matrix2 := TranslateGeometry(1, 1) diff --git a/image.go b/image.go index c5a6d7d52..c4b203e9e 100644 --- a/image.go +++ b/image.go @@ -152,7 +152,7 @@ func (i *Image) Fill(clr color.Color) (err error) { // The parts of the given image at the parts of the destination. // After determining parts to draw, this applies the geometry matrix and the color matrix. // -// If options is nil or its members are nil, the default values are used. +// Here are the default values: // DstParts: (0, 0) - (source width, source height) // SrcParts: (0, 0) - (source width, source height) (i.e. the whole source image) // GeometryMatrix: Identity matrix