DesktopX: Scripting Color (Beginner)

From WinCustomize Wiki

Jump to: navigation, search
Scripting Color
Original Author:sViz
Date Created:September 8, 2007
Application:DesktopX
Programs Used:DesktopX


Contents

[edit] Scripting Color

You might be thinking "A widget already has preferences where you can adjust the hue etc." True, but sometimes that just doesn’t work out right:

Image:Dx colorPreferencesHue.png

Sure, the colors change, but in a wonky, un-uniformed way. One of the benefits of changing a widget’s color through scripting is that you can choose which parts to change and which specific colors to change it to. As you can see, with scripting we’ve only changed the hue of the menu background leaving the icons intact:

Image:Dx colorPreferencesHue2.png

I’m going to show you two scripts for this. One which will select specific colors on left-click and another which will go through the entire spectrum of colors on left-click.

Create a new object, name it “object1” and apply the image below.

Image:Redcard.png

In the General tab, click on ‘new’ where it says Script. For this tutorial, we want to make sure the object starts out at its normal color OnScriptEnter. We do this with object.hue. All you’ll really be setting is the hue shift. Just like you would in the States > Color tab.

'Called when script is executed
Sub Object_OnScriptEnter
	Object.hue = 0
End Sub

[edit] Select Hue shift

Now, let’s say for some reason I decide my widget looks best only in red, green, or blue. I can create a script that changes the object’s hue to only those three colors. This will be accomplished by the OnLButtonUp function. OnLButtonUp is a function that is run only when the user clicks and releases the left mouse button on the object. Now what do we want to happen on left-click? Well we want to check the current hue of the object and then change it to the next color. If it is red (0, the original color), change it to green (80). If it’s green, change it to blue (150), and if it’s blue change it back to red (0).

'Called when L-click is released
Function Object_OnLButtonUp(x, y, dragged)
	If Not dragged Then
		'Check current hue
		Select Case Object.hue
			Case 0
				'Store the next hue in a variable
				hueshift = 80 
			Case 80
				hueshift = 150
			Case 150
				hueshift = 0
		End Select
		'Set the object hue
		object.hue = hueshift
	End If
End Function

In the command bar of the DesktopX Script Editor click on File > Save and Apply. L-click on the widget to try it out.

[edit] Increment Hue shift

Now supposing I don’t really care what color the object is changed to and I want to let the user choose from the entire spectrum of colors; whatever they want. Still using the OnLButtonUp function we can increment the hue shift instead of setting specific colors. Here, we'll increase the amount of hueshift on each click until it reaches the end of the spectrum, and then it will start over again. Just replace the OnLButtonUp procedure with this one:

'Called when L-click is released
Function Object_OnLButtonUp(x, y, dragged)
	If Not dragged Then
		'Store the current hue in variable
		hueshift = Object.hue
		'If current hue is equal to or greater than 255
		'reset to 0
		If hueshift => 255 Then
			hueshift = 0
		'If hue is not yet 255, keep adding
		Else
			hueshift = hueshift + 7
		End If
		'Set the object hue
		object.hue = hueshift
	End If
End Function


[edit] White and Black

The colors white and black are achieved through brightness, not hue. Object.brightness = 255 is completely white and Object.brightness = -255 is completely black. However a complete black/white out may not be the effect you’re looking for:

Image:Dx_colorWhiteBlackout.PNG‎

You may want to create another state with a white or black version of your original image. Then, when the hueshift reaches the end of the spectrum have the state change to the white state. In another click have it change to the black state, and in another click have it change back to the original state. This is accomplished with more conditional statements. If the object is in the original state, l-clicking on the object will increment hueshift until it cycles through all the colors then it changes the object state to the white state. If the object is in the white state, l-clicking the object will change it to the black state, after which it will change back to the original state.

Here are the black and white images. Here are the scripts:

SELECT HUE SHIFT

'Called when L-click Is released
Function Object_OnLButtonUp(x, y, dragged)
	If Not dragged Then
		If object.state = "original" Then
			'Check current hue
			Select Case Object.hue
				Case 0
					'Store the next hue in a variable
					hueshift = 80 
				Case 80
					hueshift = 150
				Case 150
					'Store new state and hue in variables
					newstate = "white"
					hueshift = 0
			End Select
		ElseIf object.State = "white" Then
			newstate = "black"
		ElseIf object.State = "black" Then
			newstate = "original"
		End If
		'Set the object hue and state
		object.hue = hueshift
		object.State = newstate
	End If
End Function

INCREMENT HUE SHIFT

'Called when L-click is released
Function Object_OnLButtonUp(x, y, dragged)
	If Not dragged Then
		'Store the current hue in variable
		hueshift = Object.hue
		If object.State = "original" Then
			'If current hue is equal to or greater than 255
			'reset to 0 and store next state
			If hueshift => 240 Then
				hueshift = 0
				newstate = "white"
			'If hue is not yet 255, keep adding
			Else
				hueshift = hueshift + 7
			End If
		ElseIf object.State = "white" Then
			newstate = "black"
		ElseIf object.State = "black" Then
			newstate = "original"
		End If
		'Set the object hue and state
		object.hue = hueshift
		object.State = newstate
	End If
End Function

[edit] Text Objects

You can shift the hue of text objects the same as with regular objects, but with text objects, you can set the text color itself.

Object.TextColor = vbRed

The value vbRed is a predefined vbscript color constant. You can find more of them here: Vbscript Color Constants

You can also use RGB values.

Object.TextColor = RGB(255,0,0)

A list of RGB color values can be found here: RGB Color Values


Next Tutorial: DesktopX: Scripting Time Displays (Beginner)

[edit] Tips

  • Does your object have more than one state that needs to be changed? You can control all states of your object like this:
'Set the object hue
DesktopX.Object("object1").states("").hue = hueshift
  • Need to control the color of another object? Anywhere it says object.hue replace it with:

DesktopX.Object("anotherobject").hue

  • Want to change the color of several objects of the same color? Try grouping them and using a For Each loop:
'Set the objects hue
For Each elem In DesktopX.GroupObjects("group1")
	elem.hue = hueshift
Next

[edit] Further Reading