Integer number from Lua calculation needed

Help creating logic scripts for Air Manager Instruments

Moderators: russ, Ralph

Message
Author
User avatar
Keith Baxter
Posts: 4674
Joined: Wed Dec 20, 2017 11:00 am
Location: Botswana

Re: Integer number from Lua calculation needed

#31 Post by Keith Baxter »

Hi,

Where theirs a will there is a way. :mrgreen:

I little messy but it works. I am sure the likes of Tony @Sling might be able to streamline A bit more. ;)
I used the hw adc to simulate a 0.0 to 1.0 input

You could use the same hex_table{} to manipulate the other R,G,B channels.

Code: Select all

my_canvas= canvas_add(10,10,400,400)
 
 hex_table = {"00","01","02","03","04","05","06","07","08","09","0A","0B","0C","0D","0E","0F","10","11","12","13","14","15","16","17","18","19","1A","1B","1C","1D","1E","1F","20","21","22","23","24","25","26","27","28","29","2A","2B","2C","2D","2E","2F","30","31","32","33","34","35","36","37","38","39","3A","3B","3C","3D","3E","3F","40","41","42","43","44","45","46","47","48","49","4A","4B","4C","4D","4E","4F","50","51","52","53","54","55","56","57","58","59","5A","5B","5C","5D","5E","5F","60","61","62","63","64","65","66","67","68","69","6A","6B","6C","6D","6E","6F","70","71","72","73","74","75","76","77","78","79","7A","7B","7C","7D","7E","7F","80","81","82","83","84","85","86","87","88","89","8A","8B","8C","8D","8E","8F","90","91","92","93","94","95","96","97","98","99","9A","9B","9C","9D","9E","9F","A0","A1","A2","A3","A4","A5","A6","A7","A8","A9","AA","AB","AC","AD","AE","AF","B0","B1","B2","B3","B4","B5","B6","B7","B8","B9","BA","BB","BC","BD","BE","BF","C0","C1","C2","C3","C4","C5","C6","C7","C8","C9","CA","CB","CC","CD","CE","CF","D0","D1","D2","D3","D4","D5","D6","D7","D8","D9","DA","DB","DC","DD","DE","DF","E0","E1","E2","E3","E4","E5","E6","E7","E8","E9","EA","EB","EC","ED","EE","EF","F0","F1","F2","F3","F4","F5","F6","F7","F8","F9","FA","FB","FC","FD","FE","FF"}
 
 new_value = 0
 
hw_adc_input_add("hex code gen)",function(value)
   new_value = math.floor(255 * value)
   new_string = "#000000"..tostring(hex_table[new_value])
 
   canvas_draw(my_canvas,function()
       _rect(10,10,200,200)
       _fill(new_string)
   end) 
end)

Keith


EDIT: Due to the dataref subscribe, I recommend using Asynchronous coding and or throttling the fires. This is because there will be millions of redraws every time a slight change in the dataref.
AMD RYZEN 9 5950X CPU, Corsair H80I cooler, ASUS TUF GAMING B550-PLUS AMD Ryzen Mother Board,  32Gb ram Corsair Vengeance 3000Mh, MSI GTX960 4G graphics card 

User avatar
Keith Baxter
Posts: 4674
Joined: Wed Dec 20, 2017 11:00 am
Location: Botswana

Re: Integer number from Lua calculation needed

#32 Post by Keith Baxter »

Hi,

@Detlef Ha Ha.
After doing the code in the above post, and mentioning Tony to streamline the code, the obvious hit me. Face palm. I had a doof day yesterday. Sorry
I feel a whole lot stupid and embarrassed. Ek voel soos n groot doos :oops: And I am sure @Corjan is going to spit out his coffee and laugh his head off after reading this.
giphy (2).gif
giphy (2).gif (1.38 MiB) Viewed 663 times
The cool thing is that your issue is sorted.

It is this easy. As long as your input is in the 0.0 to 1.0 range.

Code: Select all

local input = 0.5
local output = string.format("%x", tostring(math.floor(input * 255)))
print(output)   -- output should print -- "7F"
Happy days. :D

PS.. I enjoyed going round and round the mulberry bush. And yes I know the comments will come. Please be gentle guys. I am going to hide away for a bit. :oops:

Keith
AMD RYZEN 9 5950X CPU, Corsair H80I cooler, ASUS TUF GAMING B550-PLUS AMD Ryzen Mother Board,  32Gb ram Corsair Vengeance 3000Mh, MSI GTX960 4G graphics card 

Tetrachromat
Posts: 236
Joined: Sun Feb 14, 2021 6:55 pm

Re: Integer number from Lua calculation needed

#33 Post by Tetrachromat »

No need to excuse. Sometimes it takes diversions to come up with a solution.

But I would suggest to include at least a 'precision' tag in the format string to produce strings of length 2 in any case.
And for heavens sake remove that call to 'tostring' as the format specifier 'x' or 'X' asks for an integer argument. That call requires LUA to convert the string back to an integer.

You may also replace the call to 'math.floor' with the floor division as suggested in my previous post.

Code: Select all

local output = string.format("%.2x", input * 255 // 1)
Paul

User avatar
Keith Baxter
Posts: 4674
Joined: Wed Dec 20, 2017 11:00 am
Location: Botswana

Re: Integer number from Lua calculation needed

#34 Post by Keith Baxter »

Thanks for that Paul,

So simple when you think about it.

Keith
AMD RYZEN 9 5950X CPU, Corsair H80I cooler, ASUS TUF GAMING B550-PLUS AMD Ryzen Mother Board,  32Gb ram Corsair Vengeance 3000Mh, MSI GTX960 4G graphics card 

Detlef
Posts: 304
Joined: Mon Nov 16, 2020 9:43 am
Location: Bavaria

Re: Integer number from Lua calculation needed

#35 Post by Detlef »

Hi @Keith and all,

no need to apologize for anything. Still, that // operator does not work for me. In my "instrument" attached option 1 and 3 do work, but number 2 not.

Code: Select all

brightness_lines = 0
gColorPrint = "blue"

function DrawCanvas()
   _rect(10, 10 ,200, 200)
   _fill(gColorPrint)
end

gCanvas = canvas_add(10, 10, 400, 400, DrawCanvas)


timer_start(500, 500, 10, function()
  brightness_lines = brightness_lines+.1
  print(brightness_lines)
  gColorPrint = string.format("#%02X%02X%02XFF", math.floor(180*brightness_lines),  math.floor(255*brightness_lines), math.floor(80*brightness_lines))
  canvas_draw(gCanvas, DrawCanvas)
end)


timer_start(7000, 500, 10, function()
  brightness_lines = brightness_lines-.1
  print(brightness_lines)
  gColorPrint = string.format("#%.2X%.2X%.2XFF", 180*brightness_lines*255//1,  255*brightness_lines*255//1, 80*brightness_lines*255//1)
  canvas_draw(gCanvas, DrawCanvas)
end)


hex_table = {"00","01","02","03","04","05","06","07","08","09","0A","0B","0C","0D","0E","0F","10","11","12","13","14","15","16","17","18","19","1A","1B","1C","1D","1E","1F","20","21","22","23","24","25","26","27","28","29","2A","2B","2C","2D","2E","2F","30","31","32","33","34","35","36","37","38","39","3A","3B","3C","3D","3E","3F","40","41","42","43","44","45","46","47","48","49","4A","4B","4C","4D","4E","4F","50","51","52","53","54","55","56","57","58","59","5A","5B","5C","5D","5E","5F","60","61","62","63","64","65","66","67","68","69","6A","6B","6C","6D","6E","6F","70","71","72","73","74","75","76","77","78","79","7A","7B","7C","7D","7E","7F","80","81","82","83","84","85","86","87","88","89","8A","8B","8C","8D","8E","8F","90","91","92","93","94","95","96","97","98","99","9A","9B","9C","9D","9E","9F","A0","A1","A2","A3","A4","A5","A6","A7","A8","A9","AA","AB","AC","AD","AE","AF","B0","B1","B2","B3","B4","B5","B6","B7","B8","B9","BA","BB","BC","BD","BE","BF","C0","C1","C2","C3","C4","C5","C6","C7","C8","C9","CA","CB","CC","CD","CE","CF","D0","D1","D2","D3","D4","D5","D6","D7","D8","D9","DA","DB","DC","DD","DE","DF","E0","E1","E2","E3","E4","E5","E6","E7","E8","E9","EA","EB","EC","ED","EE","EF","F0","F1","F2","F3","F4","F5","F6","F7","F8","F9","FA","FB","FC","FD","FE","FF"}
 
function HexaMagic(v)
  return hex_table[math.floor(v)]
end
 
hw_adc_input_add("hex code gen)",function(value)
   new_value = math.floor(255 * value)
   new_string = "#000000"..tostring(hex_table[new_value])
 
   canvas_draw(my_canvas,function()
       _rect(10,10,200,200)
       _fill(new_string)
   end) 
end)
timer_start(14000, 500, 10, function()
  brightness_lines = brightness_lines+.1
  print(brightness_lines)
  gColorPrint = "#" .. HexaMagic(180*brightness_lines) .. HexaMagic(255*brightness_lines) .. HexaMagic(80*brightness_lines) .. "FF"
  canvas_draw(gCanvas, DrawCanvas)
end)



-- canvas_add(10, 10, 400, 400, function()
   -- _rect(10, 10 ,200, 200)
   -- _fill(gColorPrint)
-- end)
Generic - Utility Color Formatting.siff
(4.31 KiB) Downloaded 53 times
So I seem to have a solution, and by now don't understand why I thought in the first place that math.floor() did not return an integer. It does work in this example. Thank you again for your help. There is no need at the moment for more suggestions :) I am in the process of changing my instrument illumination anyway. I will get new errors then, I am sure.

Thank you
Detlef

Tetrachromat
Posts: 236
Joined: Sun Feb 14, 2021 6:55 pm

Re: Integer number from Lua calculation needed

#36 Post by Tetrachromat »

@Detlef option 2 does not work beacuse you multiply each part with 255. This gives way to high numbers requiring more than 2 hex digits.

Code: Select all

gColorPrint = string.format("#%.2X%.2X%.2XFF", 180*brightness_lines*255//1,  255*brightness_lines*255//1, 80*brightness_lines*255//1)
To be comparable, this should be written as:

Code: Select all

gColorPrint = string.format("#%.2X%.2X%.2XFF", 180*brightness_lines//1,  255*brightness_lines//1, 80*brightness_lines//1)
Paul

Detlef
Posts: 304
Joined: Mon Nov 16, 2020 9:43 am
Location: Bavaria

Re: Integer number from Lua calculation needed

#37 Post by Detlef »

@Tetrachromat ,

thank you Paul. That works! I see that now. Sometimes simple things are difficult, but your solution is really elegant and short.

And thanks to all
Detlef

Post Reply