Wingnut

Well-known member
3rd Party Module Dev
Tutorial Author
A classic example of achieving an even Gradient: Illustrating how to create 16 distinct steps.

Code:
``````Import mojo2

Function Main()
New MyGame()
End Function

Class MyGame Extends App

Field myCanvas:Canvas
Field r:Int[2],g:Int[2], b:Int[2]
Field cnt:Int

Method OnCreate()
myCanvas = New Canvas()
SetUpdateRate 0
Seed = GetDate[5]
For Local i=0 To 1
r[i] = Rnd(0,255) ; g[i] = Rnd(0,255) ; b[i] = Rnd(0,255)
Next
End Method

Method OnUpdate()
cnt+=1
If cnt>200
cnt=0
For Local i=0 To 1
r[i] = Rnd(0,255) ; g[i] = Rnd(0,255) ; b[i] = Rnd(0,255)
Next
End If
End Method

Method OnRender()
myCanvas.Clear
For Local i=0 Until 16
myCanvas.DrawRect i*(DeviceWidth/16),0, (DeviceWidth/16),DeviceHeight
Next
myCanvas.SetColor 0,0,0
myCanvas.DrawText "Cerberus-X - Spreading 2 colors " + "out into 16 colors - Example",0,0
myCanvas.DrawText "R1:"+r[0]+ " G1:"+g[0] +  " B1:"+b[0],100,50
myCanvas.DrawText "R2:"+r[1] + " G2:"+g[1] + " B2:"+b[1],100,70
myCanvas.Flush
End Method
End Class

Field r:Float[16], g:Float[16], b:Float[16]

Method New(  r1:Int,g1:Int,b1:Int, r2:Int,g2:Int,b2:Int)
Local sr:Float, sg:Float, sb:Float
If r1>r2
sr = (r1-r2)/16
For Local i:Float=0 Until 16 Step 1
r[i] = r1-(i*sr)
Next
Else
sr = (r2-r1)/16
For Local i:Float=0 Until 16 Step 1
r[i] = r1+i*sr
Next
End If
If g1>g2
sg = (g1-g2)/16
For Local i:Float=0 Until 16 Step 1
g[i] = g1-(i*sg)
Next
Else
sg = (g2-g1)/16
For Local i:Float=0 Until 16 Step 1
g[i] = g1+i*sg
Next
End If
If b1>b2
sb = (b1-b2)/16
For Local i:Float=0 Until 16 Step 1
b[i] = b1-(i*sb)
Next
Else
sb = (b2-b1)/16
For Local i:Float=0 Until 16 Step 1
b[i] = b1+i*sb
Next
End If
End Method

End Class``````