It is currently April 19th, 2024, 8:40 am

Changing a font's color based on the value of a measure

Tips and Tricks from the Rainmeter Community
User avatar
jsmorley
Developer
Posts: 22629
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Changing a font's color based on the value of a measure

Post by jsmorley »

Folks often ask how they can have the color of an element in Rainmeter change color based on the value of a measure. For instance, have the display of a CPU meter turn red if the CPU percent used goes over some amount. This skin demonstrates how you can do this. It uses NetIn as a sample, but the idea behind it can be used for pretty much any meter that takes a color as a parameter.

Code: Select all

[Rainmeter]
Update=500
DynamicWindowSize=1

[Variables]
NetInColor=255,255,255,255

[MeasureNetworkIn]
Measure=NetIn
IfAboveValue=1023
IfAboveAction=!RainmeterSetVariable NetInColor 255,0,0,255
IfBelowValue=1024
IfBelowAction=!RainmeterSetVariable NetInColor 255,255,255,255

[MeasureDelayNetIn]
Measure=Calc
Formula=MeasureNetworkIn

[MeterNetworkIn]
Meter=String
MeasureName=MeasureDelayNetIn
W=200
H=35
X=200
Y=0
StringAlign=RIGHT
FontColor=#NetInColor#
FontSize=25
FontFace=Trebuchet MS
AntiAlias=1
NumOfDecimals=1
AutoScale=1
DynamicVariables=1
Text="%1B"
Let's describe what is going on:

1) I set the [Variable] NetInColor=255,255,255,255 to "initialize" it to something, otherwise the first time it is used, before the first !RainmeterSetVariable happens, it will default to 0,0,0,255

2) I get the NetIn value. Dont' forget it is in bytes, not what it shows in the meter where it is autoscaled. So if it is 1k, the measure actually has 1024 in it.

3) I use IfAboveValue and IfBelowValue to execute a !RainmeterSetVariable to change the font color.

4) I then have to do a bit of a shell game. The issue is that during a single Update= cycle, the FontColor variable is set and the meter is dislpayed, but the color is always one cycle behind. Not a big deal normally, but on a network meter or a cpu meter where it jumps around a lot, the color change will be "out of sync" with the number displayed.

So we have to "delay" the display of the "number" by one cycle, so it stays in sync with the color. We do that by creating a simple calc measure that gets the value from the NetIn Measure as the "result" of a formula. Then use the value of the calc measure in the meter instead of directly using the value of the NetIn measure. That delays the display of the number in the text meter by one cycle, and the color and number will match up.

So this will display the number in white if it is below 1k, and in red if at or above 1k.
wasky1
Posts: 127
Joined: June 10th, 2009, 8:26 am

Re: Changing a font's color based on the value of a measure

Post by wasky1 »

this is great information. i would never have thaught of a delay, thanks V much.
Rain_Seek3r
Posts: 9
Joined: April 1st, 2010, 5:45 pm

Re: Changing a font's color based on the value of a measure

Post by Rain_Seek3r »

jsmorley

In font color All i did was this and it works
ex.
FontColor= Green 10

and i got a color i wanted is that what your post telling me to do??
wasky1
Posts: 127
Joined: June 10th, 2009, 8:26 am

Re: Changing a font's color based on the value of a measure

Post by wasky1 »

this is a simple explination how to have a meter change color depending on a value, and describing how to make it appear more responsive to the value change by using a "delay"
hope this helps
Archer
Posts: 1
Joined: June 23rd, 2010, 1:09 pm

Re: Changing a font's color based on the value of a measure

Post by Archer »

Would this technique work with a meter, say a polar clock?

Here's a link to a thread I posted asking what I want to do specifically.

http://rainmeter.net/forum/viewtopic.php?f=28&t=4494
Miyako
Posts: 7
Joined: November 10th, 2009, 4:17 pm
Location: Antwerp, Belgium

Re: Changing a font's color based on the value of a measure

Post by Miyako »

Will it also work when the measure-value is a string parsed by the webparser?
User avatar
Alex2539
Rainmeter Sage
Posts: 642
Joined: July 19th, 2009, 5:59 am
Location: Montreal, QC, Canada

Re: Changing a font's color based on the value of a measure

Post by Alex2539 »

It will, as long as the string is in the proper format (ie: R,G,B,A). In that case, it is easier to skip the IfAbove/Below actions and !RainmeterSetVariable, and use the value directly, like so:

Code: Select all

Meter=String
Text=This is some text
FontColor=[WebParserMeasure]
DynamicVariables=1
Here, if [WebParserMeasure] is returning something like "255,128,0,255" as its value, the text will turn orange.
ImageImageImageImage
Miyako
Posts: 7
Joined: November 10th, 2009, 4:17 pm
Location: Antwerp, Belgium

Re: Changing a font's color based on the value of a measure

Post by Miyako »

O, because I was parsing a website which is using <font color='red'> or <font color='green'> and I had some problems by putting the same colors in rainmeter.
But I could use something like this?

Code: Select all

Substitute="red":"255, 0, 0, 255","green":"0, 128, 0"
User avatar
Chewtoy
Moderator
Posts: 995
Joined: June 10th, 2009, 12:44 pm
Location: Sweden

Re: Changing a font's color based on the value of a measure

Post by Chewtoy »

Miyako wrote:O, because I was parsing a website which is using <font color='red'> or <font color='green'> and I had some problems by putting the same colors in rainmeter.
But I could use something like this?

Code: Select all

Substitute="red":"255, 0, 0, 255","green":"0, 128, 0"
Yes.
I don't think, therefore I'm not.
User avatar
Scolex
Posts: 111
Joined: July 31st, 2010, 8:52 am

Re: Changing a font's color based on the value of a measure

Post by Scolex »

A couple of questions;
Does this also work for graphs?
Is there support for more than 2 colors, for example: CPU temp 0-30° = color x, 31-50° = color y, 50°-∞ = color z

I just started using RM today so I have no idea what it's capabilities are. I have started playing around already though and created a skin to utilize the registry entries of Everest Ultimate.