It is currently April 20th, 2024, 4:40 pm

[Solved]Is it possible to recalculate a calc measure?

Get help with creating, editing & fixing problems with skins
JorgeSM
Posts: 5
Joined: January 9th, 2017, 5:03 pm

[Solved]Is it possible to recalculate a calc measure?

Post by JorgeSM »

Hi.
I'm making a weather widget and I want to use a gradient to show temperature in a bar graph.
What I have now (and works) is this:

Code: Select all

[mTemp0]
Measure=Plugin
Plugin=Plugin\WebParser.dll
Url=[mWunderXML]
StringIndex=56
MinValue=#Meteo.TMin#
MaxValue=#Meteo.TMax#

[mTemp1]
Measure=Plugin
Plugin=Plugins\WebParser.dll
Url=[mWunderXML]
StringIndex=59
MinValue=#Meteo.TMin#
MaxValue=#Meteo.TMax#
...
[mtR0]
Measure=calc
Formula=mTemp0 <= ((#Meteo.TMax# - #Meteo.TMin#) / 2 + #Meteo.TMin#) ? ((mTemp0 - #Meteo.TMin#) * 255 / (#Meteo.TMax# - #Meteo.TMin#)) * 2 : 255
[mtG0]
Measure=calc
Formula=mTemp0 <= ((#Meteo.TMax# - #Meteo.TMin#) / 2 + #Meteo.TMin#) ? 255 : (255 - ((mTemp0 - #Meteo.TMin#) * 255 / (#Meteo.TMax# - #Meteo.TMin#))) * 2
[mtB0]
Measure=calc
Formula=mTemp0 <= ((#Meteo.TMax# - #Meteo.TMin#) / 2 + #Meteo.TMin#) ? 255 - ((mTemp0 - #Meteo.TMin#) * 255 / (#Meteo.TMax# - #Meteo.TMin#)) * 2 : 0

[mtR1]
Measure=calc
Formula=mTemp1 <= ((#Meteo.TMax# - #Meteo.TMin#) / 2 + #Meteo.TMin#) ? ((mTemp1 - #Meteo.TMin#) * 255 / (#Meteo.TMax# - #Meteo.TMin#)) * 2 : 255
[mtG1]
Measure=calc
Formula=mTemp1 <= ((#Meteo.TMax# - #Meteo.TMin#) / 2 + #Meteo.TMin#) ? 255 : (255 - ((mTemp1 - #Meteo.TMin#) * 255 / (#Meteo.TMax# - #Meteo.TMin#))) * 2
[mtB1]
Measure=calc
Formula=mTemp1 <= ((#Meteo.TMax# - #Meteo.TMin#) / 2 + #Meteo.TMin#) ? 255 - ((mTemp1 - #Meteo.TMin#) * 255 / (#Meteo.TMax# - #Meteo.TMin#)) * 2 : 0
...
[MeterTemp0]
Meter=Bar
MeasureName=mTemp0
x=20
Y=10
h=36
w=20
BarColor=[mtR0],[mtG0],[mtB0],192
BarOrientation=Vertical
DynamicVariables=1

[MeterTemp1]
Meter=Bar
MeasureName=mTemp1
x=21r
Y=10
h=36
w=20
BarColor=[mtR1],[mtG1],[mtB1],192
BarOrientation=Vertical
DynamicVariables=1
The code repeats for 12 bars.

Now, if I want to change the gradient formula, I have to change it in all the measures. Is it possible to change the value of a variable and recalculate the formula to apply in the next measure? Something like

Code: Select all

[mtR]
Measure=calc
Formula=mTemp <= ((#Meteo.TMax# - #Meteo.TMin#) / 2 + #Meteo.TMin#) ? ((mTemp - #Meteo.TMin#) * 255 / (#Meteo.TMax# - #Meteo.TMin#)) * 2 : 255
[mtG]
Measure=calc
Formula=mTemp <= ((#Meteo.TMax# - #Meteo.TMin#) / 2 + #Meteo.TMin#) ? 255 : (255 - ((mTemp - #Meteo.TMin#) * 255 / (#Meteo.TMax# - #Meteo.TMin#))) * 2
[mtB]
Measure=calc
Formula=mTemp <= ((#Meteo.TMax# - #Meteo.TMin#) / 2 + #Meteo.TMin#) ? 255 - ((mTemp - #Meteo.TMin#) * 255 / (#Meteo.TMax# - #Meteo.TMin#)) * 2 : 0

[MeterTemp0]
Meter=Bar
MeasureName=mTemp0
x=20
Y=10
h=36
w=20
!SetVariable mTemp = mTemp0
BarColor=[mtR],[mtG],[mtB],192
BarOrientation=Vertical
DynamicVariables=1

[MeterTemp1]
Meter=Bar
MeasureName=mTemp1
x=21r
Y=10
h=36
w=20
!SetVariable mTemp = mTemp1
BarColor=[mtR],[mtG],[mtB],192
BarOrientation=Vertical
DynamicVariables=1
Thanks in advance
Jorge
Last edited by JorgeSM on January 10th, 2017, 2:35 pm, edited 1 time in total.
JorgeSM
Posts: 5
Joined: January 9th, 2017, 5:03 pm

Re: Is it possible to recalculate a calc measure?

Post by JorgeSM »

Hi,
Just to let you know that I solved it with a lua script, and now I have:

Code: Select all

[mTemp0]
Measure=Plugin
Plugin=Plugin\WebParser.dll
Url=[mWunderXML]
StringIndex=56
MinValue=#Meteo.TMin#
MaxValue=#Meteo.TMax#

[mTemp1]
Measure=Plugin
Plugin=Plugins\WebParser.dll
Url=[mWunderXML]
StringIndex=59
MinValue=#Meteo.TMin#
MaxValue=#Meteo.TMax#
...
[sTemp0]
Measure=Script
ScriptFile="#CURRENTPATH#TempColor.lua"
CurrTemp=mTemp0
DestTemp=MeterTemp0

[sTemp1]
Measure=Script
ScriptFile="#CURRENTPATH#TempColor.lua"
CurrTemp=mTemp1
DestTemp=MeterTemp1
...
[MeterTemp0]
Meter=Bar
MeasureName=mTemp0
x=20
Y=20
h=50
w=15
BarOrientation=Vertical
DynamicVariables=1

[MeterTemp1]
Meter=Bar
MeasureName=mTemp1
x=16r
Y=20
h=50
w=15
BarOrientation=Vertical
DynamicVariables=1
My lua script is:

Code: Select all


function Initialize()
	sCurrTemp = SELF:GetOption('CurrTemp')
	sDestTemp = SELF:GetOption('DestTemp')
	msCurrTemp = SKIN:GetMeasure(sCurrTemp)
end -- function Initialize

-- ## Update: ###################################################################
function Update()

	iMaxTemp = tonumber(SKIN:GetVariable("Meteo.TMax"))
	iMinTemp = tonumber(SKIN:GetVariable("Meteo.TMin"))
	iVarAlfa = tonumber(SKIN:GetVariable("VarAlfa"))
	iCurrTemp = tonumber(msCurrTemp:GetValue())
	
	sColor = GetG01(iCurrTemp, iMaxTemp, iMinTemp)
	SKIN:Bang('!SetOption '..sDestTemp..' BarColor '..sColor..','..iVarAlfa)

end -- function Update

-- ## Custom Functions: #########################################################
GetG01 = function(iTCurr, iTMax, iTMin)

	local iR = 0
	local iG = 0
	local iB = 0
	
	if iTCurr <= ((iTMax - iTMin) / 2 + iTMin) then
		iR = ((iTCurr - iTMin) * 255 / (iTMax - iTMin)) * 2
		iG = 255
		iB = 255 - ((iTCurr - iTMin) * 255 / (iTMax - iTMin)) * 2
	else
		iR = 255
		iG = (255 - ((iTCurr - iTMin) * 255 / (iTMax - iTMin))) * 2
		iB = 0
	end
	
	return iR..","..iG..","..iB
	
end -->GetG01
Now I can add as many gradient functions as I like and use the one I want in the Update() function.