Wifi and Battery meters are a staple of laptop users and are incredibly easy to create.
ImageCrop, ImageTint, and all of the other Image related features are a very useful feature of Rainmeter opening a whole new method of creating dynamic images. We're also going to use some neat tricks with DyanamicVariables, Substitution, and Conditional Statements to make the battery indicator change color and blink when the battery is low.
Below is our example skin.
Note: The big reason we're using ImageCrop instead of a Bar meter is that when using an Image with a Bar meter, the Image cannot be scaled.
We need to start by grabbing the image we're going to use.
It's grey because we're going to use ImageTint to change it's color in Rainmeter.
Lets start our code by setting up the basics and some Variables that we're going to use later on.[Variables]
OrigHW is the original Height/Width of the image. We're using a 100x100 image so we only need to define one variable here.OrigHW=100
Sections defines the number of sections in the image.Sections=4
SectionsW is the width of the individual sections. For the purpose of this tutorial this Variable is in the form of a formula.SectionsW= (#OrigHW#/#Sections#)
HW defines the scaled Height/Width of the individual indicators. Once again, we only need one variable since our image is a square.HW=50
Now we need to define the colors that we want to use.BatteryBack=0,0,255,100
BatteryIndic=0,0,255,255
Battery50=255,255,0,255
Battery25=225,0,0,255
WifiBack=0,255,0,100
WifiIndic=0,255,0,255
Now we're going to work on the Wifi meter since it's the more basic code.
First we need to define the measure for the WiFi status.[msWifi]
Measure= Plugin
Plugin= PluginsWiFiStatus.dll
WiFiInfoType=Quality
Now is the fun part. We need to determine the width of the indicator.[cWifi]
Measure=Calc
Formula=ROUND(msWifi/(100/#Sections#))*#SectionsW#
ROUND will round the our value to the nearest whole number. If you want to round up use CEIL, or you can use FLOOR to round down.
Note: If you want to use a bar meter instead of using ImageCrop, use this formula instead: (CurrentValue/MaxValue)*OriginalDimension
Now let's define the background image.[mBack]
Meter=Image
ImageName=Back.png
ImageTint=#WifiBack#
AntiAlias=1
Don't forget to make it the Height and Width of the variable we made earlier.H=#HW#
W=#HW#
We are moving the wifi meter one section to the right, and just a little bit down to make room for the battery indicator.X= (#HW#*0.25)
Y= (#HW#*0.05)
Now we'll create the actual WiFi indicator.[mStat]
Meter=Image
ImageName=Back.png
ImageTint=#WifiIndic#
AntiAlias=1
X=r
Y=r
DynamicVariables=1
At this point the image is still it's original dimensions.ImageCrop=0,0,[cWifi],#OrigHW#
What this means: Start at the top, left and crop out a box that is [cWifi] wide and #OrigHW# tall.
Now to scale it to the height and width we defined earlier.H=#HW#
Since Scaling is done last, we have to now scale the cropped image to the new width. Take the width of the cropped image, divide it by the original width to create a ratio or percent, and multiply it by the new width.W= (#HW#*([cWifi]/#OrigHW#))
That's it. We have a working WiFi meter. Now let's work on the battery meter.
Just like with the WiFi we have to measure the Battery.[msPower]
Measure= Plugin
Plugin= PluginsPowerPlugin.dll
PowerState= Percent
The same thing as before.[cPower]
Measure=Calc
Formula=ROUND(msPower/(100/#Sections#))*#SectionsW#
Another background image.[mBack2]
Meter=Image
ImageName=Back.png
ImageTint=#BatteryBack#
H=#HW#
W=#HW#
AntiAlias=1
We're using the same image so we need to flip it around a bit.ImageFlip=Both
Here's the part where we change the color of the battery indicator based on the percent.[BatteryColor]
Measure=Calc
Formula=msPower>=50?-1: (msPower<25?-3:-2)
Now we turn those values into colors using a substitute. We used negative numbers since they cannot exist in a color code.Substitute="-1":"#BatteryIndic#","-2":"#Battery50#","-3":"#Battery25#"
Just to make things nifty lets have the indicator blink if the power is less than 25.[BatteryBlink]
Measure=Calc
Conditional statements evaluate to 1 (true) or 0 (false) so we can actually use them in calculations.Formula= (msPower<25)*(1-BatteryBlink)
Now let's use our measures to put together the battery indicator.[mStat2]
Meter=Image
ImageName=Back.png
ImageTint=[BatteryColor]
ImageFlip=Both
AntiAlias=1
DynamicVariables=1
Nothing really different here.ImageCrop=0,0,[cPower],#OrigHW#
W= (#HW#*([cPower]/#OrigHW#))
H=#HW#
Since we flipped our image around we need to move our flipped, cropped and scaled indicator into the correct position.
Take the width of the cropped image (before it's scaled) divide it by the original width to get what ratio it is compared to the whole image. What we need is the ratio of what we're not using so we take 1 and subtract that ratio we just calculated and multiply that by the new width.X= (#HW#*(1-[cPower]/#OrigHW#))
Y=r
Here's where we use the measure we made to make the indicator blink.Hidden=[BatteryBlink]