Sub Process_Globals
Private MainForm As Form
Private ImageView1 As B4XView
Private bc As BitmapCreator
Private xui As XUI
Private Pane1 As B4XView
End Sub
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.RootPane.LoadLayout("1") 'Load the layout file.
MainForm.Show
Dim bmp As B4XBitmap = xui.LoadBitmapResize(File.DirAssets, "1.png", ImageView1.Width / xui.Scale, ImageView1.Height / xui.Scale, False)
bc.Initialize(bmp.Width, bmp.Height)
bc.CopyPixelsFromBitmap(bmp)
bc.SetBitmapToImageView(bc.Bitmap, ImageView1)
End Sub
Sub Pane1_Touch (Action As Int, X As Float, Y As Float)
If Action = Pane1.TOUCH_ACTION_UP Then
FloodFill(x / xui.Scale, y / xui.Scale, Rnd(0xff000000, 0xffffffff))
bc.SetBitmapToImageView(bc.Bitmap, ImageView1)
End If
End Sub
Private Sub FloodFill (X As Int, Y As Int, ReplacementColor As Int)
Dim xx As List
xx.Initialize
Dim yy As List
yy.Initialize
Dim tpm, rpm As PremultipliedColor
Dim argb As ARGBColor
bc.GetPremultipliedColor(X, Y, tpm)
bc.ARGBToPremultipliedColor(bc.ColorToARGB(ReplacementColor, argb), rpm)
If ColorsEqual(rpm, tpm) Then Return
SetAndAddToQueue(X, Y, xx, yy, tpm, rpm)
Do While xx.Size > 0
Dim nx As Int = xx.Get(0)
Dim ny As Int = yy.Get(0)
xx.RemoveAt(0)
yy.RemoveAt(0)
SetAndAddToQueue(nx, ny + 1, xx, yy, tpm, rpm)
SetAndAddToQueue(nx, ny - 1, xx, yy, tpm, rpm)
SetAndAddToQueue(nx + 1, ny, xx, yy, tpm, rpm)
SetAndAddToQueue(nx - 1, ny, xx, yy, tpm, rpm)
Loop
End Sub
Private Sub SetAndAddToQueue (x As Int, y As Int, xx As List, yy As List, tpm As PremultipliedColor, rpm As PremultipliedColor)
If x < 0 Or x >= bc.mWidth Or y < 0 Or y >= bc.mHeight Then Return
Dim temp As PremultipliedColor
If ColorsEqual(bc.GetPremultipliedColor(x, y, temp), tpm) Then
bc.SetPremultipliedColor(x, y, rpm)
xx.Add(x)
yy.Add(y)
End If
End Sub
Private Sub ColorsEqual(pm1 As PremultipliedColor, pm2 As PremultipliedColor) As Boolean
Return pm1.r = pm2.r And pm1.g = pm2.g And pm1.b = pm2.b
End Sub