From 3cf449c52c37abec3e62a561d18a337c18c75377 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Mon, 15 Dec 2014 09:37:44 +0900 Subject: [PATCH] Change the algorithm of RotateHue --- colormatrix.go | 32 ++++++++++++++++++++++-------- example/blocks/blocks/gamescene.go | 6 ++---- example/paint/main.go | 6 ++---- 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/colormatrix.go b/colormatrix.go index 6f450186a..42cf085fc 100644 --- a/colormatrix.go +++ b/colormatrix.go @@ -110,18 +110,34 @@ func TranslateColor(clr color.Color) ColorMatrix { // RotateHue returns a color matrix to rotate the hue func RotateHue(theta float64) ColorMatrix { 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 - // TODO: Need to clamp the values between 0 and 1? - return ColorMatrix{ + // RBG to YIQ + c := ColorMatrix{ [ColorMatrixDim - 1][ColorMatrixDim]float64{ - {v1, v2, v3, 0, 0}, - {v3, v1, v2, 0, 0}, - {v2, v3, v1, 0, 0}, + {0.299, 0.587, 0.114, 0, 0}, + {0.596, -0.275, -0.321, 0, 0}, + {0.212, -0.523, 0.311, 0, 0}, {0, 0, 0, 1, 0}, }, } + // Rotate around the Y axis + c.Concat(ColorMatrix{ + [ColorMatrixDim - 1][ColorMatrixDim]float64{ + {1, 0, 0, 0, 0}, + {0, cos, sin, 0, 0}, + {0, -sin, cos, 0, 0}, + {0, 0, 0, 1, 0}, + }, + }) + // YIQ to RGB + c.Concat(ColorMatrix{ + [ColorMatrixDim - 1][ColorMatrixDim]float64{ + {1, 0.956, 0.621, 0, 0}, + {1, -0.272, -0.647, 0, 0}, + {1, -1.105, 1.702, 0, 0}, + {0, 0, 0, 1, 0}, + }, + }) + return c } func rgba(clr color.Color) (float64, float64, float64, float64) { diff --git a/example/blocks/blocks/gamescene.go b/example/blocks/blocks/gamescene.go index 2d0609415..f980f4ffa 100644 --- a/example/blocks/blocks/gamescene.go +++ b/example/blocks/blocks/gamescene.go @@ -115,11 +115,9 @@ func (s *GameScene) Draw(context ebiten.GraphicsContext, textures *Textures) { context.Fill(0xff, 0xff, 0xff) field := textures.GetTexture("empty") - geoMat := ebiten.GeometryMatrixI() - geoMat.Concat(ebiten.ScaleGeometry(float64(fieldWidth)/float64(emptyWidth), float64(fieldHeight)/float64(emptyHeight))) + geoMat := ebiten.ScaleGeometry(float64(fieldWidth)/float64(emptyWidth), float64(fieldHeight)/float64(emptyHeight)) geoMat.Concat(ebiten.TranslateGeometry(20, 20)) // TODO: magic number? - colorMat := ebiten.ColorMatrixI() - colorMat.Concat(ebiten.ScaleColor(color.RGBA{0, 0, 0, 0x80})) + colorMat := ebiten.ScaleColor(color.RGBA{0, 0, 0, 0x80}) ebiten.DrawWhole(context.Texture(field), emptyWidth, emptyHeight, geoMat, colorMat) geoMat = ebiten.GeometryMatrixI() diff --git a/example/paint/main.go b/example/paint/main.go index a444f53b6..84d8c70a5 100644 --- a/example/paint/main.go +++ b/example/paint/main.go @@ -54,10 +54,8 @@ func (g *Game) Draw(gr ebiten.GraphicsContext) error { if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) { gr.PushRenderTarget(g.canvasRenderTarget) - geo := ebiten.GeometryMatrixI() - geo.Concat(ebiten.TranslateGeometry(float64(mx), float64(my))) - clr := ebiten.ColorMatrixI() - clr.Concat(ebiten.ScaleColor(color.RGBA{0xff, 0xff, 0x00, 0xff})) + geo := ebiten.TranslateGeometry(float64(mx), float64(my)) + clr := ebiten.ScaleColor(color.RGBA{0xff, 0x40, 0x40, 0xff}) theta := 2.0 * math.Pi * float64(g.count%60) / 60.0 clr.Concat(ebiten.RotateHue(theta)) ebiten.DrawWhole(gr.RenderTarget(g.brushRenderTarget), 1, 1, geo, clr)