It is currently May 20th, 2024, 8:47 am

raincolors.lua [1.2.18.03.24]

Skins that control functions in Windows or Rainmeter
RicardoTM
Posts: 274
Joined: December 28th, 2022, 9:30 pm
Location: México

Re: colors.lua

Post by RicardoTM »

Alright, it's practically finished, now I need your help to optimize it if needed, as this is practically my first time on Lua for real.

but before I post it I just need help with some final thing, there are 3 functions that generate shades, tints and tones. As I wrote it it only can generate 2 at a time, so I need some form of way to let the user select how many they want. All 3 functions look like this:

Code: Select all

--------------------------------------------------------------------------
---	Returns shades, 2 colors based on 1					
---	to separate string see splitComplementary ^^^			
---	color r,g,b			0-255					
---	return r,g,b,r,g,b		0-255					
------------------------------------------------------------------------------
function shades(color,amount)
	local amount = amount or 10
	local color1 = shiftLight(color, -amount)
	local color2 = shiftLight(color, -amount-amount)
	return string.format('%s,%s',color1,color2)
end
I want it to be function shades(color,N,amount).
Where N is the number of shades to generate
amount is how much the color dims , if no amount entered, then it should be relative to the number of shades.
how it should work: https://colors.dopely.top/color-toner/3e97c1-shades-18

Just to clarify, this approach will need the user to separate the colors using substitute (which would also need optimization) on a measure:

Code: Select all

[shade2]
measure=String
string=[&test:shades('[#Color]')]
RegExpSubstitute=1
Substitute="(\d+,\d+,\d+),(\d+,\d+,\d+)":"\2"
DynamicVariables=1
Captura de pantalla 2024-03-07 145514.jpg
You do not have the required permissions to view the files attached to this post.
RicardoTM
Posts: 274
Joined: December 28th, 2022, 9:30 pm
Location: México

Re: colors.lua

Post by RicardoTM »

Yincognito wrote: March 7th, 2024, 5:32 pm I though you talked about retrieving them in Rainmeter from Lua (at least that's what I was talking about), so not sure how setting / writing them from Rainmeter achieves that, but anyway, maybe we referred to different things.
Sorry, I explained myself incorrectly (and changed my mind on part). This will be only a library of functions, how the colors are set or how they will be used is up to whoever is using the functions on their skin, maybe someone prefers to use the [&Measure:Function()] directly on the color option, someone will create measures and use those measures, or use those measures to set variables, etc. This is basically a color generator/manipulator.

Yincognito wrote: March 7th, 2024, 5:32 pm and if proper scripted using the ... variable number of parameters, control how many you need too
This might be part of the solution to the problem described on my post above.
User avatar
Yincognito
Rainmeter Sage
Posts: 7291
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: colors.lua

Post by Yincognito »

RicardoTM wrote: March 7th, 2024, 8:48 pm Alright, it's practically finished, now I need your help to optimize it if needed, as this is practically my first time on Lua for real.

but before I post it I just need help with some final thing, there are 3 functions that generate shades, tints and tones. As I wrote it it only can generate 2 at a time, so I need some form of way to let the user select how many they want. All 3 functions look like this:

Code: Select all

--------------------------------------------------------------------------
---	Returns shades, 2 colors based on 1					
---	to separate string see splitComplementary ^^^			
---	color r,g,b			0-255					
---	return r,g,b,r,g,b		0-255					
------------------------------------------------------------------------------
function shades(color,amount)
	local amount = amount or 10
	local color1 = shiftLight(color, -amount)
	local color2 = shiftLight(color, -amount-amount)
	return string.format('%s,%s',color1,color2)
end
I want it to be function shades(color,N,amount).
Where N is the number of shades to generate
amount is how much the color dims , if no amount entered, then it should be relative to the number of shades.
how it should work: https://colors.dopely.top/color-toner/3e97c1-shades-18

Just to clarify, this approach will need the user to separate the colors using substitute on a measure (which would also need optimization):

Code: Select all

[shade2]
measure=String
string=[&test:shades('[#Color]')]
RegExpSubstitute=1
Substitute="(\d+,\d+,\d+),(\d+,\d+,\d+)":"\2"
DynamicVariables=1
Captura de pantalla 2024-03-07 145514.jpg
I don't know how the shiftLight() functions work - that will be your job to adapt -, but I'd do something along the lines of (N + 1 is count below):

Code: Select all

function getshades(color, count, amount)
  -- get the r, g, and b from color here
  if amount == nil then amount = max(r, g, b) / count end
  local shades = {color}
  for i = 1, count do
    -- subtract proportionally from r, g, and b inside shiftLight()
    shades[i + 1] = shiftLight(shades[i], - amount)
  end
  return table.concat(shades, ';')
end
If adapted correctly, the above should produce all the shades, including the original color (which is used as a base to successively shiftLight() for count times in the FOR loop in the script). I'm not that much into string.format(), and in this case table.concat() is more suited since you build a shades table anyway. The result would look like '0,139,204;0,104,153;...' for better clarity as to where each 'r,g,b' set in the string ends. I suppose that's more or less how the site you linked to computes the amount, from what I could tell. Didn't use the variable number of parameters above since, after all, there is a single "optional" parameter, aka amount, and although it will occupy more space than not writing it when you call the function, it can easily be made to "not exist" by passing nil as the parameter. If you really want to use it, just replace amount with ... in the function definition and use arg[3] instead of amount (or declare a local amount set to arg[3]) in the function body (arg is a table holding all the arguments / parameters to a function). For the record, a quick way to refer to the number of elements in sometable is to write #sometable.
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth
RicardoTM
Posts: 274
Joined: December 28th, 2022, 9:30 pm
Location: México

Re: colors.lua

Post by RicardoTM »

Yincognito wrote: March 7th, 2024, 10:13 pm I don't know how the shiftLight() functions work - that will be your job to adapt -, but I'd do something along the lines of (N + 1 is count below):

Code: Select all

function getshades(color, count, amount)
  -- get the r, g, and b from color here
  if amount == nil then amount = max(r, g, b) / count end
  local shades = {color}
  for i = 1, count do
    -- subtract proportionally from r, g, and b inside shiftLight()
    shades[i + 1] = shiftLight(shades[i], - amount)
  end
  return table.concat(shades, ';')
end
If adapted correctly, the above should produce all the shades, including the original color (which is used as a base to successively shiftLight() for count times in the FOR loop in the script). I'm not that much into string.format(), and in this case table.concat() is more suited since you build a shades table anyway. The result would look like '0,139,204;0,104,153;...' for better clarity as to where each 'r,g,b' set in the string ends. I suppose that's more or less how the site you linked to computes the amount, from what I could tell. Didn't use the variable number of parameters above since, after all, there is a single "optional" parameter, aka amount, and although it will occupy more space than not writing it when you call the function, it can easily be made to "not exist" by passing nil as the parameter. If you really want to use it, just replace amount with ... in the function definition and use arg[3] instead of amount (or declare a local amount set to arg[3]) in the function body (arg is a table holding all the arguments / parameters to a function). For the record, a quick way to refer to the number of elements in sometable is to write #sometable.
Thanks, this is how shiftLight() works:

Code: Select all

function shiftLight(color,amount)
--- splits r,g,b
	local r, g, b = split(color)
	--- converts to HSL
    local h, s, l = rgb2hsl(r, g, b)
    --- shifts l by amount
	local amount = amount or 60
    local l = (l + amount/100)
    local l = math.max(0, math.min(1, l))
    --- converts back to RGB
    local r,g,b = hsl2rgb(h, s, l)
    ---returns r,g,b
    return string.format('%d,%d,%d', r, g, b)
end
By the way, I don't really want it to return the original.
User avatar
Yincognito
Rainmeter Sage
Posts: 7291
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: colors.lua

Post by Yincognito »

RicardoTM wrote: March 7th, 2024, 10:25 pm Thanks, this is how shiftLight() works:

Code: Select all

function shiftLight(color,amount)
--- splits r,g,b
	local r, g, b = split(color)
	--- converts to HSL
    local h, s, l = rgb2hsl(r, g, b)
    --- shifts l by amount
	local amount = amount or 60
    local l = (l + amount/100)
    local l = math.max(0, math.min(1, l))
    --- converts back to RGB
    local r,g,b = hsl2rgb(h, s, l)
    ---returns r,g,b
    return string.format('%d,%d,%d', r, g, b)
end
Yeah, well, not entirely sure, but I believe it's equivalent to adding / subtracting proportional quantities from r, g, and b anyway, if I'm not mistaken. For example, if color is '100,200,50' and amount is -50, then the result would be something like '(100-50*(100/200)),(200-50*(200/200)),(50-50*(50/200))', with the full -50 added to the maximum of 200. It probably involves some clamping so that stuff don't go below 0 or above 255. Obviously, the proper "scientific" way to do it would be through color spaces like you did, I'm just talking about the rudimentar way of thinking about it.

P.S. Just noticed that I wrote plain max(), when I should have wrote math.max() above. :oops:
RicardoTM wrote: March 7th, 2024, 10:25 pmBy the way, I don't really want it to return the original.
Then change it accordingly. :D
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth
RicardoTM
Posts: 274
Joined: December 28th, 2022, 9:30 pm
Location: México

Re: colors.lua

Post by RicardoTM »

Yincognito wrote: March 7th, 2024, 10:48 pm Yeah, well, not entirely sure, but I believe it's equivalent to adding / subtracting proportional quantities from r, g, and b anyway, if I'm not mistaken. For example, if color is '100,200,50' and amount is -50, then the result would be something like '(100-50*(100/200)),(200-50*(200/200)),(50-50*(50/200))', with the full -50 added to the maximum of 200. It probably involves some clamping so that stuff don't go below 0 or above 255. Obviously, the proper "scientific" way to do it would be through color spaces like you did, I'm just talking about the rudimentar way of thinking about it.

P.S. Just noticed that I wrote plain max(), when I should have wrote math.max() above. :oops:
that is handled by the hsl function, so no problem there, it won't ever go below 0 or beyond 100.
wrote it like this:

Code: Select all

function shades(color,count,amount)
   local count = count or 2
  local amount = amount or 100/count
  local shades = {color}
  for i = 1, count do
    -- subtract proportionally from r, g, and b inside shiftLight()
    shades[i + 1] = shiftLight(shades[i], - amount)
  end
  return table.concat(shades, ';')
end
ifs are not necessary, this handles it correctly. I just need to know how to avoid it from parsing the base color, I just need from color 2.
That's because if you tell shades(color,count,amount) to shades(color,2,10), it will in reality give 3 colors, the base and the 2 shades, but the basic is not needed since it's already known and the result is counter-intuitive.
User avatar
Yincognito
Rainmeter Sage
Posts: 7291
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: colors.lua

Post by Yincognito »

RicardoTM wrote: March 7th, 2024, 10:55 pm that is handled by the hsl function, so no problem there, it won't ever go below 0 or beyond 100.
wrote it like this:

Code: Select all

function shades(color,count,amount)
   local count = count or 2
  local amount = amount or 100/count
  local shades = {color}
  for i = 1, count do
    -- subtract proportionally from r, g, and b inside shiftLight()
    shades[i + 1] = shiftLight(shades[i], - amount)
  end
  return table.concat(shades, ';')
end
ifs are not necessary, this handles it correctly. I just need to know how to avoid it from parsing the base color, I just need from color 2.
That's because if you tell shades(color,count,amount) to shades(color,2,10), it will in reality give 3 colors, the base and the 2 shades, but the basic is not needed since it's already known and the result is counter-intuitive.
If you say so. The reason why I included the original color was because I didn't want to add an if in the loop which will subtract from color instead of "shades[0]", that's all. It's trivial to add it, just be creative:

Code: Select all

  local shades = {}
  for i = 1, count do
    if i == 1 then shades[i] = shiftLight(color, - amount) else shades[i] = shiftLight(shades[i - 1], - amount) end
  end
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth
RicardoTM
Posts: 274
Joined: December 28th, 2022, 9:30 pm
Location: México

Re: colors.lua

Post by RicardoTM »

Yincognito wrote: March 7th, 2024, 11:09 pm If you say so. The reason why I included the original color was because I didn't want to add an if in the loop which will subtract from color instead of "shades[0]", that's all. It's trivial to add it, just be creative:

Code: Select all

  local shades = {}
  for i = 1, count do
    if i == 1 then shades[i] = shiftLight(color, - amount) else shades[i] = shiftLight(shades[i - 1], - amount) end
  end
Aight, that's creative. Thanks, that fixes it. I have some stuff to do. I hope to post the whole code later tonight.
User avatar
Yincognito
Rainmeter Sage
Posts: 7291
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: colors.lua

Post by Yincognito »

RicardoTM wrote: March 7th, 2024, 11:57 pm Aight, that's creative. Thanks, that fixes it. I have some stuff to do. I hope to post the whole code later tonight.
Okeydokey. :great:
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth
RicardoTM
Posts: 274
Joined: December 28th, 2022, 9:30 pm
Location: México

Re: raincolors.lua

Post by RicardoTM »

Alright, finally released it. Edited first post