Sub Process_Globals
Private fx As JFX
Private MainForm As Form
Dim im As Image
Dim iv As ImageView
Dim colours As Map
Dim la As Label
End Sub
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.Show
im.Initialize("c:/temp","icontest.png") ' the image file
iv.Initialize("") ' just for display on form
iv.SetImage(im)
MainForm.RootPane.AddNode(iv,10,50,150,150)
la.Initialize("") ' to display the colour (text colour is the main colour found)
la.Text = "This is the main colour in the image"
MainForm.RootPane.AddNode(la,10,10,200,20)
colours.Initialize ' initialize the map
Dim st As Long = DateTime.now ' get start time
For x = 0 To im.width-1 ' read pixels in the image
For y = 0 To im.height-1
Dim c As String
c = im.GetPixel(x,y) ' get the pixel colour
If colours.ContainsKey(c) Then ' check if in map already
Dim cnt As Int = colours.Get(c)+1 ' increment count
colours.Put(c,cnt)
Else
colours.Put(c,1) ' set count to 1
End If
Next
Next
Dim pixelcount As Int = 0 ' the count of pixels
Dim maincolour As Int = 0 ' main colour found so far
For t = 0 To colours.Size -1
If colours.GetValueAt(t)> pixelcount Then ' read the map for the largest pixel count and store the colour
pixelcount = colours.GetValueAt(t)
maincolour = colours.GetKeyAt(t)
End If
Next
Log("Took "&(DateTime.Now - st)&"ms") ' display timing info
la.TextColor = fx.Colors.From32Bit(maincolour) ' set label text to main colour found
CSSUtils.SetBackgroundColor(la,fx.Colors.From32Bit(-Bit.And(0x00ffffff, maincolour))) ' set bg to opposite colour
Log("The main colour 0x"&Bit.ToHexString(maincolour)&" occurs "&pixelcount&" times in the image")
Log("There were "&colours.Size&" different colours in the image")
End Sub
'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
Return True
End Sub