It is currently April 20th, 2024, 7:45 am

[SOLVED] Using !WriteKeyValue in LUA - Filepath Backslash problems

Discuss the use of Lua in Script measures.
User avatar
raiguard
Posts: 660
Joined: June 25th, 2015, 7:02 pm
Location: The Sky, USA

[SOLVED] Using !WriteKeyValue in LUA - Filepath Backslash problems

Post by raiguard »

Image
UPDATE #3: After much toil, I have solved the problem!

----------

UPDATE #2: I have figured out the problem, but I still need help fixing it. See the replies to learn more.

----------

In my skin pack, illustro Gadgets, I am creating coloring options in the pack's settings skin. For each color, there are a few things: A box that shows the color, and when clicked will open RainRGB to allow the user to edit the color. There is a string meter that shows the color value itself (e.g. 250,250,250,200), and when clicked it pops up with a text field, allowing the user to directly edit the color value.

Last, but not least, there is a bar. This bar, when clicked, will change the color's alpha value to a specific percentage of the max, depending on where the bar is clicked (e.g. if you click the very rightmost point on the bar, it sets the alpha to 255, which is the max). This bar uses an LUA script I adopted from CircuitousTwo, which is also where I got the idea for the layout and looks of the coloring meters.

Or at least, this is what is SUPPOSED to happen. Whenever I click the bar, I get two very strange errors that I have puzzled over for several hours:

Code: Select all

Script: Settings.lua:\Files\Settings\Caleb\Rainmeter..."]:1: '<name>' expected near '\'
Script: Settings.luaattempt to call a table value
(Yes, the typos are intentional. That is exactly how they show up in Rainmeter's log)

In an attempt to make sure there were no "ghost" characters (there is not a single backslash in the entire LUA script) I completely rewrote it from scratch, and cross-checked it to make absolutely 100% sure it would all work. Also, once you have tried clicking the alpha bar three times, Rainmeter will completely crash.

----------

UPDATE #1: I have discovered that the first time you try to click the alpha bar, it reports the errors I mentioned above. The SECOND time you do it, the error is slightly different:

Code: Select all

Script: Settings.lua:\Files\Settings\Caleb\Rainmeter..."]:1: '<name>' expected near '\'
Script: Settings.luaattempt to call a nil value
The second error changed from 'table value' to 'nil value'. What the heck is going on!?

----------

Here is the link to the skin pack's GitHub repository, in which you can get the development version I am having this problem in (be sure to download the 'v0.8.0' branch, not the master branch.) The skin that uses the script is Settings.ini, under the appearance section. The script is in @Resources\Scripts\Settings.lua. For your convenience, I have copied the code of the script below, as well as the code for one of the color pickers.

Any and all help would be greatly appreciated.

Settings.lua code:

Code: Select all

isDbg = true

function Initialize()

	hexChars = { 	[0]='0', [1]='1', [2]='2', [3]='3', 
				[4]='4', [5]='5', [6]='6', [7]='7', 
				[8]='8', [9]='9', [10]='a', [11]='b', 
				[12]='c', [13]='d', [14]='e', [15]='f' }
	
	page = SKIN:GetVariable('pageName')
	
	if page == 'appearance' then
	
		colors = {	SKIN:GetVariable('colorPrimary'), 
					SKIN:GetVariable('colorSecondary'), 
					SKIN:GetVariable('colorAccent'), 
					SKIN:GetVariable('colorDim')  }
					
		bars = {	SKIN:GetMeter('ColorsPrimaryAlphaBar'), 
					SKIN:GetMeter('ColorsSecondaryAlphaBar'), 
					SKIN:GetMeter('ColorsAccentAlphaBar'), 
					SKIN:GetMeter('ColorsDimAlphaBar')  }
		
		maxBarW = SKIN:GetMeter('ColorsPrimaryAlphaBarBg'):GetW()
		
		-- set the width of the alpha bars
		for i=1,#colors do
			tempW = math.floor(getStringAlphaPercent(colors[i]) * maxBarW)
			SKIN:Bang('!SetOption', bars[i]:GetName(), 'W', tempW)
			if isDbg == true then
				print("IG: set width of '" .. tostring(bars[i]:GetName()) .. "'")
			end
		end
	
	else
		print("IG: wtf? invalid subpage id in settings skin!")
	end
end

function Update()


end

-- Transparency manipulation functions

-- called from skin - changes alpha value on a color in the specified file
function changeAlpha(color, percent, filepath)
	baseColor = SKIN:GetVariable(color)
	alpha = math.floor(percent*0.01*255)
	if (string.find(baseColor, ",") ~= nil) then
		rgb = string.match(baseColor, "%d+,%d+,%d+")
		newColor = rgb .. ',' .. alpha
	else
		rgb = string.sub(baseColor,1,6)
		alpha = decToHex(alpha)
		newColor = rgb .. alpha
	end
	SKIN:Bang('!WriteKeyValue','Variables',color, newColor, filepath)
end

-- intended to retrieve the alpha component of an RGBA or hex color and return as a percent 0.0 to 1.0
function getStringAlphaPercent(color)
	local alpha
	if (string.find(color, ",") ~= nil) then
		
		rgbIt = string.gmatch(color,"%d+")
		rgbTable = {}
		for match in rgbIt do
			table.insert(rgbTable, match)
		end
		
		if (#rgbTable < 4) then
			alpha = 1
		else
			alpha = (rgbTable[4] / 255)
			alpha = string.format("%.2f",alpha)
		end
	else
		if (string.len(color)) > 6 then
			alpha = hexToDec(string.sub(color,7,8))
			alpha = (alpha / 255)
			alpha = string.format("%.2f",alpha)
		else
			alpha = 1
		end
	end
	return tonumber(alpha)
end

-- converts a hexadecimal string to a decimal number
function hexToDec(hexNum)
	hexNum = string.lower(hexNum)
	sum = 0
	for i=1,#hexNum,1 do
		sum = sum + (findHexChar(string.sub(hexNum,i,i)) * 16^(#hexNum-i))
	end
	return sum
end

-- converts decimal number to hexadecimal string
function decToHex(decNum)
	local result = {}
	while (decNum > 0) do
		table.insert(result, 1, hexChars[math.fmod(decNum, 16)])
		decNum = math.floor(decNum / 16)
	end
	return table.concat(result,'',1,#result)
end

-- linearly searches hexChar array for a given character and returns its index
function findHexChar(char)
	for i=0,#hexChars do
		if hexChars[i] == char then
			return i
		end
	end
	return -1
end
'colorPrimary' color picking module and respective styles (contained in illustro Gadgets\Settings\Subpages\appearance.inc):

Code: Select all

[StyleColorsLabel]
FontSize=#fontSize2#
X=(#navMenuSeparatorX# + 10)
Y=23r

[StyleColorsBoxBg]
SolidColor=0,0,0,255
X=3r
Y=15r
W=24
H=24

[StyleColorsBox]
X=2r
Y=2r
W=20
H=20
ToolTipText="Edit this color using RainRGB"

[StyleColorsAlphaBarBg]
SolidColor=#colorDim#
X=26r
Y=0r
W=#colorAlphaBarBgWidth#
H=4
ToolTipText="Edit this color's alpha value (opacity)"

[StyleColorsAlphaBar]
SolidColor=#colorPrimary#
X=0r
Y=0r
H=4

[StyleColorsString]
FontSize=#fontSize2#
FontColor=#colorSecondary#
X=-2r
Y=8r
ToolTipText="Edit this color by typing an RGBA value"

; Primary Color
[ColorsPrimaryLabel]
Meter=String
MeterStyle=StyleText | StyleColorsLabel
Text="Primary Color"

[ColorsPrimaryBoxBg]
Meter=Image
MeterStyle=StyleColorsBoxBg

[ColorsPrimaryBox]
Meter=Image
MeterStyle=StyleColorsBox
LeftMouseUpAction=["#rainRgb#" "VarName=colorPrimary" "FileName=#commonSettingsPath#"]
MiddleMouseUpAction=[!WriteKeyValue Variables colorPrimary "#dColorPrimary#" "#commonSettingsPath#"][!RefreshGroup illustroGadgets]
SolidColor=#colorPrimary#

[ColorsPrimaryAlphaBarBg]
Meter=Image
MeterStyle=StyleColorsAlphaBarBg
LeftMouseUpAction=[!CommandMeasure MeasureSettingsScript "changeAlpha('colorPrimary',$MouseX:%$,#commonSettingsPath#)"][!RefreshGroup illustroGadgets]

[ColorsPrimaryAlphaBar]
Meter=Image
MeterStyle=StyleColorsAlphaBar
SolidColor=#colorPrimary#
W=1

[ColorsPrimaryString]
Meter=String
MeterStyle=StyleText | StyleColorsString
Text=#colorPrimary#
LeftMouseUpAction=[!CommandMeasure MeasureInputColorConfig "Executebatch 1"]
MiddleMouseUpAction=[!WriteKeyValue Variables colorPrimary "#dColorPrimary#" "#commonSettingsPath#"][!RefreshGroup illustroGadgets]
Last edited by raiguard on December 31st, 2015, 8:12 pm, edited 5 times in total.
”We are pretty sure that r2922 resolves the regression in resolution caused by a reversion to a revision.” - jsmorley, 2017
User avatar
balala
Rainmeter Sage
Posts: 16148
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Editing color opacity - unknown errors

Post by balala »

I downloaded your pack, but I'm not sure which skin are we talking about. I think the lua file is the illustro Gadgets\@Resources\Scripts\Settings.lua, but I'm not sure in which skin is it used, I couldn't find it. Please be more precise.
Anyway, at a first look to your code, I found a possible issue: if the colorPrimary, colorSecondary, colorAccent, colorDim, ColorsPrimaryAlphaBar, ColorsSecondaryAlphaBar, ColorsAccentAlphaBar and ColorsDimAlphaBar variables are numbers, you should try to convert the values to numbers (using the tonumber function). Eg:
colors = { tonumber(SKIN:GetVariable('colorPrimary')), tonumber(SKIN:GetVariable('colorSecondary')), tonumber(SKIN:GetVariable('colorAccent')), tonumber(SKIN:GetVariable('colorDim')) }. Check the whole code, maybe there are some other places where the taken values should be converted to numbers.
On the other hand, the print function is not the best choice if you're using the script in a Rainmeter skin. As far as I know, it won't work. Instead you should send the needed value to a variable or as an option. Eg instead of print("IG: set width of '" .. tostring(bars[i]:GetName()) .. "'") command, you should use somthing like this: SKIN:Bang('!SetOption', 'NameOfTheStringMeter', 'Text', "IG: set width of '" .. tostring(bars[i]:GetName()) .. "'").
So, if this don't help, please tell us which skin has the problem.
User avatar
raiguard
Posts: 660
Joined: June 25th, 2015, 7:02 pm
Location: The Sky, USA

Re: Editing color opacity - unknown errors

Post by raiguard »

balala wrote:I downloaded your pack, but I'm not sure which skin are we talking about. I think the lua file is the illustro Gadgets\@Resources\Scripts\Settings.lua, but I'm not sure in which skin is it used, I couldn't find it. Please be more precise.
Anyway, at a first look to your code, I found a possible issue: if the colorPrimary, colorSecondary, colorAccent, colorDim, ColorsPrimaryAlphaBar, ColorsSecondaryAlphaBar, ColorsAccentAlphaBar and ColorsDimAlphaBar variables are numbers, you should try to convert the values to numbers (using the tonumber function). Eg:
colors = { tonumber(SKIN:GetVariable('colorPrimary')), tonumber(SKIN:GetVariable('colorSecondary')), tonumber(SKIN:GetVariable('colorAccent')), tonumber(SKIN:GetVariable('colorDim')) }. Check the whole code, maybe there are some other places where the taken values should be converted to numbers.
On the other hand, the print function is not the best choice if you're using the script in a Rainmeter skin. As far as I know, it won't work. Instead you should send the needed value to a variable or as an option. Eg instead of print("IG: set width of '" .. tostring(bars[i]:GetName()) .. "'") command, you should use somthing like this: SKIN:Bang('!SetOption', 'NameOfTheStringMeter', 'Text', "IG: set width of '" .. tostring(bars[i]:GetName()) .. "'").
So, if this don't help, please tell us which skin has the problem.
EDIT #2: The 'colors' variables are numbers, but the 'bars' variables are references to meters in the settings skin, so they are not numbers.
EDIT: I updated the original post to be a lot more concise. I was really tired when I originally wrote it, or something.

You're right, I wasn't very specific. The settings skin's appearance page, as well as all of the subpages in the skin settings page are where this script is used. For each color option there is a box that shows the color, a bar that shows what percent the alpha value is set to (which is what uses the LUA), and a string meter that shows the color value. When you click the bar to change the alpha, that is when the problem occurs. You access the settings skin by hovering over any of the gadgets and clicking the settings icon that appears.

And as for your print comments, that's to output to Rainmeter's log file. I will change it to the !Log bang to do it instead.

Here is a picture of the skin:

Image
”We are pretty sure that r2922 resolves the regression in resolution caused by a reversion to a revision.” - jsmorley, 2017
User avatar
jsmorley
Developer
Posts: 22629
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Editing color opacity - unknown errors

Post by jsmorley »

print() is fine for outputting to the Rainmeter log. It's an excellent way to debug things.
User avatar
raiguard
Posts: 660
Joined: June 25th, 2015, 7:02 pm
Location: The Sky, USA

Re: Editing color opacity - unknown errors

Post by raiguard »

AHA! I figured out the problem: It was the "filepath" string - that string contains a file path for the lua to edit, which has backslashes. In Rainmeter, single backslashes in strings work fine, but in LUA, you have to use double backslashes.

So I can simply change the variables, except for one problem: #@# always uses single backslashes.

What would be the best way to go about fixing this?
”We are pretty sure that r2922 resolves the regression in resolution caused by a reversion to a revision.” - jsmorley, 2017