It is currently March 29th, 2024, 3:14 pm

Changing color of font based on a value

Get help with creating, editing & fixing problems with skins
Coolancs
Posts: 6
Joined: October 28th, 2016, 8:41 am

Changing color of font based on a value

Post by Coolancs »

Hey everyone!

I'm working on my first skin, it's a ticker for cryptocurrencies. I wanted to display the latest changes in price and tried to color it based on the value of the change. For example, 0.5 should be green, -1.24 should be red.

I've tried using variables, that didn't work either, below is my most recent try, any help would be appreciated :)

Code: Select all

[GetBTCChange]
Update=1000
Measure=Plugin
Plugin=WebParser
URL=https://api.coinmarketcap.com/v1/ticker/bitcoin/
RegExp=(?siU)percent_change_1h": "(.*)",.*

[BTCChange]
Update=1000
Measure=Plugin
Plugin=WebParser
URL=[GetBTCChange]
StringIndex=1

[ColorChange]
Measure=BTCChange
IfBelowValue=0
IfBelowAction=[!SetOption MeterBTCChange FontColor 255,0,0,128][!Redraw]

[MeterBTCChange]
Update=1000
Meter=String
MeasureName=BTCChange
X=200
Y=0
W=280
H=32
FontFace=Consolas
FontSize=24
FontColor=0,255,0,128
SolidColor=0,0,0,1
Padding=0,0,0,0
StringAlign=Left
AntiAlias=1
mak_kawa
Posts: 908
Joined: December 30th, 2015, 9:47 am

Re: Changing color of font based on a value

Post by mak_kawa »

I didn't test this code actually, but maybe works, maybe. :-p

Code: Select all

[Rainmeter]
Update=1000

[GetBTCChange]
Measure=Plugin
Plugin=WebParser
URL=https://api.coinmarketcap.com/v1/ticker/bitcoin/
RegExp=(?siU)percent_change_1h": "(.*)",.*
UpdateRate=600

[BTCChange]
Measure=Plugin
Plugin=WebParser
URL=[GetBTCChange]
StringIndex=1
IfCondition=BTCChange <0
IfTrueAction=[!SetOption MeterBTCChange FontColor "255,0,0,128"][!Redraw]
IfCondition2=(BTCChange >=0) && (BTCChange < 1)
IfTrueAction2=[!SetOption MeterBTCChange FontColor "0,255,0,128"][!Redraw]
IfCondition3=BTCChange >=1
IfTrueAction3=[!SetOption MeterBTCChange FontColor "255,255,255,128"][!Redraw]

[MeterBTCChange]
Meter=String
MeasureName=BTCChange
X=200
Y=0
W=280
H=32
FontFace=Consolas
FontSize=24
FontColor=255,255,255,128
SolidColor=0,0,0,1
Padding=0,0,0,0
StringAlign=Left
Text=%1
AntiAlias=1
When BTCChange is below 0, text color is red, 0 to 1, green, above 1, grey. Modify as you like. Read interval from the website is 10 minutes (600 seconds).
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Changing color of font based on a value

Post by jsmorley »

Just an unrelated FYI, !Redraw by itself does pretty much absolutely nothing. !Redraw should always and only be used after !UpdateMeter or !UpdateMeterGroup.

See the way Rainmeter works is that in each Update cycle it first updates measures, then it updates meters, then it redraws the skin window. If you are trying to have some action that changes a meter happen "now", rather than waiting for the next Update cycle, (which is something you will often want to do with mouse actions) you need to manually update the meters before you force the redraw, or the redraw will just be done with the old meter settings and values.

It's broken out this way for a reason. You might just use !Update and update the entire skin "now", but that is pretty brute-force, and will update ALL measures and ALL meters, and then redraw, when you might be making a change to only one meter. So we break the redraw out to its own thing, so you can do very targeted and efficient forced updates.

[!UpdateMeter MeterOne][!Redraw]
[!UpdateMeter MeterOne][!UpdateMeter MeterTwo][!Redraw]
[!UpdateMeter *][!Redraw]

In any case:

[!SetOption MeterOne FontSize "12"][!Redraw]

Will change the FontSize option on the meter to 12, and then redraw the skin window using whatever the FontSize was before the change. You haven't updated the meter to "use" the new setting yet.

[!SetOption MeterOne FontSize "12"][!UpdateMeter MeterOne][!Redraw]

Is what you want...

So the rule is: There is no point to !UpdateMeter without !Redraw, and there is no point to !Redraw without !UpdateMeter.
mak_kawa
Posts: 908
Joined: December 30th, 2015, 9:47 am

Re: Changing color of font based on a value

Post by mak_kawa »

Hi jsmorley.

Thank you for your detailed guidence. I have heard about this a while ago on this forum from you...and forgot. :-)
Coolancs
Posts: 6
Joined: October 28th, 2016, 8:41 am

Re: Changing color of font based on a value

Post by Coolancs »

mak_kawa wrote:I didn't test this code actually, but maybe works, maybe. :-p

Code: Select all

[Rainmeter]
Update=1000

[GetBTCChange]
Measure=Plugin
Plugin=WebParser
URL=https://api.coinmarketcap.com/v1/ticker/bitcoin/
RegExp=(?siU)percent_change_1h": "(.*)",.*
UpdateRate=600

[BTCChange]
Measure=Plugin
Plugin=WebParser
URL=[GetBTCChange]
StringIndex=1
IfCondition=BTCChange <0
IfTrueAction=[!SetOption MeterBTCChange FontColor "255,0,0,128"][!Redraw]
IfCondition2=(BTCChange >=0) && (BTCChange < 1)
IfTrueAction2=[!SetOption MeterBTCChange FontColor "0,255,0,128"][!Redraw]
IfCondition3=BTCChange >=1
IfTrueAction3=[!SetOption MeterBTCChange FontColor "255,255,255,128"][!Redraw]

[MeterBTCChange]
Meter=String
MeasureName=BTCChange
X=200
Y=0
W=280
H=32
FontFace=Consolas
FontSize=24
FontColor=255,255,255,128
SolidColor=0,0,0,1
Padding=0,0,0,0
StringAlign=Left
Text=%1
AntiAlias=1
When BTCChange is below 0, text color is red, 0 to 1, green, above 1, grey. Modify as you like. Read interval from the website is 10 minutes (600 seconds).
Works perfectly, thanks a lot! =)