.Net Stats:

How to support the site


Site Wide Message: (current site time 9/6/2010 10:41:37 AM EDT)
  • We want your input! One of our sponsors wants to know your opinion about development related issues. Click here to tell us what you think.
  • Are you an emerging/young developer (aged 18-30)? If so, would you like the chance to affect future developer tools and products?
    If so, then click here to give your feedback.
 

ColorFader (Class)

Print
Email
VB icon
Submitted on: 8/5/2008 4:27:47 PM
By: [RTS]BN+VS*  
Level: Intermediate
User Rating: Unrated
Compatibility:VB.NET

Users have accessed this code  1878 times.
 
author picture
(About the author)
 
     This class gives you loads of brute power for fading color poperties of objects, with just a few lines of code! It can be used in loop together with the InfoSlider class. NEW VERSION AVAIBLE! Get it here: http://code.tiko-world.com/forum/viewtopic.php?t=40
 
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
// for :ColorFader (Class)
//**************************************
Updates and demo can be found here: http://code.tiko-world.com/forum/viewtopic.php?t=40
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: ColorFader (Class)
// Description:This class gives you loads of brute power for fading color poperties of objects, with just a few lines of code! It can be used in loop together with the InfoSlider class.
NEW VERSION AVAIBLE! Get it here: http://code.tiko-world.com/forum/viewtopic.php?t=40
// By: [RTS]BN+VS*
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=6727&lngWId=10//for details.//**************************************

'************************************************' 
' Class ColorFader v2.0.0 - july 2008' 
' By De Dauw Jeroen - jeroen_dedauw@yahoo.com ' 
'************************************************' 
' Class dependencies: none ' 
'************************************************' 
Option Strict On : Option Explicit On 
#Region " Class ColorFader " 
Public Class ColorFader 
#Region "Declarations" 
#Region "Events" 
 Friend Event Initiated() 
 Friend Event Tick() 
 Friend Event Completed() 
 Friend Event Stoped() 
 Friend Event Pauzed() 
 Friend Event Resumed() 
 Friend Event ControlChanged() 
 Friend Event IntervalChanged() 
 Friend Event StepAmountChanged() 
 Friend Event StartColorChanged() 
 Friend Event StopColorChanged() 
 Friend Event ColorPropertyChanged() 
 Friend Event IsBussyChanged() 
 Friend Event IsPauzedChanged() 
#End Region 
#Region "Enums" 
 Public Enum ColorProperty 
 foreGround 
 backGround 
 End Enum 
#End Region 
#Region "Variables" 
 Private WithEvents timer As New Timer 
 Private ctrl As Object 
 Private currentR, seizeR, currentG, seizeG, currentB, seizeB, currentStep, nrSteps As Integer 
 Private initColor, endColor As Color 
 Private clrProperty As ColorProperty 
 Private hasBeenPauzed As Boolean = False 
#End Region 
#End Region 
#Region "Procedures" 
#Region "Public" 
#Region "New" 
 Public Sub New(ByVal control As Object, ByVal sColor As Color, ByVal eColor As Color, _ 
 Optional ByVal stepAmount As Integer = 40, Optional ByVal interval As Integer = 100, _ 
 Optional ByVal colorProperty As colorProperty = colorProperty.foreGround) 
 ctrl = control 
 If sColor <> Nothing Then initColor = sColor 
 If eColor <> Nothing Then endColor = eColor 
 nrSteps = stepAmount 
 With timer 
.Enabled = False 
.Interval = interval 
 End With 
 End Sub 
#End Region 
#Region "Fade" 
 Public Sub Fade(Optional ByVal interval As Integer = -1) 
 If interval >= 0 And timer.Interval <> interval Then 
timer.Interval = interval 
RaiseEvent IntervalChanged() 
 End If 
 initFading() 
 End Sub 
 Public Sub AutoFade(Optional ByVal interval As Integer = -1) 
 If interval >= 0 Then timer.Interval = interval 
 If Not TypeOf (ctrl) Is Control Then Exit Sub 
 If clrProperty = colorProperty.foreGround Then 
If CType(ctrl, Control).ForeColor <> CType(ctrl, Control).BackColor Then 
initColor = CType(ctrl, Control).ForeColor 
endColor = CType(ctrl, Control).BackColor 
Else 
initColor = CType(ctrl, Control).BackColor 
endColor = Color.Black 
End If 
 ElseIf clrProperty = colorProperty.backGround Then 
If CType(ctrl, Control).BackColor <> CType(ctrl, Control).ForeColor Then 
initColor = CType(ctrl, Control).Parent.BackColor 
endColor = CType(ctrl, Control).ForeColor 
Else 
initColor = CType(ctrl, Control).ForeColor 
endColor = CType(ctrl, Control).Parent.BackColor 
End If 
 End If 
 initFading() 
 End Sub 
 Public Sub Fade(ByVal sColor As Color, ByVal eColor As Color, Optional ByVal interval As Integer = -1) 
 If sColor <> Nothing Then initColor = sColor 
 If eColor <> Nothing Then endColor = eColor 
 If interval >= 0 Then timer.Interval = interval 
 initFading() 
 End Sub 
#End Region 
#Region "Other" 
 Public Sub Reset() 
 If hasBeenPauzed Then hasBeenPauzed = False : RaiseEvent IsPauzedChanged() 
 endFading() 
 RaiseEvent Stoped() 
 End Sub 
 Public Sub PauzeFading() 
 If timer.Enabled Then 
timer.Stop() 
If Not hasBeenPauzed Then hasBeenPauzed = True : RaiseEvent IsPauzedChanged() 
RaiseEvent Pauzed() 
 End If 
 End Sub 
 Public Sub ResumeFading() 
 timer.Start() 
 If hasBeenPauzed Then hasBeenPauzed = False : RaiseEvent IsPauzedChanged() 
 RaiseEvent Resumed() 
 End Sub 
#End Region 
#End Region 
#Region "Private" 
 Private Sub initFading() 
 If hasBeenPauzed Then hasBeenPauzed = False : RaiseEvent IsPauzedChanged() 
 resetColorFader() 
 setColorProperty(initColor) 
 timer.Enabled = True 
 RaiseEvent IsBussyChanged() 
 RaiseEvent Initiated() 
 End Sub 
 Private Sub resetColorFader() 
 ' Get the RGB values of init and end color, and calculate the step seize for each color 
 Dim startR, endR, startG, endG, startB, endB As Integer 
 colorToRGB(initColor, startR, startG, startB) 
 colorToRGB(endColor, endR, endG, endB) 
 seizeR = CInt((startR - endR) / nrSteps) 
 seizeG = CInt((startG - endG) / nrSteps) 
 seizeB = CInt((startB - endB) / nrSteps) 
 ' Reset the main dynamic values 
 currentStep = 0 
 currentR = startR : currentG = startG : currentB = startB 
 End Sub 
 Private Sub endFading() 
 timer.Enabled = False 
 setColorProperty(endColor) 
 resetColorFader() 
 RaiseEvent IsBussyChanged() 
 End Sub 
 Private Sub timer_tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles timer.Tick 
 currentStep += 1 
 If currentStep <= nrSteps Then 
doFading() 
 Else 
endFading() 
RaiseEvent Completed() 
 End If 
 End Sub 
 Private Sub doFading() 
 RaiseEvent Tick() 
 doIncreasement() 
 setColorProperty(Color.FromArgb(1, currentR, currentG, currentB)) 
 End Sub 
 Private Sub doIncreasement() 
 currentR -= seizeR : If currentR < 0 Then currentR = 0 
 currentG -= seizeG : If currentG < 0 Then currentG = 0 
 currentB -= seizeB : If currentB < 0 Then currentB = 0 
 End Sub 
 Private Sub setColorProperty(ByVal colorToSet As Color) 
 On Error Resume Next 
 Select Case clrProperty 
Case colorProperty.backGround 
If TypeOf (ctrl) Is Control Then 
 CType(ctrl, Control).BackColor = colorToSet 
 MessageBox.Show(colorToSet.ToString) 
End If 
Case colorProperty.foreGround 
If TypeOf (ctrl) Is Control Then CType(ctrl, Control).ForeColor = colorToSet 
 End Select 
 If Err.Number <> 0 Then 
Me.Reset() 
MessageBox.Show("The chosen color property is not supported by the current control", _ 
"Color property not valid", MessageBoxButtons.OK, MessageBoxIcon.Error) 
MessageBox.Show(Err.Description) 
 End If 
 End Sub 
 Private Sub colorToRGB(ByVal colorToConvert As Color, _ 
 ByRef R As Integer, ByRef G As Integer, ByRef B As Integer) 
 Dim SStr As String = Microsoft.VisualBasic.Conversion.Hex(colorToConvert.ToArgb).Substring(2) 
 R = CInt(Format("&H" & Mid(SStr, 1, 2))) 
 G = CInt(Format("&H" & Mid(SStr, 3, 2))) 
 B = CInt(Format("&H" & Mid(SStr, 5, 2))) 
 End Sub 
#End Region 
#End Region 
#Region "Properties" 
 Public Property Control() As Object 
 Get 
Return ctrl 
 End Get 
 Set(ByVal value As Object) 
If Not ctrl.Equals(value) Then ctrl = value : RaiseEvent ControlChanged() 
 End Set 
 End Property 
 Public Property StartColor() As Color 
 Get 
Return initColor 
 End Get 
 Set(ByVal value As Color) 
If initColor <> value Then initColor = value : RaiseEvent StartColorChanged() 
 End Set 
 End Property 
 Public Property StopColor() As Color 
 Get 
Return endColor 
 End Get 
 Set(ByVal value As Color) 
If endColor <> value Then endColor = value : RaiseEvent StopColorChanged() 
 End Set 
 End Property 
 Public Property Interval() As UInteger 
 Get 
Return CUInt(timer.Interval) 
 End Get 
 Set(ByVal value As UInteger) 
If value >= 0 Then timer.Interval = CInt(value) : RaiseEvent IntervalChanged() 
 End Set 
 End Property 
 Public Property StepAmount() As UInteger 
 Get 
Return CUInt(nrSteps) 
 End Get 
 Set(ByVal value As UInteger) 
If nrSteps <> CInt(value) Then nrSteps = CInt(value) : RaiseEvent StepAmountChanged() 
 End Set 
 End Property 
 Public Property FadingProperty() As ColorProperty 
 Get 
Return clrProperty 
 End Get 
 Set(ByVal value As ColorProperty) 
If clrProperty <> value Then clrProperty = value : RaiseEvent ColorPropertyChanged() 
 End Set 
 End Property 
 Public ReadOnly Property CurrentColor() As Color 
 Get 
Return Color.FromArgb(1, currentR, currentG, currentB) 
 End Get 
 End Property 
 Public ReadOnly Property IsBussy() As Boolean 
 Get 
Return timer.Enabled Or hasBeenPauzed 
 End Get 
 End Property 
 Public ReadOnly Property IsPauzed() As Boolean 
 Get 
Return hasBeenPauzed 
 End Get 
 End Property 
#End Region 
End Class 
#End Region


Other 26 submission(s) by this author

 

 
 Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:
 
Your Vote!

What do you think of this code(in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor See Voting Log
 
Other User Comments

 There are no comments on this submission.
 
Add Your Feedback!

Note:Not only will your feedback be posted, but an email will be sent to the code's author from the email account you registered on the site, so you can correspond directly.

NOTICE: The author of this code has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular code, please click here.
 
To post feedback, first please login.