It is currently May 12th, 2024, 6:04 pm

Bad Code?

General topics related to Rainmeter.
JayVodka
Posts: 9
Joined: January 14th, 2012, 5:37 am

Bad Code?

Post by JayVodka »

I'm trying to create a meter that will change color as the value of a measurement changes and now I've ended up with this lua script to direct the color change. The variables c1, c2, and c3 represent the three values for the RGB color. It appears that the color does change when using this code, however rainmeter crashes almost immediately.

Is my code not robust enough or has some kinda error or is doing something like this not possible?

Code: Select all

function Initialize()
	Measure = SKIN:GetMeasure("MeasureCPU")
end

function Update()
	y = 76.5
	c1 = 0
	c2 = 255
	c3 = 255
	x = Measure:GetValue() * "0.765"
	while y ~= x do
		repeat
			if x >= 51 then	
				if x < y then
					c3 = c3 - 1
					y = y - 0.1
					SKIN:Bang("!SetVariable Color3 "..c3)
				else
					c3 = c3 + 1
					y = y + 0.1
					SKIN:Bang("!SetVariable Color3 "..c3)
				end
			elseif x >= 25.5 then
				if x < y then
					c1 = c1 + 1
					y = y - 0.1
					SKIN:Bang("!SetVariable Color1 "..c1)
				else
					c1 = c1 - 1
					y = y + 0.1
					SKIN:Bang("!SetVariable Color1 "..c1)
				end
			else
				if x < y then
					c2 = c2 - 1
					y = y - 0.1
					SKIN:Bang("!SetVariable Color2 "..c2)
				else
					c2 = c2 + 1
					y = y + 0.1
					SKIN:Bang("!SetVariable Color2 "..c2)
				end
			end
		until y == x
	end
end
User avatar
jsmorley
Developer
Posts: 22632
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Bad Code?

Post by jsmorley »

I suspect that you are not properly getting out of that while - do / repeat - until loop. You have to be very careful that you don't get in a position where Rainmeter executes the Lua code, and then sits there waiting for the Update() cycle of the Lua script to complete. Rainmeter runs Lua in a single thread, and if you write code that causes it not to return to Rainmeter within a few updates of the skin / script measure, then Rainmeter will become "unresponsive". It doesn't crash as such, but when an app becomes "unresponsive" to Windows messages, Windows will either just let it sit there stalled, or can send in the white blood cells to kill the offender. The offender in this case is seen as Rainmeter.

I'm not sure exactly what is going on with your code, but I get very nervous with any conditional loops in a Lua script in Rainmeter. You have to ensure that the while - until condition is going to be met EVERY time, and in quick order... I notice that in both parts of your loop(s) the while - do and the repeat - until, you are depending on x being exactly equal to y to get out. You sure with the math you have and the fact that you are incrementing by 0.1 at a time, that you are sure to hit EXACTLY x == y in both loops?
JayVodka
Posts: 9
Joined: January 14th, 2012, 5:37 am

Re: Bad Code?

Post by JayVodka »

That may be the problem but there's also the other issue I have to address... I'm assuming that RGB colors contain only integer values correct? If that is so then I need to establish a certain proportion between the max value of the meter and the max value of the RGB colors which doesn't seem to get me very nice numbers... I think that rounding might be a possible solution but I feel that something may become desynchronized in doing so.
User avatar
jsmorley
Developer
Posts: 22632
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Bad Code?

Post by jsmorley »

JayVodka wrote:That may be the problem but there's also the other issue I have to address... I'm assuming that RGB colors contain only integer values correct? If that is so then I need to establish a certain proportion between the max value of the meter and the max value of the RGB colors which doesn't seem to get me very nice numbers... I think that rounding might be a possible solution but I feel that something may become desynchronized in doing so.
Well it is not clear to me what you are trying to accomplish. If you are trying to have the Lua script "transition" the color from one to the other in some loop in Lua, that will just never work. Rainmeter goes out to the Lua, the Lua is executed, and all the actions that the Lua does to the skin all happen at once when the script returns to Rainmeter. So sending a blizzard of SKIN:Bang() commands all setting the same value to the skin in a loop will not cause them to be done in some discrete way, just the "last"one will happen really.

It is perfectly fine to set the three components of the color code, so you can do separate math on them and put them together when you set the FontColor or BarColor or whatever it is in the skin.

You can also directly set colors if you want, and not use three variables in the skin, and it might be a bit easier too.

sRed=0
sGreen=150
sBlue=255
SKIN:Bang('!SetOption MeterName FontColor '..sRed..','..sGreen..','..sBlue)

Yes, RRR,GGG,BBB,AAA colors in Rainmeter consist of integers from 0 - 255. However I'm fairly sure Rainmeter will just use the "floor" of each number if they have decimal places. 255.2,255.8,255.1,255 is still white... You can however round numbers in Lua, and it is probably cleaner. You don't have to worry too much about "rounding errors" creeping in over time, as you should and do get a fresh copy of the value of the measure each Update() and so you are presumably only going to round each number once.

Code: Select all

function round(num, places)
  local mult = 10^(places or 0)
  return math.floor(num * mult + 0.5) / mult
end
As i said, I'm not sure where you are going. Seems like an awful lot going on here to just get a value of a measure and set the RRR,GGG,BBB values accordingly. Maybe if you describe exactly what the goal is...
JayVodka
Posts: 9
Joined: January 14th, 2012, 5:37 am

Re: Bad Code?

Post by JayVodka »

The exact thing I'm trying to accomplish is that with a cpu meter (or any meter that is measured in percentage) transition from cyan (0,255,255,255) to red (255,0,0,255). The meter will basically transition through the color spectrum toward red as the meter approaches 0. I find that the transition happens in three chunks: blue -> green, green -> yellow, yellow-> red. As the blue turns into green the B component goes from 255 to 0, as the green turns into yellow the R component increases from 0 to 255, and then as yellow turns into red the G component decreases from 255 to 0.

What my code attempted to accomplish was divide the 765 color transitions (255*3) into the 100.0 percent value represented by the meter. Making 765 blocks of code for each transition seemed a bit unreasonable so by proportioning 100 into 76.5 I was hoping to manipulate the R, G, or B by factor of 1 every time the returned value of the meter changed by 0.1 by using a loop.

As I think through this, would it be possible to find the difference between the values on each update and use that found delta to change the color accordingly while keeping in mind the 3 partitions? Maybe an array could accomplish this or have I made a simple task more complicated?
User avatar
smurfier
Moderator
Posts: 1931
Joined: January 29th, 2010, 1:43 am
Location: Willmar, MN

Re: Bad Code?

Post by smurfier »

Could you please try this script.
GitHub | DeviantArt | Tumblr
This is the song that never ends. It just goes on and on my friends. Some people started singing it not knowing what it was, and they'll continue singing it forever just because . . .
JayVodka
Posts: 9
Joined: January 14th, 2012, 5:37 am

Re: Bad Code?

Post by JayVodka »

This script seems very close to what I am trying to achieve however it doesn't cover the range of colors that I was looking for. Seems like there are points where the saturation level drops. I tried modifying the RGB components individually because as the color changes only one component was being manipulated at a time.

I noticed the pattern of the changing variables by going into the edit color palette in paint. By dragging along the top of the spectrum of the palette from cyan to red only one component was changing at a time. In terms of Hue, Saturation, and Luminosity, Saturation and Luminosity remained at a constant 240 and 120 value respectively while the hue increased from 0 to 120.

Thanks for the hint!
User avatar
smurfier
Moderator
Posts: 1931
Joined: January 29th, 2010, 1:43 am
Location: Willmar, MN

Re: Bad Code?

Post by smurfier »

As stuff like hue and saturation are not part of the color code I doubt one can take such things into consideration.

Also, the method I used (the version currently posted) is pretty much the only method I found when I researched this some time last year. It incrementally averages the colors.
GitHub | DeviantArt | Tumblr
This is the song that never ends. It just goes on and on my friends. Some people started singing it not knowing what it was, and they'll continue singing it forever just because . . .
JayVodka
Posts: 9
Joined: January 14th, 2012, 5:37 am

Re: Bad Code?

Post by JayVodka »

Well I know that the hue and saturation are not variables that can be considered, but I'm using them as a visual reference point if one chooses to see how the RGB components change. Hue and Saturation are values dependent on RGB, so technically these values are indirectly taken into consideration.

Since the script takes the average then it makes sense that more that one component is being manipulated at once therefore the colors are darker/dimmer (hence saturation levels have dropped).

So as for your research, I guess there is no easy way for me to do perform the task that I'm looking for if it is even possible.

I'll post something if I find it.