It is currently September 21st, 2019, 10:21 pm

Converting RGB to HEX with lua

Help with creating, editing & fixing problems with skins
User avatar
StArL0rd84
Posts: 336
Joined: February 8th, 2015, 10:07 pm
Location: EU, Denmark.

Converting RGB to HEX with lua

StArL0rd84 » March 16th, 2019, 2:20 pm

Lua still baffles me...
Found this example lua script,
and my best guess was that (color) is were it gets the RGB code to convert.
But the script measure still reads out 0.
What am i missing here?

Code: Select all

function ConvertToHex(color) -- Converts RGB colors to HEX
	local hex = {}

	for rgb in color:gmatch('%d+') do
		table.insert(hex, ('%02X'):format(tonumber(rgb)))
	end

	return table.concat(hex)
end

Code: Select all

[MeasureRgbToHex]
 Measure=Script
 ScriptFile=#@#Lua\RgbToHex.lua
 Color=255,33,154
 UpdateDivider=-1
(#WorkTime# = 1 ? #Work# : ([mEnergyLoss:%] > 100 ? #SleepMode# : #Rainmeter#))
User avatar
jsmorley
Developer
Posts: 19474
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Converting RGB to HEX with lua

jsmorley » March 16th, 2019, 2:29 pm

StArL0rd84 wrote:
March 16th, 2019, 2:20 pm
Lua still baffles me...
Found this example lua script,
and my best guess was that (color) is were it gets the RGB code to convert.
But the script measure still reads out 0.
What am i missing here?

Code: Select all

function ConvertToHex(color) -- Converts RGB colors to HEX
	local hex = {}

	for rgb in color:gmatch('%d+') do
		table.insert(hex, ('%02X'):format(tonumber(rgb)))
	end

	return table.concat(hex)
end

Code: Select all

[MeasureRgbToHex]
 Measure=Script
 ScriptFile=#@#Lua\RgbToHex.lua
 Color=255,33,154
 UpdateDivider=-1
That's not going to work, as you have two different approaches going on.

You have a Script measure that is attempting to execute the Update() function of a Lua script, while containing an option that holds the "color". In a Lua script with an Update() function, you could use SELF:() to read that color option from the Script measure, then take the action, which in and of itself is fine, and return the hex value to the Script measure to use elsewhere in your skin.

But you have a ConvertToHex() function in the Lua file, which is expecting to be directly called with a parameter of the "color" you want to return. That would be useful in Inline Lua, but as you have it now, that function is never called by anything.

I can get deeper into it if you want, but it would help to know how and where you want to use the hex value you are converting from rgb.
User avatar
jsmorley
Developer
Posts: 19474
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Converting RGB to HEX with lua

jsmorley » March 16th, 2019, 2:36 pm

Skin:

Code: Select all

[Rainmeter]
Update=1000
DynamicWindowSize=1
AccurateText=1

[Variables]
MyColor=255,255,255

[Lua]
Measure=Script
ScriptFile=#CURRENTPATH#Test.lua
Disabled=1

[MeterColor]
Meter=String
FontSize=15
FontWeight=400
FontColor=#MyColor#,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
DynamicVariables=1
Text=RGB is #MyColor# | HEX is [&Lua:ConvertToHex('#MyColor#')]

Test.lua:

Code: Select all

function ConvertToHex(color) -- Converts RGB colors to HEX
	local hex = {}

	for rgb in color:gmatch('%d+') do
		table.insert(hex, ('%02X'):format(tonumber(rgb)))
	end

	return table.concat(hex)
end

1.jpg


Change the MyColor variable to 181,255,191 and you get:
You do not have the required permissions to view the files attached to this post.
User avatar
jsmorley
Developer
Posts: 19474
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Converting RGB to HEX with lua

jsmorley » March 16th, 2019, 2:43 pm

https://docs.rainmeter.net/manual/lua-scripting/inline-lua/

There is just no advantage, and some disadvantage, to using this as a call to Update() in the Lua script with a SELF() option of the color. The disadvantage is that then you can only use it in one instance, since the color is hard-coded and can only deal with that one color. With Inline Lua, the same .lua file can be used to convert any color(s), on demand, as many places in the skin as you want.
User avatar
StArL0rd84
Posts: 336
Joined: February 8th, 2015, 10:07 pm
Location: EU, Denmark.

Re: Converting RGB to HEX with lua

StArL0rd84 » March 16th, 2019, 2:43 pm

jsmorley wrote:
March 16th, 2019, 2:29 pm
That's not going to work, as you have two different approaches going on.

You have a Script measure that is attempting to execute the Update() function of a Lua script, while containing an option that holds the "color". In a Lua script with an Update() function, you could use SELF:() to read that color option from the Script measure, then take the action, which in and of itself is fine, and return the hex value to the Script measure to use elsewhere in your skin.

But you have a ConvertToHex() function in the Lua file, which is expecting to be directly called with a parameter of the "color" you want to return. That would be useful in Inline Lua, but as you have it now, that function is never called by anything.

I can get deeper into it if you want, but it would help to know how and where you want to use the hex value you are converting from rgb.
Found the script here: https://docs.rainmeter.net/snippets/colors/
But it does not go into how to use it...

I want to use the hex code in a image meters imagetint function.
The image meter is a icon for a menu which can have two colors depending on the state.

Just trying to condense:
ImageTint=(#MenuMode#=1?#ColorR#:#DefaultR#),(#MenuMode#=1?#ColorG#:#DefaultG#),(#MenuMode#=1?#ColorBlue#:#DefaultB#)

Into something like this:
ImageTint=(#MenuMode#=1?[ColorHex]:#DefaultRGB#)
(#WorkTime# = 1 ? #Work# : ([mEnergyLoss:%] > 100 ? #SleepMode# : #Rainmeter#))
User avatar
StArL0rd84
Posts: 336
Joined: February 8th, 2015, 10:07 pm
Location: EU, Denmark.

Re: Converting RGB to HEX with lua

StArL0rd84 » March 16th, 2019, 2:50 pm

jsmorley wrote:
March 16th, 2019, 2:36 pm
Skin:

Code: Select all

[Rainmeter]
Update=1000
DynamicWindowSize=1
AccurateText=1

[Variables]
MyColor=255,255,255

[Lua]
Measure=Script
ScriptFile=#CURRENTPATH#Test.lua
Disabled=1

[MeterColor]
Meter=String
FontSize=15
FontWeight=400
FontColor=#MyColor#,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
DynamicVariables=1
Text=RGB is #MyColor# | HEX is [&Lua:ConvertToHex('#MyColor#')]

Test.lua:

Code: Select all

function ConvertToHex(color) -- Converts RGB colors to HEX
	local hex = {}

	for rgb in color:gmatch('%d+') do
		table.insert(hex, ('%02X'):format(tonumber(rgb)))
	end

	return table.concat(hex)
end


1.jpg



Change the MyColor variable to 181,255,191 and you get:

I think i can use that code, thank you
(#WorkTime# = 1 ? #Work# : ([mEnergyLoss:%] > 100 ? #SleepMode# : #Rainmeter#))
User avatar
jsmorley
Developer
Posts: 19474
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Converting RGB to HEX with lua

jsmorley » March 16th, 2019, 2:54 pm

Skin:

Code: Select all

[Rainmeter]
Update=1000
DynamicWindowSize=1
AccurateText=1

[Variables]
MyRed=181
MyGreen=255
MyBlue=191

[Lua]
Measure=Script
ScriptFile=#CURRENTPATH#Test.lua
Disabled=1

[MeterImage]
Meter=Image
ImageName=#@#Images\Chrome.png
GreyScale=1
ImageTint=[&Lua:ConvertToHex('#MyRed#,#MyGreen#,#MyBlue#')]
DynamicVariables=1

1.jpg
You do not have the required permissions to view the files attached to this post.
User avatar
balala
Rainmeter Sage
Posts: 8636
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Converting RGB to HEX with lua

balala » March 16th, 2019, 2:56 pm

I suppose jsmorley will disagree pretty much this, but this has been discussed before and I've suggested a solution which didn't use lua script for this conversion. Here it is, if you're interested: https://forum.rainmeter.net/viewtopic.php?f=119&t=26950
User avatar
jsmorley
Developer
Posts: 19474
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Converting RGB to HEX with lua

jsmorley » March 16th, 2019, 2:57 pm

balala wrote:
March 16th, 2019, 2:56 pm
I suppose jsmorley will disagree pretty much with this, but this has been discussed before and I've suggested a solution which didn't use lua script for this conversion. Here it is, if you're interested: https://forum.rainmeter.net/viewtopic.php?f=119&t=26950
No, I don't disagree. Simply converting RGB to HEX probably doesn't require Lua.

Again though, I like the fact that a simple function in Lua can be used a hundred places in your skin without having to duplicate any effort or create a bunch of extra measures.
User avatar
balala
Rainmeter Sage
Posts: 8636
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Converting RGB to HEX with lua

balala » March 16th, 2019, 2:59 pm

jsmorley wrote:
March 16th, 2019, 2:57 pm
No, I don't disagree. Simply converting RGB to HEX probably doesn't require Lua.
I'm glad. It was a suggestion in case my code fits StArL0rd84's needs.