From d5f8e79c885bdf6c58dfb5b49845bcbf035bd3cb Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Mon, 15 Dec 2014 01:45:41 +0900 Subject: [PATCH] Add RotateHue --- colormatrix.go | 23 ++++++++++++++++++++--- example/paint/main.go | 14 ++++++++++++-- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/colormatrix.go b/colormatrix.go index aef7db648..8ee1c7e05 100644 --- a/colormatrix.go +++ b/colormatrix.go @@ -68,9 +68,9 @@ func (c *ColorMatrix) setElement(i, j int, element float64) { // Monochrome returns a color matrix to make an image monochrome. func Monochrome() ColorMatrix { - const r float64 = 6968.0 / 32768.0 - const g float64 = 23434.0 / 32768.0 - const b float64 = 2366.0 / 32768.0 + const r = 6968.0 / 32768.0 + const g = 23434.0 / 32768.0 + const b = 2366.0 / 32768.0 return ColorMatrix{ [ColorMatrixDim - 1][ColorMatrixDim]float64{ {r, g, b, 0, 0}, @@ -81,6 +81,23 @@ func Monochrome() 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{ + [ColorMatrixDim - 1][ColorMatrixDim]float64{ + {v1, v2, v3, 0, 0}, + {v3, v1, v2, 0, 0}, + {v2, v3, v1, 0, 0}, + {0, 0, 0, 1, 0}, + }, + } +} + func rgba(clr color.Color) (float64, float64, float64, float64) { r, g, b, a := clr.RGBA() rf := float64(r) / float64(math.MaxUint16) diff --git a/example/paint/main.go b/example/paint/main.go index a52f5563d..c9d171563 100644 --- a/example/paint/main.go +++ b/example/paint/main.go @@ -4,7 +4,9 @@ import ( "fmt" "github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten/ebitenutil" + "image/color" "log" + "math" "runtime" ) @@ -14,11 +16,15 @@ const ( ) type Game struct { + count int brushRenderTarget ebiten.RenderTargetID canvasRenderTarget ebiten.RenderTargetID } func (g *Game) Update() error { + if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) { + g.count++ + } return nil } @@ -31,7 +37,7 @@ func (g *Game) Draw(gr ebiten.GraphicsContext) error { } gr.PushRenderTarget(g.brushRenderTarget) - gr.Fill(0, 0, 0) + gr.Fill(0xff, 0xff, 0xff) gr.PopRenderTarget() } if g.canvasRenderTarget.IsNil() { @@ -50,7 +56,11 @@ func (g *Game) Draw(gr ebiten.GraphicsContext) error { gr.PushRenderTarget(g.canvasRenderTarget) geo := ebiten.GeometryMatrixI() geo.Translate(float64(mx), float64(my)) - ebiten.DrawWhole(gr.RenderTarget(g.brushRenderTarget), 1, 1, geo, ebiten.ColorMatrixI()) + clr := ebiten.ColorMatrixI() + clr.Scale(color.RGBA{0xff, 0xff, 0x00, 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) gr.PopRenderTarget() }