It is currently March 4th, 2021, 2:17 am

Color Management with LUA

Skins that control functions in Windows or Rainmeter
User avatar
kami
Posts: 39
Joined: July 3rd, 2010, 10:47 am
Location: Canosa di Puglia, Italy

Color Management with LUA

Post by kami »

Hi, as i started working on colours in Rainmeter with LUA scripts i want to share an example file to show some applications.
Color Management in Rainmeter with LUA Scripts_1.0.rmskin
As everyone knows, color in Rainmeter is represented by a RGB triplet or by a Hex value:
the RGB triplet is made of 3 values (each from 0 to 255) separated by commas.
This value is seen by Rainmeter as a string and there is no way of picking up one of the components of the triplet as a numeric value.

What if one wants to calculate the average color between two given RGB triplets?
What if one wants to calculate the lightness parameter (HSL coordinates) from a given RGB triplet?

There are simple mathematical formulas on the web which allow to perform these calculations BUT these are done using single numeric values picked from RGB triplets and Rainmeter is not able to extract them.

Using some LUA scripts you can solve these issues.
The attached file has the purpose of showing how you can handle, from Rainmeter skin files, RGB triplet strings performing any kind of mathematical operation on its components and eventually recombine the results back into strings.
You do not have the required permissions to view the files attached to this post.
User avatar
balala
Rainmeter Sage
Posts: 12329
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Color Management with LUA

Post by balala »

kami wrote:There are simple mathematical formulas on the web which allow to perform these calculations BUT these are done using single numeric values picked from RGB triplets and Rainmeter is not able to extract them.
Don't believe I'm not appreciating your work, the skin and the lua scripts are well done, everything seems to be ok, but just to be correct, I have to disagree here. Rainmeter can do the same job, even without using lua. The following code will calculate (just) the average of the colors, as you did with the lua script, but without a such script:

Code: Select all

[Rainmeter]
AccurateText=1
Update=1000

[Metadata]
Name=Flat Mail
Author=Luca Mennoia
Information=gmail desktop animated notifier for Rainmeter
Version=2.0
License=Creative Commons Attribution License

[Variables]
@Include=#@#inc\skinVariables.inc
@Include2=#@#inc\userVariables.inc
@Include3=#@#inc\lng\#LANGUAGE#.inc
@Include4=#@#inc\shapes.inc
@Include5=#@#fnt\
COLX=217,132,134
COLY=0,55,255

[MeasureR1]
Measure=String
String=#COLX#
RegExpSubstitute=1
Substitute="^(.*),(.*),(.*)$":"\1"

[MeasureG1]
Measure=String
String=#COLX#
RegExpSubstitute=1
Substitute="^(.*),(.*),(.*)$":"\2"

[MeasureB1]
Measure=String
String=#COLX#
RegExpSubstitute=1
Substitute="^(.*),(.*),(.*)$":"\3"

[MeasureR2]
Measure=String
String=#COLY#
RegExpSubstitute=1
Substitute="^(.*),(.*),(.*)$":"\1"

[MeasureG2]
Measure=String
String=#COLY#
RegExpSubstitute=1
Substitute="^(.*),(.*),(.*)$":"\2"

[MeasureB2]
Measure=String
String=#COLY#
RegExpSubstitute=1
Substitute="^(.*),(.*),(.*)$":"\3"

[GetAvgRGB1]
Measure=Calc
Formula=(( [MeasureR1] + [MeasureR2] ) / 2 )
DynamicVariables=1

[GetAvgRGB2]
Measure=Calc
Formula=(( [MeasureG1] + [MeasureG2] ) / 2 )
DynamicVariables=1

[GetAvgRGB3]
Measure=Calc
Formula=(( [MeasureB1] + [MeasureB2] ) / 2 )
DynamicVariables=1

;***METERS***

[SolidBack]
Meter=Image
SolidColor=0,0,0,200
W=200
H=170

[ColSampleA]
Meter=Image
SolidColor=#COLX#
X=10
Y=10
W=20
H=20
LeftMouseUpAction=["#@#ext\RainRGB.exe" "VarName=COLX" "FileName=#CURRENTPATH##CURRENTFILE#"][!RefreshApp]

[ColSampleB]
Meter=Image
SolidColor=#COLY#
X=30r
Y=10
W=20
H=20
LeftMouseUpAction=["#@#ext\RainRGB.exe" "VarName=COLY" "FileName=#CURRENTPATH##CURRENTFILE#"][!RefreshApp]

[ColSampleAvg]
Meter=Image
SolidColor=[GetAvgRGB1],[GetAvgRGB2],[GetAvgRGB3]
X=30r
Y=10
W=20
H=20
DynamicVariables=1

[ExtremalValues]
Meter=String
Text=ColorX: #COLX##CRLF#ColorY: #COLY##CRLF#Average: %1,%2,%3
MeasureName=GetAvgRGB1
MeasureName2=GetAvgRGB2
MeasureName3=GetAvgRGB3
FontColor=255,255,255
FontSize=14
AntiAlias=1
X=10
Y=40
The key is the RegExpSubstitute and the corresponding Substitute option.
Once again: your code and scripts are entirely ok, they're working well, no objections in relation to them. I hope I didn't offended you and you don't mind. But I had to clarify this.
User avatar
kami
Posts: 39
Joined: July 3rd, 2010, 10:47 am
Location: Canosa di Puglia, Italy

Re: Color Management with LUA

Post by kami »

I'm anything but offended, indeed i appreciate your contribution as i have to learn from it (but also if i had not) because i think it's not about me: it's about good coding and perhaps your strategy is more efficient in many cases.
I'm not that good with RegExps, i didn't even imagined a solution like the one you spoke on and i believe your suggestions are more than proper in a place like this. Thanks.
I have to admit that the needed calculations for the average color between two colors are quite simple but i think many other tasks, especially on colors, are more complex and they need a lot of mathematical steps and decisions.
Don't get me wrong if i ask, as i really want to know, but can RegExps go this far?
User avatar
balala
Rainmeter Sage
Posts: 12329
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Color Management with LUA

Post by balala »

kami wrote:I'm anything but offended, indeed i appreciate your contribution as i have to learn from it (but also if i had not) because i think it's not about me: it's about good coding and perhaps your strategy is more efficient in many cases.
Thanks!
kami wrote:I'm not that good with RegExps, i didn't even imagined a solution like the one you spoke on and i believe your suggestions are more than proper in a place like this. Thanks.
Regular expressions are not a simple things, I'm also still learning and trying to use them. You can find good descriptions and examples on internet. What to say, they are interesting...
kami wrote:I have to admit that the needed calculations for the average color between two colors are quite simple but i think many other tasks, especially on colors, are more complex and they need a lot of mathematical steps and decisions.
Don't get me wrong if i ask, as i really want to know, but can RegExps go this far?
How far they can, is a good question, but for sure many calculations can be done. Eg to calculate the lightness as you defined it on the CalculateLightness.lua, you can use the following two measures:

Code: Select all

[MeasureLightnessX]
Measure=Calc
Formula=( Ceil (( Min ( [MeasureR1], Min ( [MeasureG1], [MeasureB1] )) + Max ( [MeasureR1], Max ( [MeasureG1], [MeasureB1] ))) / 2 ))
DynamicVariables=1

[MeasureLightnessY]
Measure=Calc
Formula=( Ceil (( Min ( [MeasureR2], Min ( [MeasureG2], [MeasureB2] )) + Max ( [MeasureR2], Max ( [MeasureG2], [MeasureB2] ))) / 2 ))
DynamicVariables=1
For sure there are calculations which can't be made by Rainmeter or they would be extremely complicated. In such cases you the lua scripts are indeed easier to use.
User avatar
kami
Posts: 39
Joined: July 3rd, 2010, 10:47 am
Location: Canosa di Puglia, Italy

Re: Color Management with LUA

Post by kami »

If you are coding complex rainmeter skins you usually need to minimize the amount of code. In these cases, if i use measures instead of external modular scripts, there would be a lot of "auxiliary" code to perform main calcs. I really liked the possibility to shorten and improve coding possibilities using LUA scripts as black-boxes. This aspect encourages the modular design which i think is a smarter way to code: to use things i made today in order to create things i'll need tomorrow is a concept i can easily realize with LUA and this perhaps was the reason why i started this topic :D
Anyway thanks a lot for your clarification, i've found it really interesting especially for what concerns RegExps.
User avatar
balala
Rainmeter Sage
Posts: 12329
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Color Management with LUA

Post by balala »

I think both methods have their own advantages and disadvantages. You have to decide which one you prefer.