python to lua converter

I wound up figuring it out on my own after some hardcore trial and error. The function rgb2short should have been:

 function rgb2short(rgb)
     -- Find closest xterm-256 approximation to the given RGB value
     _create_dicts()
     rgb = _strip_hash(rgb)
     local res = ""
     local equiv = ""

     local incs = {"0x00", "0x5f", "0x87", "0xaf", "0xd7", "0xff"}

     for part in string.gmatch(rgb, "(..)") do
         part = tonumber(part, 16)
         i = 1
         while i < #incs-1 do
             s, b = tonumber(incs[i]), tonumber(incs[i+1])
             if s <= part and part <= b then
                 s1 = math.abs(s - part)
                 b1 = math.abs(b - part)
                 --break
             --end

                 if s1 < b1 then
                     closest = s
                 else
                     closest = b
                 end
                 res = res .. string.format("%02x", closest)
                 break
             end
             i = i + 1
         end
     end


     equiv = rgb2short_dict[res]

     return equiv, res
 end

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source