Change the algorithm of RotateHue

This commit is contained in:
Hajime Hoshi 2014-12-15 09:37:44 +09:00
parent c3e909676c
commit 3cf449c52c
3 changed files with 28 additions and 16 deletions

View File

@ -110,18 +110,34 @@ func TranslateColor(clr color.Color) ColorMatrix {
// RotateHue returns a color matrix to rotate the hue // RotateHue returns a color matrix to rotate the hue
func RotateHue(theta float64) ColorMatrix { func RotateHue(theta float64) ColorMatrix {
sin, cos := math.Sincos(theta) sin, cos := math.Sincos(theta)
v1 := cos + (1.0-cos)/3.0 // RBG to YIQ
v2 := 1.0/3.0*(1.0-cos) - math.Sqrt(1.0/3.0)*sin c := ColorMatrix{
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{ [ColorMatrixDim - 1][ColorMatrixDim]float64{
{v1, v2, v3, 0, 0}, {0.299, 0.587, 0.114, 0, 0},
{v3, v1, v2, 0, 0}, {0.596, -0.275, -0.321, 0, 0},
{v2, v3, v1, 0, 0}, {0.212, -0.523, 0.311, 0, 0},
{0, 0, 0, 1, 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) { func rgba(clr color.Color) (float64, float64, float64, float64) {

View File

@ -115,11 +115,9 @@ func (s *GameScene) Draw(context ebiten.GraphicsContext, textures *Textures) {
context.Fill(0xff, 0xff, 0xff) context.Fill(0xff, 0xff, 0xff)
field := textures.GetTexture("empty") field := textures.GetTexture("empty")
geoMat := ebiten.GeometryMatrixI() geoMat := ebiten.ScaleGeometry(float64(fieldWidth)/float64(emptyWidth), float64(fieldHeight)/float64(emptyHeight))
geoMat.Concat(ebiten.ScaleGeometry(float64(fieldWidth)/float64(emptyWidth), float64(fieldHeight)/float64(emptyHeight)))
geoMat.Concat(ebiten.TranslateGeometry(20, 20)) // TODO: magic number? geoMat.Concat(ebiten.TranslateGeometry(20, 20)) // TODO: magic number?
colorMat := ebiten.ColorMatrixI() colorMat := ebiten.ScaleColor(color.RGBA{0, 0, 0, 0x80})
colorMat.Concat(ebiten.ScaleColor(color.RGBA{0, 0, 0, 0x80}))
ebiten.DrawWhole(context.Texture(field), emptyWidth, emptyHeight, geoMat, colorMat) ebiten.DrawWhole(context.Texture(field), emptyWidth, emptyHeight, geoMat, colorMat)
geoMat = ebiten.GeometryMatrixI() geoMat = ebiten.GeometryMatrixI()

View File

@ -54,10 +54,8 @@ func (g *Game) Draw(gr ebiten.GraphicsContext) error {
if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) { if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
gr.PushRenderTarget(g.canvasRenderTarget) gr.PushRenderTarget(g.canvasRenderTarget)
geo := ebiten.GeometryMatrixI() geo := ebiten.TranslateGeometry(float64(mx), float64(my))
geo.Concat(ebiten.TranslateGeometry(float64(mx), float64(my))) clr := ebiten.ScaleColor(color.RGBA{0xff, 0x40, 0x40, 0xff})
clr := ebiten.ColorMatrixI()
clr.Concat(ebiten.ScaleColor(color.RGBA{0xff, 0xff, 0x00, 0xff}))
theta := 2.0 * math.Pi * float64(g.count%60) / 60.0 theta := 2.0 * math.Pi * float64(g.count%60) / 60.0
clr.Concat(ebiten.RotateHue(theta)) clr.Concat(ebiten.RotateHue(theta))
ebiten.DrawWhole(gr.RenderTarget(g.brushRenderTarget), 1, 1, geo, clr) ebiten.DrawWhole(gr.RenderTarget(g.brushRenderTarget), 1, 1, geo, clr)