python to lua converter

Solutions on MaxInterview for python to lua converter by the best coders in the world

showing results for - "python to lua converter"
Andrea
01 Oct 2020
1I wound up figuring it out on my own after some hardcore trial and error. The function rgb2short should have been:
2
3 function rgb2short(rgb)
4     -- Find closest xterm-256 approximation to the given RGB value
5     _create_dicts()
6     rgb = _strip_hash(rgb)
7     local res = ""
8     local equiv = ""
9
10     local incs = {"0x00", "0x5f", "0x87", "0xaf", "0xd7", "0xff"}
11
12     for part in string.gmatch(rgb, "(..)") do
13         part = tonumber(part, 16)
14         i = 1
15         while i < #incs-1 do
16             s, b = tonumber(incs[i]), tonumber(incs[i+1])
17             if s <= part and part <= b then
18                 s1 = math.abs(s - part)
19                 b1 = math.abs(b - part)
20                 --break
21             --end
22
23                 if s1 < b1 then
24                     closest = s
25                 else
26                     closest = b
27                 end
28                 res = res .. string.format("%02x", closest)
29                 break
30             end
31             i = i + 1
32         end
33     end
34
35
36     equiv = rgb2short_dict[res]
37
38     return equiv, res
39 end
similar questions
how to print in lua