It is currently April 16th, 2024, 7:02 am

create a color picker in Rainmeter

Tips and Tricks from the Rainmeter Community
User avatar
rbriddickk84
Rainmeter Sage
Posts: 276
Joined: February 17th, 2014, 12:39 pm
Location: Hungary

create a color picker in Rainmeter

Post by rbriddickk84 »

Basically the RainRGB gave me the idea that, what if i could make rgb color picker in Rainmeter without Lua scripting or any external addons.

The basic concept is i have an image, and a detecting area with the same size as the image itself. The sizes here is important :) becouse we will use the pixels as the base of our calculations. Where we will click with the mouse, the relative x;y mouse position will return with values.

Code: Select all

[detectingArea]
Meter=Image
SolidColor=0,0,0,1
X=0
Y=0
W=200
H=85
LeftMouseDownAction=[!SetVariable "xx" "$MouseX$"][!SetVariable "yy" "$MouseY$"][!WriteKeyValue Variables "xi" "$MouseX$" "set.inc"][!WriteKeyValue Variables "yi" "$MouseY$" "set.inc"]
DynamicVariables=1
MouseActionCursor=0
I used the WriteKeyValue becouse i wanted to record the values for a refresh action.
exam1.jpg
As the image above is showing, i had to "split in half" with a calculation the area horizontally, so one measure can make the full brightness function. For this my idea was the following:

color = (#red#-#brightness#), (#green#-#brightness#), (#blue#-#brightness#)

So let's have an example: red=255, green=16, blue=113, brightness=-200 (upwards from the middle of the image's zero line)
The red will remain 255, because the script will allways reduce the max number to 255 for the final r, g, and b.
The green is 16, but we have a "-200" on brightness, so: (16-(-200)) will be 216.
The blue is 113 and -200 on brightness, so (113-(-200)) will be 313, but capped to 255.
The final result will be 255,216,255. It will be almost white.

Here is the code for the brightness measure:

Code: Select all

[CalcBrightness]
Measure=Calc
Formula=((#yy#*6)-255)
ifCondition=(CalcBrightness < -240)
ifTrueAction=[!SetVariable "brg" -255][!WriteKeyValue Variables "tbrg" "-255" "set.inc"]
ifCondition2=((CalcBrightness > -240) && (CalcBrightness < 240) && ((#brg#) <> CalcBrightness))
ifTrueAction2=[!SetVariable "brg" [CalcBrightness]][!WriteKeyValue Variables "tbrg" "[CalcBrightness]" "set.inc"]
ifCondition3=(CalcBrightness > 240)
ifTrueAction3=[!SetVariable "brg" 255][!WriteKeyValue Variables "tbrg" "255" "set.inc"]
DynamicVariables=1
Let's see the color calculations:

I used here "Rdec" name, because it is "red decrease". decrease because the red is separated in two to fulfill the full range of rgb color shades.

Code: Select all

[CalcRdec]
Measure=Calc
Formula=(8*(62-#xx#))
ifCondition=((#xx# > 70) && (#xx# < 139))
ifTrueAction=[!SetVariable "xr" 0]
ifCondition2=(#xx# < 35)
ifTrueAction2=[!SetVariable "xr" 255]
ifCondition3=((#xx# > 35) && (#xx# < 70) && (#xr# <> [CalcRdec]))
ifTrueAction3=[!SetVariable "xr" [CalcRdec]]
DynamicVariables=1
In the above code i reversed the range of pixels. So it started with 255, and it will remain 255 untill 35 pixel on X. Above 35 pixel it will count down to 1 till it reaches the 70th pixel.
Between 70 and 139 pixel will zeroing the value. But i needed a separate code for the increasing red at the end of the image:

Code: Select all

[CalcRinc]
Measure=Calc
Formula=8*((#xx#)-136)
ifCondition=((#xx# > 139) && (#xx# < 165) && (#xr# <> CalcRinc))
ifTrueAction=[!SetVariable "xr" [CalcRinc]]
ifCondition2=(#xx# > 165)
ifTrueAction2=[!SetVariable "xr" 255]
DynamicVariables=1


After calculated these normal red values, i needed to add the brightness control.

Code: Select all

[CalcRed]
Measure=Calc
Formula=(#xr# - #brg#)
ifCondition=((CalcRed > 0)&&(CalcRed < 255)&&(CalcRed <> #rr#))
ifTrueAction=[!SetVariable "rr" [CalcRed]]
ifCondition2=(CalcRed < 1)
ifTrueAction2=[!SetVariable "rr" 0]
ifCondition3=(CalcRed > 254)
ifTrueAction3=[!SetVariable "rr" 255]
DynamicVariables=1
So as i mentioned above, i just subtracted the brightness value from the temporary red value, and limit the minimum and maximum to 0, and 255.

I only mention the Green calculation, because the Blue is almost the same, just 70 pixel difference in X position.

Code: Select all

[CalcGi]
Measure=Calc
Formula=Abs((Abs(11*(#xx#-66)))-637)
ifCondition=((#xx# > 33)&&(#xx# < 101))
ifTrueAction=[!SetVariable "xg" 255]
ifCondition2=(((#xx# > 8)&&(#xx# < 33))&&(#xg# <> [CalcGi]))
ifTrueAction2=[!SetVariable "xg" [CalcGi]]
ifCondition3=(((#xx# > 101)&&(#xx# < 124))&&(#xg# <> [CalcGi]))
ifTrueAction3=[!SetVariable "xg" [CalcGi]]
ifCondition4=((#xx# < 8)||(#xx# > 124))
ifTrueAction4=[!SetVariable "xg" 0]
DynamicVariables=1
First i needed a temporary xg variable, to calc the normal green color.
My basic idea was a strange range. :)
For example: there is a 100% wide image. Now i have to control the value like this:
0-30% = 0<->254
30-70% = 255
70-100% = 254<->0
I don't know if you get my technique. I hope it's clear. :)
So i had to be a little tricky, becouse i had to calculate the whole range's maximum value.
I have 2 x 255 range, that is 510. And there is the last 40% range, so a simple calculation and you get the final in pixels.
I had to convert these values in pixels, and played a little for the better visual matching.

And here is the final green variable calc measure code:

Code: Select all

[CalcGrn]
Measure=Calc
Formula=(#xg# - #brg#)
ifCondition=((CalcGrn > 0)&&(CalcGrn < 255)&&(CalcGrn <> #gg#))
ifTrueAction=[!SetVariable "gg" [CalcGrn]]
ifCondition2=(CalcGrn < 1)
ifTrueAction2=[!SetVariable "gg" 0]
ifCondition3=(CalcGrn > 254)
ifTrueAction3=[!SetVariable "gg" 255]
DynamicVariables=1
It's almost the same as the Red's code.

For safety reasons, at the end of the color and brightness calculations i save the final variables into a set.inc file:

Code: Select all

[SkinRefresMemory]
Measure=Calc
Formula=(#rr#<>#rx#)||(#gg#<>#gx#)||(#bb#<>#bx#)?1:0
ifEqualValue=1
ifEqualAction=[!WriteKeyValue Variables "rx" "#rr#" "set.inc"][!WriteKeyValue Variables "gx" "#gg#" "set.inc"][!WriteKeyValue Variables "bx" "#bb#" "set.inc"]
DynamicVariables=1
Disabled=0
For some reason, i don't know why, the writing part isn't working as intended after refresh, so that will need more developing. :)

I hope this script will be helpfull! ;)
You do not have the required permissions to view the files attached to this post.