Farbwechselalgorithmus

    • VB.NET

      Farbwechselalgorithmus

      Ich bin im Moment sehr beschäftigt, aber wollte euch eine Kleinigkeit zur Verfügung stellen.

      Guck auf die Animation und du siehst was du kriegst.
      Du kannst jeden Hintergrund eines Steuerelements animieren, setze einfach die Hintergrundfarbe am Anfang auf rot.
      Sowohl der Ablauf, als auch die Stopanimation läuft flüssig.



      Wichtig: Benutze keinen transparencykey auf eurer Form, sonst wird das für eine Farbenframe komisch aussehen.

      Schlechter Beispielcode

      VB.NET-Quellcode

      1. Dim x, x2 As Threading.Thread
      2. Dim colorChangeTest As SmoothColorChangeAlgorithm
      3. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
      4. x = New Threading.Thread(AddressOf colorChangeTest.startChanging)
      5. x.Start()
      6. End Sub
      7. Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
      8. x2 = New Threading.Thread(AddressOf colorChangeTest.stopChanging)
      9. x2.Start()
      10. End Sub
      11. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
      12. colorChangeTest = New SmoothColorChangeAlgorithm(Panel1, 0, SmoothColorChangeAlgorithm.switchSpeed.fast)
      13. End Sub
      14. Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
      15. Try
      16. x.Abort()
      17. x2.Abort()
      18. Catch
      19. End Try
      20. End Sub


      Code:

      VB.NET-Quellcode

      1. Public Class SmoothColorChangeAlgorithm
      2. #Region "Color table"
      3. Private colArray() As Color = New Color() {Color.FromArgb(255, 0, 0), _
      4. Color.FromArgb(255, 128, 0), _
      5. Color.FromArgb(255, 255, 0), _
      6. Color.FromArgb(128, 255, 0), _
      7. Color.FromArgb(0, 255, 0), _
      8. Color.FromArgb(0, 255, 128), _
      9. Color.FromArgb(0, 255, 255), _
      10. Color.FromArgb(0, 128, 255), _
      11. Color.FromArgb(0, 0, 255), _
      12. Color.FromArgb(128, 0, 255), _
      13. Color.FromArgb(255, 0, 255), _
      14. Color.FromArgb(255, 0, 128)}
      15. #End Region
      16. #Region "Helper"
      17. Private changeStop As Boolean
      18. Private changeDelay, sleepDelay, changeIndex As Integer
      19. Private target As Control
      20. Private swtSpd As switchSpeed = switchSpeed.fast
      21. Public Enum switchSpeed As Integer
      22. chilling = 50
      23. slow = 20
      24. normal = 10
      25. fast = 5
      26. End Enum
      27. Private Delegate Sub setColorDelegate(ByVal color As Color)
      28. Private Sub setColor(ByVal color As Color)
      29. target.BackColor = color
      30. End Sub
      31. #End Region
      32. #Region "Constructor"
      33. Public Sub New(ByVal target As Control)
      34. Me.target = target
      35. Me.sleepDelay = 2000
      36. Me.swtSpd = switchSpeed.normal
      37. End Sub
      38. Public Sub New(ByVal target As Control, ByVal sleepDelay As Integer)
      39. Me.target = target
      40. Me.sleepDelay = sleepDelay
      41. Me.swtSpd = switchSpeed.normal
      42. End Sub
      43. Public Sub New(ByVal target As Control, ByVal sleepDelay As Integer, ByVal switchSpeed As switchSpeed)
      44. Me.target = target
      45. Me.sleepDelay = sleepDelay
      46. Me.swtSpd = switchSpeed
      47. End Sub
      48. #End Region
      49. #Region "Core"
      50. Public Sub startChanging()
      51. changeStop = False
      52. setColor(colArray(0))
      53. changeIndex = 1
      54. Dim d As setColorDelegate = AddressOf setColor
      55. Do While changeStop = False
      56. Dim changeR, changeG, changeB As Single
      57. If target.BackColor.R < colArray(changeIndex).R Then
      58. changeR = -((colArray(changeIndex).R - target.BackColor.R) / 128)
      59. Else
      60. changeR = (target.BackColor.R - colArray(changeIndex).R) / 128
      61. End If
      62. If target.BackColor.G < colArray(changeIndex).G Then
      63. changeG = -((colArray(changeIndex).G - target.BackColor.G) / 128)
      64. Else
      65. changeG = (target.BackColor.G - colArray(changeIndex).G) / 128
      66. End If
      67. If target.BackColor.B < colArray(changeIndex).B Then
      68. changeB = -((colArray(changeIndex).B - target.BackColor.B) / 128)
      69. Else
      70. changeB = (target.BackColor.B - colArray(changeIndex).B) / 128
      71. End If
      72. For changeIndex As Integer = 1 To 127 Step 1
      73. Dim newR As Double = target.BackColor.R - changeR
      74. If newR > 255 Then
      75. newR += changeR + changeR
      76. End If
      77. Dim newg As Double = target.BackColor.G - changeG
      78. If newg > 255 Then
      79. newg += changeG + changeG
      80. End If
      81. Dim newb As Double = target.BackColor.B - changeB
      82. If newb > 255 Then
      83. newb += changeB + changeB
      84. End If
      85. If changeStop = True Then Exit Do
      86. Dim col As Color = Color.FromArgb(newR, newg, newb)
      87. d.BeginInvoke(col, Nothing, Nothing)
      88. Threading.Thread.Sleep(swtSpd)
      89. Next
      90. d.BeginInvoke(colArray(changeIndex), Nothing, Nothing)
      91. If changeStop = True Then Exit Do
      92. Threading.Thread.Sleep(sleepDelay)
      93. changeIndex += 1
      94. If changeIndex = 12 Then changeIndex = 0
      95. Loop
      96. End Sub
      97. Public Sub stopChanging()
      98. changeStop = True
      99. Dim d As setColorDelegate = AddressOf setColor
      100. Do While target.BackColor <> Color.Red
      101. Dim newR As Double = target.BackColor.R
      102. Dim newG As Double = target.BackColor.G
      103. Dim newB As Double = target.BackColor.B
      104. If newR < 255 Then
      105. newR += 1
      106. End If
      107. If newG > 0 Then
      108. newG -= 1
      109. End If
      110. If newB > 0 Then
      111. newB -= 1
      112. End If
      113. Dim col As Color = Color.FromArgb(newR, newG, newB)
      114. d.BeginInvoke(col, Nothing, Nothing)
      115. Threading.Thread.Sleep(swtSpd)
      116. Loop
      117. End Sub
      118. #End Region
      119. End Class
      Bilder
      • ColorChange.gif

        1,21 MB, 236×319, 150 mal angesehen