It is currently March 28th, 2024, 12:36 pm

[Solved]Can you parse the RGBA values from a Color variable?

Get help with creating, editing & fixing problems with skins
Post Reply
oni5115
Posts: 27
Joined: October 4th, 2010, 3:33 pm

[Solved]Can you parse the RGBA values from a Color variable?

Post by oni5115 »

Can you parse the individual RGBA values from a Color variable?

What I am trying to do is make a custom gradient since for Roundline Meters you can't gradient the lines, only the background. I'm curious though if I can have Color1=rr,gg,bb,aa style variables or if I will have to do Color1R, Color1G, etc. as I have done in the example below. My assumption is that I will indeed have to do this for each color.

For reference, I made code that actually fully does the gradient including working with a transition delay.

Code: Select all

[Rainmeter]
Update=100

[Variables]
; Color 1
r1=0
g1=255
b1=0
a1=255

; Color 2
r2=255
g2=0
b2=0
a2=255

; Delay determines when the meter starts to chage colors
fDelay=0.75

; DelayJump determines if it slowly gradiates from Color1 to Color2,
; or if it jumps from Color1 to where it should already be gradiated from Colo1.
; Basically, 0 = smooth, 1 = drastic change 
DelayJump=0

; Produce Data that is between 0.0 and 1.0
[Data]
Measure=Calc
Formula=(Counter % 100)/100
MaxValue=100

; Determine the normalization value
[NormalizationVal]
Measure=Calc
Formula= (    ([Data] < #fDelay#) ? 0 : ((#DelayJump# = 1) ? [Data] : 1-(1-[Data])/(1-#fDelay#)  )  )
DynamicVariables=1

; Calculate the RGBa values respectively.
[rVal]
Measure=Calc
Formula=CEIL( (1-[NormalizationVal])*#r1# + ([NormalizationVal]*#r2# ))
DynamicVariables=1
[gVal]
Measure=Calc
Formula=CEIL( (1-[NormalizationVal])*#g1# + ([NormalizationVal]*#g2# ))
Formula=255
DynamicVariables=1
[bVal]
Measure=Calc
Formula=CEIL( (1-[NormalizationVal])*#b1# + ([NormalizationVal]*#b2# ))
DynamicVariables=1
[aVal]
Measure=Calc
Formula=CEIL( (1-[NormalizationVal])*#a1# + ([NormalizationVal]*#a2# ))
DynamicVariables=1

; Display Color Info for testing
[DisplayText]
Meter=STRING
MeasureName=Data
X=0
Y=0
FontColor=[rVal],[gVal],[bVal],[aVal]
FontFace=Trebuchet MS
FontSize=14
StringEffect=Shadow
FontEffectColor=0,0,0,255
AntiAlias=1
Text=D: %1
NumOfDecimals=2
DynamicVariables=1

[DisplayText2]
Meter=STRING
MeasureName=rVal
MeasureName2=gVal
MeasureName3=bVal
MeasureName4=aVal
X=0
Y=0R
FontColor=[rVal],[gVal],[bVal],[aVal]
FontFace=Trebuchet MS
FontSize=14
StringEffect=Shadow
FontEffectColor=0,0,0,255
AntiAlias=1
Text=R: %1#CRLF#G: %2#CRLF#B: %3#CRLF#A: %4
NumOfDecimals=0
DynamicVariables=1
Last edited by oni5115 on October 12th, 2010, 5:08 am, edited 1 time in total.
User avatar
Alex2539
Rainmeter Sage
Posts: 642
Joined: July 19th, 2009, 5:59 am
Location: Montreal, QC, Canada

Re: Can you parse the RGBA values from a Color variable?

Post by Alex2539 »

When using variables, imagine that wherever that variable is placed in the skin that whatever the variable represents is literally there. So, if you have "Color1=12,34,56,245" and then you use, for example, "LineColor=#Color1#" as far as Rainmeter is concerned, you're really just saying "LineColor=12,34,56,245" and treats it as though you had put it there, variable or not.

Since it looks like you're trying to perform calculations on each individual colour, using a variable like Color1 with each component in one string will not work since the Calc won't know what to do with the commas. The most intuitive way to perform a calculation on each component is the way you're doing it: separate the colours and then do it one by one. However, there is a way to keep it all in a single variable.

If you use a Hex value for a colour (eg: Color1=0C2238F5), you can use that value directly in a Calc measure by adding "0x" to the start of it, like so:

Code: Select all

[Variables]
Color1=0C2238F5

[Calc1]
Measure=Calc
Formula=0x#Color1#
Adding "0x" will tell the Calc to convert a hexadecimal number into decimal. In this example, Calc1 would return "203569397". After that you can use a bit of math to separate the individual colour values:

Code: Select all

[Variables]
Color1=0c2238f5

[Red1]
Measure=Calc
Formula=FLOOR(0x#Color1#/(16**6))

[Green1]
Measure=Calc
Formula=FLOOR(0x#Color1#/(16**4))%256

[Blue1]
Measure=Calc
Formula=FLOOR(0x#Color1#/(16**2))%256

[Alpha1]
Measure=Calc
Formula=0x#Color1#%256

[String]
Meter=String
MeasureName=Red1
MeasureName2=Green1
MeasureName3=Blue1
MeasureName4=Alpha1
SolidColor=FFFFFF
Text=%1, %2, %3, %4
The string in that example shows the colour in its decimal RGBA format, which is "12, 34, 56, 245". This method lets you keep everything together in a single variable, which may be easier for the end user to manipulate. The drawbacks are that you must perform the extra calculations in order to work with the individual colour components, and it is possible that the user doesn't know how to change a hexadecimal colour.
oni5115
Posts: 27
Joined: October 4th, 2010, 3:33 pm

Re: Can you parse the RGBA values from a Color variable?

Post by oni5115 »

Thank you. I'll probably do them separately since it would likely be a little easier for others to modify it later. Though it is good to know how to do the conversions should I ever need to.
Post Reply