Rename ColorGeometry -> ColorM, GeometryMatrix -> GeoM (#27)

This commit is contained in:
Hajime Hoshi 2014-12-26 22:50:02 +09:00
parent 951eac3452
commit ffab448d40
15 changed files with 101 additions and 101 deletions

View File

@ -18,24 +18,24 @@ import (
"math"
)
// ColorMatrixDim is a dimension of a ColorMatrix.
const ColorMatrixDim = 5
// ColorMDim is a dimension of a ColorM.
const ColorMDim = 5
// A ColorMatrix represents a matrix to transform coloring when rendering an image.
// A ColorM represents a matrix to transform coloring when rendering an image.
//
// A ColorMatrix is applied to the source alpha color
// A ColorM is applied to the source alpha color
// while an Image's pixels' format is alpha premultiplied.
// Before applying a matrix, a color is un-multiplied, and after applying the matrix,
// the color is multiplied again.
//
// The initial value is identity.
type ColorMatrix struct {
type ColorM struct {
initialized bool
es [ColorMatrixDim - 1][ColorMatrixDim]float64
es [ColorMDim - 1][ColorMDim]float64
}
func colorMatrixEsI() [ColorMatrixDim - 1][ColorMatrixDim]float64 {
return [ColorMatrixDim - 1][ColorMatrixDim]float64{
func colorMatrixEsI() [ColorMDim - 1][ColorMDim]float64 {
return [ColorMDim - 1][ColorMDim]float64{
{1, 0, 0, 0, 0},
{0, 1, 0, 0, 0},
{0, 0, 1, 0, 0},
@ -43,12 +43,12 @@ func colorMatrixEsI() [ColorMatrixDim - 1][ColorMatrixDim]float64 {
}
}
func (c ColorMatrix) dim() int {
return ColorMatrixDim
func (c ColorM) dim() int {
return ColorMDim
}
// Element returns a value of a matrix at (i, j).
func (c ColorMatrix) Element(i, j int) float64 {
func (c ColorM) Element(i, j int) float64 {
if !c.initialized {
if i == j {
return 1
@ -59,29 +59,29 @@ func (c ColorMatrix) Element(i, j int) float64 {
}
// Concat multiplies a color matrix with the other color matrix.
func (c *ColorMatrix) Concat(other ColorMatrix) {
func (c *ColorM) Concat(other ColorM) {
if !c.initialized {
c.es = colorMatrixEsI()
c.initialized = true
}
result := ColorMatrix{}
result := ColorM{}
mul(&other, c, &result)
*c = result
}
// Add adds a color matrix with the other color matrix.
func (c *ColorMatrix) Add(other ColorMatrix) {
func (c *ColorM) Add(other ColorM) {
if !c.initialized {
c.es = colorMatrixEsI()
c.initialized = true
}
result := ColorMatrix{}
result := ColorM{}
add(&other, c, &result)
*c = result
}
// SetElement sets an element at (i, j).
func (c *ColorMatrix) SetElement(i, j int, element float64) {
func (c *ColorM) SetElement(i, j int, element float64) {
if !c.initialized {
c.es = colorMatrixEsI()
c.initialized = true
@ -90,13 +90,13 @@ func (c *ColorMatrix) SetElement(i, j int, element float64) {
}
// Monochrome returns a color matrix to make an image monochrome.
func Monochrome() ColorMatrix {
func Monochrome() ColorM {
const r = 6968.0 / 32768.0
const g = 23434.0 / 32768.0
const b = 2366.0 / 32768.0
return ColorMatrix{
return ColorM{
initialized: true,
es: [ColorMatrixDim - 1][ColorMatrixDim]float64{
es: [ColorMDim - 1][ColorMDim]float64{
{r, g, b, 0, 0},
{r, g, b, 0, 0},
{r, g, b, 0, 0},
@ -106,10 +106,10 @@ 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{
func ScaleColor(r, g, b, a float64) ColorM {
return ColorM{
initialized: true,
es: [ColorMatrixDim - 1][ColorMatrixDim]float64{
es: [ColorMDim - 1][ColorMDim]float64{
{r, 0, 0, 0, 0},
{0, g, 0, 0, 0},
{0, 0, b, 0, 0},
@ -119,10 +119,10 @@ 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{
func TranslateColor(r, g, b, a float64) ColorM {
return ColorM{
initialized: true,
es: [ColorMatrixDim - 1][ColorMatrixDim]float64{
es: [ColorMDim - 1][ColorMDim]float64{
{1, 0, 0, 0, r},
{0, 1, 0, 0, g},
{0, 0, 1, 0, b},
@ -132,14 +132,14 @@ func TranslateColor(r, g, b, a float64) ColorMatrix {
}
// RotateHue returns a color matrix to rotate the hue
func RotateHue(theta float64) ColorMatrix {
func RotateHue(theta float64) ColorM {
sin, cos := math.Sincos(theta)
v1 := cos + (1.0-cos)/3.0
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{
return ColorM{
initialized: true,
es: [ColorMatrixDim - 1][ColorMatrixDim]float64{
es: [ColorMDim - 1][ColorMDim]float64{
{v1, v2, v3, 0, 0},
{v3, v1, v2, 0, 0},
{v2, v3, v1, 0, 0},

View File

@ -20,9 +20,9 @@ import (
)
func TestColorInit(t *testing.T) {
var m ColorMatrix
for i := 0; i < ColorMatrixDim-1; i++ {
for j := 0; j < ColorMatrixDim; j++ {
var m ColorM
for i := 0; i < ColorMDim-1; i++ {
for j := 0; j < ColorMDim; j++ {
got := m.Element(i, j)
want := 0.0
if i == j {
@ -35,8 +35,8 @@ func TestColorInit(t *testing.T) {
}
m.Add(m)
for i := 0; i < ColorMatrixDim-1; i++ {
for j := 0; j < ColorMatrixDim; j++ {
for i := 0; i < ColorMDim-1; i++ {
for j := 0; j < ColorMDim; j++ {
got := m.Element(i, j)
want := 0.0
if i == j {

View File

@ -59,9 +59,9 @@ func (d *debugPrintState) drawText(rt *ebiten.Image, str string, x, y int, c col
b := float64(cc.B) / math.MaxUint16
a := float64(cc.A) / math.MaxUint16
rt.DrawImageAt(d.textImage, x+1, y, &ebiten.DrawImageOptions{
DstParts: dsts,
SrcParts: srcs,
ColorMatrix: ebiten.ScaleColor(r, g, b, a),
DstParts: dsts,
SrcParts: srcs,
ColorM: ebiten.ScaleColor(r, g, b, a),
})
}

View File

@ -50,7 +50,7 @@ func update(screen *ebiten.Image) error {
}
for i := 0; i < 10; i++ {
op := &ebiten.DrawImageOptions{
ColorMatrix: ebiten.ScaleColor(1.0, 1.0, 1.0, 0.5),
ColorM: ebiten.ScaleColor(1.0, 1.0, 1.0, 0.5),
}
if err := tmpRenderTarget.DrawImageAt(ebitenImage, 15+int(float64(i)*diff), 20, op); err != nil {
return err

View File

@ -51,8 +51,8 @@ func drawText(rt *ebiten.Image, images *Images, str string, ox, oy, scale int, c
locationX += charWidth
}
geo := ebiten.ScaleGeometry(float64(scale), float64(scale))
geo.Concat(ebiten.TranslateGeometry(float64(ox), float64(oy)))
geo := ebiten.ScaleGeo(float64(scale), float64(scale))
geo.Concat(ebiten.TranslateGeo(float64(ox), float64(oy)))
c2 := color.NRGBA64Model.Convert(c).(color.NRGBA64)
const max = math.MaxUint16
r := float64(c2.R) / max
@ -61,10 +61,10 @@ func drawText(rt *ebiten.Image, images *Images, str string, ox, oy, scale int, c
a := float64(c2.A) / max
clr := ebiten.ScaleColor(r, g, b, a)
rt.DrawImage(fontImageId, &ebiten.DrawImageOptions{
DstParts: dsts,
SrcParts: srcs,
GeometryMatrix: geo,
ColorMatrix: clr,
DstParts: dsts,
SrcParts: srcs,
GeoM: geo,
ColorM: clr,
})
}

View File

@ -112,11 +112,11 @@ func (s *GameScene) Draw(r *ebiten.Image, images *Images) {
field := images.GetImage("empty")
w, h := field.Size()
geo := ebiten.ScaleGeometry(float64(fieldWidth)/float64(w), float64(fieldHeight)/float64(h))
geo.Concat(ebiten.TranslateGeometry(20, 20)) // TODO: magic number?
geo := ebiten.ScaleGeo(float64(fieldWidth)/float64(w), float64(fieldHeight)/float64(h))
geo.Concat(ebiten.TranslateGeo(20, 20)) // TODO: magic number?
r.DrawImage(field, &ebiten.DrawImageOptions{
GeometryMatrix: geo,
ColorMatrix: ebiten.ScaleColor(0.0, 0.0, 0.0, 0.5),
GeoM: geo,
ColorM: ebiten.ScaleColor(0.0, 0.0, 0.0, 0.5),
})
s.field.Draw(r, images, 20, 20)

View File

@ -75,7 +75,7 @@ func (s *SceneManager) Draw(r *ebiten.Image, images *Images) {
alpha := float64(s.transitionCount) / float64(transitionMaxCount)
op := &ebiten.DrawImageOptions{
ColorMatrix: ebiten.ScaleColor(1, 1, 1, alpha),
ColorM: ebiten.ScaleColor(1, 1, 1, alpha),
}
r.DrawImage(to, op)
}

View File

@ -35,10 +35,10 @@ var (
func update(screen *ebiten.Image) error {
gophersRenderTarget.DrawImage(gophersImage, &ebiten.DrawImageOptions{
GeometryMatrix: ebiten.ScaleGeometry(1.0/mosaicRatio, 1.0/mosaicRatio),
GeoM: ebiten.ScaleGeo(1.0/mosaicRatio, 1.0/mosaicRatio),
})
screen.DrawImage(gophersRenderTarget, &ebiten.DrawImageOptions{
GeometryMatrix: ebiten.ScaleGeometry(mosaicRatio, mosaicRatio),
GeoM: ebiten.ScaleGeo(mosaicRatio, mosaicRatio),
})
return nil
}

View File

@ -46,7 +46,7 @@ func Update(screen *ebiten.Image) error {
theta := 2.0 * math.Pi * float64(count%60) / 60.0
clr.Concat(ebiten.RotateHue(theta))
canvasRenderTarget.DrawImageAt(brushRenderTarget, mx, my, &ebiten.DrawImageOptions{
ColorMatrix: clr,
ColorM: clr,
})
}

View File

@ -41,12 +41,12 @@ func update(screen *ebiten.Image) error {
srcs = append(srcs, image.Rect(0, i, w, i+1))
}
maxWidth := float64(w) + float64(h)*0.75
geo := ebiten.TranslateGeometry(-maxWidth/2, -float64(h)/2)
geo.Concat(ebiten.TranslateGeometry(screenWidth/2, screenHeight/2))
geo := ebiten.TranslateGeo(-maxWidth/2, -float64(h)/2)
geo.Concat(ebiten.TranslateGeo(screenWidth/2, screenHeight/2))
screen.DrawImage(gophersImage, &ebiten.DrawImageOptions{
DstParts: dsts,
SrcParts: srcs,
GeometryMatrix: geo,
DstParts: dsts,
SrcParts: srcs,
GeoM: geo,
})
return nil
}

View File

@ -50,14 +50,14 @@ func update(screen *ebiten.Image) error {
}
w, h := gophersImage.Size()
geo := ebiten.TranslateGeometry(-float64(w)/2, -float64(h)/2)
geo := ebiten.TranslateGeo(-float64(w)/2, -float64(h)/2)
scaleX := math.Pow(1.05, float64(horizontalCount))
scaleY := math.Pow(1.05, float64(verticalCount))
geo.Concat(ebiten.ScaleGeometry(scaleX, scaleY))
geo.Concat(ebiten.RotateGeometry(float64(count%720) * 2 * math.Pi / 720))
geo.Concat(ebiten.TranslateGeometry(screenWidth/2, screenHeight/2))
geo.Concat(ebiten.ScaleGeo(scaleX, scaleY))
geo.Concat(ebiten.RotateGeo(float64(count%720) * 2 * math.Pi / 720))
geo.Concat(ebiten.TranslateGeo(screenWidth/2, screenHeight/2))
if err := screen.DrawImage(gophersImage, &ebiten.DrawImageOptions{
GeometryMatrix: geo,
GeoM: geo,
}); err != nil {
return err
}

View File

@ -18,30 +18,30 @@ import (
"math"
)
// GeometryMatrixDim is a dimension of a GeometryMatrix.
const GeometryMatrixDim = 3
// GeoMDim is a dimension of a GeoM.
const GeoMDim = 3
// A GeometryMatrix represents a matrix to transform geometry when rendering an image.
// A GeoM represents a matrix to transform geometry when rendering an image.
//
// The initial value is identity.
type GeometryMatrix struct {
type GeoM struct {
initialized bool
es [GeometryMatrixDim - 1][GeometryMatrixDim]float64
es [GeoMDim - 1][GeoMDim]float64
}
func geometryMatrixEsI() [GeometryMatrixDim - 1][GeometryMatrixDim]float64 {
return [GeometryMatrixDim - 1][GeometryMatrixDim]float64{
func geometryMatrixEsI() [GeoMDim - 1][GeoMDim]float64 {
return [GeoMDim - 1][GeoMDim]float64{
{1, 0, 0},
{0, 1, 0},
}
}
func (g GeometryMatrix) dim() int {
return GeometryMatrixDim
func (g GeoM) dim() int {
return GeoMDim
}
// Element returns a value of a matrix at (i, j).
func (g GeometryMatrix) Element(i, j int) float64 {
func (g GeoM) Element(i, j int) float64 {
if !g.initialized {
if i == j {
return 1
@ -52,29 +52,29 @@ func (g GeometryMatrix) Element(i, j int) float64 {
}
// Concat multiplies a geometry matrix with the other geometry matrix.
func (g *GeometryMatrix) Concat(other GeometryMatrix) {
func (g *GeoM) Concat(other GeoM) {
if !g.initialized {
g.es = geometryMatrixEsI()
g.initialized = true
}
result := GeometryMatrix{}
result := GeoM{}
mul(&other, g, &result)
*g = result
}
// Add adds a geometry matrix with the other geometry matrix.
func (g *GeometryMatrix) Add(other GeometryMatrix) {
func (g *GeoM) Add(other GeoM) {
if !g.initialized {
g.es = geometryMatrixEsI()
g.initialized = true
}
result := GeometryMatrix{}
result := GeoM{}
add(&other, g, &result)
*g = result
}
// SetElement sets an element at (i, j).
func (g *GeometryMatrix) SetElement(i, j int, element float64) {
func (g *GeoM) SetElement(i, j int, element float64) {
if !g.initialized {
g.es = geometryMatrixEsI()
g.initialized = true
@ -82,9 +82,9 @@ func (g *GeometryMatrix) SetElement(i, j int, element float64) {
g.es[i][j] = element
}
// ScaleGeometry returns a matrix that scales a geometry matrix by (x, y).
func ScaleGeometry(x, y float64) GeometryMatrix {
return GeometryMatrix{
// ScaleGeo returns a matrix that scales a geometry matrix by (x, y).
func ScaleGeo(x, y float64) GeoM {
return GeoM{
initialized: true,
es: [2][3]float64{
{x, 0, 0},
@ -93,9 +93,9 @@ 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{
// TranslateGeo returns a matrix taht translates a geometry matrix by (tx, ty).
func TranslateGeo(tx, ty float64) GeoM {
return GeoM{
initialized: true,
es: [2][3]float64{
{1, 0, tx},
@ -104,10 +104,10 @@ func TranslateGeometry(tx, ty float64) GeometryMatrix {
}
}
// RotateGeometry returns a matrix that rotates a geometry matrix by theta.
func RotateGeometry(theta float64) GeometryMatrix {
// RotateGeo returns a matrix that rotates a geometry matrix by theta.
func RotateGeo(theta float64) GeoM {
sin, cos := math.Sincos(theta)
return GeometryMatrix{
return GeoM{
initialized: true,
es: [2][3]float64{
{cos, -sin, 0},

View File

@ -20,9 +20,9 @@ import (
)
func TestGeometryInit(t *testing.T) {
var m GeometryMatrix
for i := 0; i < GeometryMatrixDim-1; i++ {
for j := 0; j < GeometryMatrixDim; j++ {
var m GeoM
for i := 0; i < GeoMDim-1; i++ {
for j := 0; j < GeoMDim; j++ {
got := m.Element(i, j)
want := 0.0
if i == j {
@ -35,8 +35,8 @@ func TestGeometryInit(t *testing.T) {
}
m.Add(m)
for i := 0; i < GeometryMatrixDim-1; i++ {
for j := 0; j < GeometryMatrixDim; j++ {
for i := 0; i < GeoMDim-1; i++ {
for j := 0; j < GeoMDim; j++ {
got := m.Element(i, j)
want := 0.0
if i == j {
@ -50,7 +50,7 @@ func TestGeometryInit(t *testing.T) {
}
func TestGeometryAssign(t *testing.T) {
m := ScaleGeometry(1, 1) // Create elements explicitly
m := ScaleGeo(1, 1) // Create elements explicitly
m2 := m
m.SetElement(0, 0, 0)
got := m2.Element(0, 0)
@ -61,8 +61,8 @@ func TestGeometryAssign(t *testing.T) {
}
func TestGeometryConcat(t *testing.T) {
matrix1 := ScaleGeometry(2, 2)
matrix2 := TranslateGeometry(1, 1)
matrix1 := ScaleGeo(2, 2)
matrix2 := TranslateGeo(1, 1)
matrix3 := matrix1
matrix3.Concat(matrix2)

View File

@ -67,7 +67,7 @@ func (c *graphicsContext) postUpdate() error {
scale := float64(c.screenScale)
options := &DrawImageOptions{
GeometryMatrix: ScaleGeometry(scale, scale),
GeoM: ScaleGeo(scale, scale),
}
if err := c.defaultR.drawImage(c.screen, options); err != nil {
return err

View File

@ -67,8 +67,8 @@ func (i *innerImage) drawImage(img *innerImage, options *DrawImageOptions) error
image.Rect(0, 0, w, h),
}
}
geo := options.GeometryMatrix
clr := options.ColorMatrix
geo := options.GeoM
clr := options.ColorM
if err := i.framebuffer.SetAsViewport(); err != nil {
return err
@ -168,7 +168,7 @@ func (i *Image) DrawImageAt(image *Image, x, y int, options *DrawImageOptions) (
if options == nil {
options = &DrawImageOptions{}
}
options.GeometryMatrix.Concat(TranslateGeometry(float64(x), float64(y)))
options.GeoM.Concat(TranslateGeo(float64(x), float64(y)))
return i.drawImage(image.inner, options)
}
@ -213,8 +213,8 @@ func (i *Image) At(x, y int) color.Color {
// A DrawImageOptions presents options to render an image on an image.
type DrawImageOptions struct {
DstParts []image.Rectangle
SrcParts []image.Rectangle
GeometryMatrix GeometryMatrix
ColorMatrix ColorMatrix
DstParts []image.Rectangle
SrcParts []image.Rectangle
GeoM GeoM
ColorM ColorM
}