04-14-2025, 04:19 PM
Hey, that worked! Thanks.
In fact, I liked it so much that I modified it to smoothly transition between random colors:
In fact, I liked it so much that I modified it to smoothly transition between random colors:
Code: (Select All)
Dim c~&
Dim r1&, g1&, b1&
Dim dr!, dg!, db!
Screen _NewImage(320, 240, 32)
_FullScreen
_Delay .5 'give the screen time to initialize fully so we can associate mouse input with it and such
'c~& = Point(iX, iY)
Randomize Timer
c~& = _RGB32(Rnd * 256, Rnd * 256, Rnd * 256)
r1& = _Red32(c~&): g1& = _Green32(c~&): b1& = _Blue32(c~&)
Cls , c~&
GetNextColor c~&, dr!, dg!, db!
t# = Timer(0.001)
While _MouseInput: Wend: oldX = _MouseX: oldy = _MouseY 'initial mouse placement
Do
' Every 5 seconds, pick a new color and setup to transition to that in 5 seconds
If Timer(0.001) > t# Then 'change background every 3 seconds
'Cls , _RGB32(Rnd * 256, Rnd * 256, Rnd * 256)
't# = Timer(0.001) + 3
GetNextColor c~&, dr!, dg!, db!
'Cls , c~&
'r1& = _Red32(c~&): g1& = _Green32(c~&): b1& = _Blue32(c~&)
'c~& = _RGB32(r2&, g2&, b2&)
t# = Timer(0.001) + 5
End If
While _MouseInput: Wend
If oldX <> _MouseX Or oldy <> _MouseY Then Exit Do 'if the mouse moved, quit
' Change r/g/b values to move towards next color
r1& = _Red32(c~&): g1& = _Green32(c~&): b1& = _Blue32(c~&)
r1& = r1& + dr!
g1& = g1& + dg!
b1& = b1& + db!
c~& = _RGB32(r1&, g1&, b1&)
Cls , c~&
_Limit 51
Loop Until _KeyHit Or _MouseButton(1) Or _MouseButton(2) 'keyhit or mouse button hit, quit
System
' /////////////////////////////////////////////////////////////////////////////
' c~& = current color
' dr!, dg!, db! = amount to change each color channel per frame to get to next color in 5 seconds
Sub GetNextColor (c~&, dr!, dg!, db!)
Dim r1&, g1&, b1& ' current color
Dim r2&, g2&, b2& ' new color
Dim rDiff&, gDiff&, bDiff& ' diff between each color channel for old/new colors
r1& = _Red32(c~&): g1& = _Green32(c~&): b1& = _Blue32(c~&)
' choose a random new color
Do
r2& = Rnd * 256: g2& = Rnd * 256: b2& = Rnd * 256
If r2& <> r1& Or g2& <> g1& Or b2& <> b1& Then Exit Do
Loop
'c~& = _RGB32(r2&, g2&, b2&)
' get differences for each color channel
rDiff& = r1& - r2&
gDiff& = g1& - g2&
bDiff& = b1& - b2&
' determine how much to change each color channel each frame
' to evenly arrive at new color when time is up
If Abs(rDiff&) <= Abs(gDiff&) And Abs(rDiff&) <= Abs(bDiff&) Then
dr! = 1 * _IIf(r1& < r2&, 1, -1)
dg! = Abs(rDiff&) / Abs(gDiff&) * _IIf(g1& < g2&, 1, -1)
db! = Abs(rDiff&) / Abs(bDiff&) * _IIf(b1& < b2&, 1, -1)
ElseIf Abs(gDiff&) <= Abs(rDiff&) And Abs(gDiff&) <= Abs(bDiff&) Then
dr! = Abs(gDiff&) / Abs(rDiff&) * _IIf(r1& < r2&, 1, -1)
dg! = 1 * _IIf(g1& < g2&, 1, -1)
db! = Abs(gDiff&) / Abs(bDiff&) * _IIf(b1& < b2&, 1, -1)
Else
dr! = Abs(bDiff&) / Abs(rDiff&) * _IIf(r1& < r2&, 1, -1)
dg! = Abs(bDiff&) / Abs(gDiff&) * _IIf(g1& < g2&, 1, -1)
db! = 1 * _IIf(b1& < b2&, 1, -1)
End If
' max diff = 255
' change smallest 1 every cycle
' 255/5 = 51
' total cycles = {fps} * {seconds} = 255
' 51 * 5 = 255
End Sub ' GetNextColor