It is currently May 2nd, 2024, 9:30 am

Battery display color change help (SOLVED)

Get help with creating, editing & fixing problems with skins
Torric
Posts: 6
Joined: September 1st, 2010, 9:03 pm

Battery display color change help (SOLVED)

Post by Torric »

First thing's first: credit goes to Kaelri for the Enigma skin (from where I borrowed the code for the power display).

That being said.
I'm trying to reconfigure the skin I'm working on so that the percent values change color depending on the percent left in the battery. Like this:

Power 100
Power 82
Power 34
Power 7

I was thinking about having 100% blue, 99 - 50% green, 49 - 11% yellow, and 10 - 1% red.
Here's the code for anyone who wants to take a look:

Code: Select all

;------------------------------------------------------------------------------------------------

[Rainmeter]
Author=Torric
AppVersion=1000
Update=1000

[Metadata]
Name=
Config=
Description=
Version=
Tags=
License=
Preview=

;----------------------------------------------------------
; Variables

Fontname=Segoe UI Light
FontSize=14

;----------------------------------------------------------
; MEASURES

[MeasurePower]
Measure=Plugin
Plugin=Plugins\PowerPlugin.dll
PowerState=PERCENT


;----------------------------------------------------------
; METERS

[PWR]
Meter=STRING
FontSize=14
FontColor=0, 0, 255, 255
FontFacee=#Fontname#
MeasureName=MeasurePower
X=75
AnitAlias=1

[Label PWR]
Meter=STRING
FontFace=#Fontname#
FontSize=14
Text="Power"
AntiAlias=1

[MeterPower]
MeasureName=MeasurePower
Meter=BAR
X=0
Y=18
W=110
H=20
BarColor=255,255,255,70
SolidColor=255,255,255,70
BarOrientation=HORIZONTAL

[MeterPowerBarBack]
Meter=IMAGE
X=0r
Y=0r
Last edited by Torric on October 31st, 2010, 8:01 pm, edited 1 time in total.
User avatar
Alex2539
Rainmeter Sage
Posts: 642
Joined: July 19th, 2009, 5:59 am
Location: Montreal, QC, Canada

Re: Battery display color change help

Post by Alex2539 »

What you need to do is add a new Calc measure to check what the value of the battery level is, then spit out a value depending on it. For that, you would use conditional statements, then substitute the values for colours. It would looks like this:

Code: Select all

[powerColor]
Measure=Calc
Formula=([MeasurePower] = 100 ? 1 : ([MeasurePower] > 49 ? 2 : ([MeasurePower] > 10 ? 3 : 4)))
Substitute=".0":"", "2":"0,255,0,255", "3":"255,255,0,255", "4":"255,0,0,255", "1":"0,0,255,255"
DynamicVariables=1
Then, just switch out the colour of the text with a the powerColor measure as a dynamic variable, like so:

Code: Select all

[PWR]
Meter=STRING
FontSize=14
FontColor=[powerColor]
FontFacee=#Fontname#
MeasureName=MeasurePower
X=75
AntiAlias=1
DynamicVariables=1
The text should now change colour according to the battery level.
ImageImageImageImage
Torric
Posts: 6
Joined: September 1st, 2010, 9:03 pm

Re: Battery display color change help

Post by Torric »

I tweaked toe code a little and got this:

Code: Select all

[PowerColor]
Measure=Calc
Formula=([MeasurePower] < 11 ? 4 : ([MeasurePower] < 50 ? 3 : ([MeasurePower] < 100 ? 2 : [MeasurePower] = 100 ? 1))))
Substitute="1":"0, 0, 255, 255", "2":"0, 255, 0, 255", "3":"255, 255, 0, 255", "4":"255, 0, 0, 255"
DynamicVariables=1

;----------------------------------------------------------
; METERS

[PWR]
Meter=STRING
FontSize=14
FontColor=PowerColor
FontFacee=#Fontname#
MeasureName=MeasurePower
X=75
AnitAlias=1
DynamicVariables=1
Now, for some oddball reason, the text below 100% shows up as a light blue, not green :?
-Turns out that editing the alpha of the bar color meter:

Code: Select all

[MeterPower]
MeasureName=MeasurePower
Meter=BAR
X=0
Y=18
W=110
H=20
BarColor=0, 255, 0, 255
SolidColor=0, 255, 0, 70
BarOrientation=HORIZONTAL
Also effects the alpha of the percentage display...

Edit: Funny colors seem to take effect during manual refresh. Probably a conflict with the auto update?
User avatar
Alex2539
Rainmeter Sage
Posts: 642
Joined: July 19th, 2009, 5:59 am
Location: Montreal, QC, Canada

Re: Battery display color change help

Post by Alex2539 »

No, it's because you've made a mistake in the formula, so the Calc measure is returning zero all the time. You meter doesn't understand the colour "0", so it gives you some random colour instead. Your problem is at the end. A conditional statement works like this:

(true/false statement) ? (value if it is true) : (value if it is false)

You need to have all three parts; conditionals are always an if/then/else statement, no more no less. Your formula can be broken down like this:

Code: Select all

IF [MeasurePower] < 11
    RETURN 4
ELSE
    IF [MeasurePower] < 50
        RETURN 3
    ELSE
        IF [MeasurePower] < 100
            RETURN 2
        ELSE
            IF [MeasurePower] = 100
                RETURN 1
            ELSE
Your last conditional is missing the "ELSE". However, if you think about it, by the time you get to that statement, you have already run through every possibility except if it is equal to 100. Because it is the only possibility left, you don't need to test for it. It is necessarily true. Change your formula to this and it should work:

Code: Select all

Formula=([MeasurePower] < 11 ? 4 : ([MeasurePower] < 50 ? 3 : ([MeasurePower] < 100 ? 2 : 1)))
One other problem that you're going to hit is when the value is in fact 100. Because of the way you have rearranged the Substitute statement, the colour will be invalid. I realize that you put them in order, but I have the substitution for "2" at the start for a reason. When the substitution is performed, it doesn't look for that single value in the original string and just replace that, it performs each substitution in order. So, if the value returned from the measure is 1, the following substitutions will take place: "1" will be matched first and become "0, 0, 255, 255", then the "2"s in the new string will be matched and the value will become ""0, 0, 0, 255, 0, 25555, 0, 255, 0, 25555". To fix this, all you need to do is rearrange the substitutions in a way that they won't interfere with each other. The simplest way to do that is to just put the substitution for "2" first.
ImageImageImageImage
Torric
Posts: 6
Joined: September 1st, 2010, 9:03 pm

Re: Battery display color change help

Post by Torric »

Now I have this code for the Calc:

Code: Select all

[PowerColor]
Measure=Calc
Formula=([MeasurePower] < 11 ? 4 : ([MeasurePower] < 50 ? 3 : ([MeasurePower] < 100 ? 2 : 1)))
Substitute="2":"0, 255, 0, 255", "3":"255, 255, 0, 255", "4":"255, 0, 0, 255", "1":"0, 0, 255, 255"
DynamicVariables=1
But I'm still not getting the normal colors.
Is it possible to just bypass the Substitute part and the color code within the Formula?

Formula=([MeasurePower] < 11 ? FontColor=255, 0, 0, 255 : ([MeasurePower] < 50 ? FontColor=255, 255, 0, 255 : ([MeasurePower] < 100 ? FontColor=0. 255, 0, 255 : FontColor=0, 0, 255, 255)))
User avatar
jsmorley
Developer
Posts: 22631
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Battery display color change help

Post by jsmorley »

No, you can't combine calculations and assignments like that.

As calcs are resolved to 5 decimal places, try changing your Substitute like this:

Substitute=".00000":"","2":"0, 255, 0, 255", "3":"255, 255, 0, 255", "4":"255, 0, 0, 255", "1":"0, 0, 255, 255"

This issue does not matter when you are using a calc as a "value", as 3 is the same as 3.00000. However, when you want to use a value from a calc with a Substitute to create a "string", it does matter.

(I didn't test this as I am not on a battery)
Torric
Posts: 6
Joined: September 1st, 2010, 9:03 pm

Re: Battery display color change help

Post by Torric »

Here's my current settings:

Code: Select all

;----------------------------------------------------------

[Rainmeter]
Author=Torric
AppVersion=1000
Update=1000

[Metadata]
Name=
Config=
Description=
Version=
Tags=
License=
Preview=

;----------------------------------------------------------
; Variables

Fontname=Segoe UI Light
FontSize=14

;----------------------------------------------------------
; MEASURES

[MeasurePower]
Measure=Plugin
Plugin=Plugins\PowerPlugin.dll
PowerState=PERCENT

[PowerColor]
Measure=Calc
Formula=([MeasurePower] < 11 ? 4 : ([MeasurePower] < 50 ? 3 : ([MeasurePower] < 100 ? 2 : 1)))
Substitute=".00000":"","2":"0, 255, 0, 255", "3":"255, 255, 0, 255", "4":"255, 0, 0, 255", "1":"0, 0, 255, 255"
DynamicVariables=1

;----------------------------------------------------------
; METERS

[PWR]
Meter=STRING
FontSize=14
FontColor=PowerColor
FontFacee=#Fontname#
MeasureName=MeasurePower
X=75
AnitAlias=1
DynamicVariables=1

[Label PWR]
Meter=STRING
FontFace=#Fontname#
FontSize=14
Text="Power"
AntiAlias=1

[MeterPower]
MeasureName=MeasurePower
Meter=BAR
X=0
Y=18
W=110
H=20
BarColor=128, 0, 0, 70
SolidColor=128, 0, 0, 70
BarOrientation=HORIZONTAL

[MeterPowerBarBack]
Meter=IMAGE
X=0r
Y=0r
W=110
H=20
SolidColor=128, 0, 0, 20
When I manually refresh the skin, the percent changes between a light blue and purple.
User avatar
jsmorley
Developer
Posts: 22631
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Battery display color change help

Post by jsmorley »

Just change this:

FontColor=PowerColor

to:

FontColor=[PowerColor]

You can test this by temporarily replacing your measure for battery power with a hard coded calc statement that you can change and refresh to see the result:

;[MeasurePower]
;Measure=Plugin
;Plugin=Plugins\PowerPlugin.dll
;PowerState=PERCENT

[MeasurePower]
Measure=Calc
Formula=25
Torric
Posts: 6
Joined: September 1st, 2010, 9:03 pm

Re: Battery display color change help

Post by Torric »

IT WORKS!!

Thanks a million, all!