It is currently March 28th, 2024, 7:57 am

[Script] ColorChanger

Discuss the use of Lua in Script measures.
Post Reply
User avatar
smurfier
Moderator
Posts: 1931
Joined: January 29th, 2010, 1:43 am
Location: Willmar, MN

[Script] ColorChanger

Post by smurfier »

ColorChanger is a script that transitions between colors based on the value of a measure.

Settings:
  • MeasureName
    • The name of the measure used by the script.
  • Colors
    • A pipe delimited list of the colors to be used in the RGBA format. The colors are used in order from left to right.
  • MinimumValue
    • The minimum value of the measure. Defaults to 0.
  • MaximumValue
    • The maximum value of the measure. Defaults to 1.

Code: Select all

-- ColorChanger v1.3 by Smurfier (smurfier20@gmail.com)
-- This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 License.

PROPERTIES={Colors='';MeasureName='';MinimumValue=0;MaximumValue=1;}

function Initialize()
	MinValue,Colors,Measure=tonumber(PROPERTIES.MinimumValue),{},SKIN:GetMeasure(PROPERTIES.MeasureName)
	for a=1,4 do Colors[a]={} end
	if string.len(PROPERTIES.Colors)==0 or not string.match(PROPERTIES.Colors,'%d+%s-,%s-%d+%s-,%s-%d+') then
		print('ColorChanger: Invalid color string')
	elseif not Measure then
		print('ColorChanger: Cannot retrieve Measure '..PROPERTIES.MeasureName)
	else
		for a in string.gmatch(PROPERTIES.Colors,'[^%|]+') do
			local b={}
			for c in string.gmatch(a,'[^,]+') do table.insert(b,tonumber(c)) end
			for d=1,4 do table.insert(Colors[d],b[d] and b[d] or 255) end
		end
		Divider=math.ceil((tonumber(PROPERTIES.MaximumValue)-MinValue)/(#Colors[1]-1))
	end
end

function Update()
	if #Colors[1]<2 or not Measure then
		return #Colors[1]==1 and string.gsub(PROPERTIES.Colors,'|','') or '255,255,255,255'
	else
		local rValue,uColor=Measure:GetValue()-MinValue,{}
		Num=math.floor(rValue/Divider)
		for i=1,4 do table.insert(uColor,Average(Colors[i][Num+1],Colors[i][Num+2],rValue%Divider,Divider)) end
		return table.concat(uColor,',')
	end
end

function Average(a,b,c,d) return (a*(d-c)+(b and b or 0)*c)/d end
Example of use:

Code: Select all

[Calc]
Measure=Calc
Formula=Calc%100+1

[Lua]
Measure=Script
ScriptFile=ColorChanger.lua
Colors=255,0,0|0,255,0|0,0,255
MeasureName=Calc
MaximumValue=100

[Image]
Meter=Image
H=100
W=100
SolidColor=[Lua]
DynamicVariables=1
RedAngel121
Posts: 3
Joined: October 2nd, 2016, 2:45 pm

Re: [Script] ColorChanger

Post by RedAngel121 »

Hello! I found this today and want to use it for my desktop. There are 2 issues one of which i have fixed but the other is the speed in which the color change happens. Is there a way to speed up the change of colors? right now it changes colors once per second. but i would like it to change 10 colors per sec.


BTW the issue I fixed was the color would stop at bright blue and switch to red without the purple.

Colors=255,0,0|0,255,0|0,0,255|255,0,0

^^ this fixes that ^^
RedAngel121
Posts: 3
Joined: October 2nd, 2016, 2:45 pm

Re: [Script] ColorChanger

Post by RedAngel121 »

Youre a lifesaver... i didnt even think of that. :confused:
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: [Script] ColorChanger

Post by jsmorley »

I would caution that setting a skin to Update=16 (which is the lowest effective value) will cause it to use a lot of CPU depending on what you are doing in the skin.

I would be very hesitant to have a skin that has more than a meter or two and uses a Calc measure in a counter-loop and runs a Lua script every 16 milliseconds that changes a color. Just a word to the wise... At a minimum, if you have other measures in the skin, be sure to put an UpdateDivider on them so they are not all hammering the system every 16 milliseconds.
RedAngel121
Posts: 3
Joined: October 2nd, 2016, 2:45 pm

Re: [Script] ColorChanger

Post by RedAngel121 »

I wanted it around 200-300 so 16 isnt going to be an issue but now I have to figure out how to incorporate it in my current setup. I'm attempting to setup a color changing audio visualizer. I'm using Lano visualizer currently and just playing around with it.
Post Reply