lua加速python_从Python到Lua的代码转换几乎完成了

我发现了一个Python脚本,我正试图将其转换为Lua。我相信我已经完成了转换,但是代码不能正常工作,所以我需要帮助,因为我根本不懂Python,只能猜测其意图。这仅仅是一个将RGB颜色转换为xterm 256的颜色转换器。这张桌子很大,为了便于阅读,我把它截了。在

Python代码:import sys, re

CLUT = [ # color look-up table

# 8-bit, RGB hex

# Primary 3-bit (8 colors). Unique representation!

('00', '000000'),

('01', '800000'),

('02', '008000'),

('03', '808000'),

('04', '000080'),

('05', '800080'),

('06', '008080'),

('07', 'c0c0c0'),

]

def _str2hex(hexstr):

return int(hexstr, 16)

def _strip_hash(rgb):

# Strip leading `#` if exists.

if rgb.startswith('#'):

rgb = rgb.lstrip('#')

return rgb

def _create_dicts():

short2rgb_dict = dict(CLUT)

rgb2short_dict = {}

for k, v in short2rgb_dict.items():

rgb2short_dict[v] = k

return rgb2short_dict, short2rgb_dict

def short2rgb(short):

return SHORT2RGB_DICT[short]

def print_all():

""" Print all 256 xterm color codes.

"""

for short, rgb in CLUT:

sys.stdout.write('\033[48;5;%sm%s:%s' % (short, short, rgb))

sys.stdout.write("\033[0m ")

sys.stdout.write('\033[38;5;%sm%s:%s' % (short, short, rgb))

sys.stdout.write("\033[0m\n")

print "Printed all codes."

print "You can translate a hex or 0-255 code by providing an argument."

def rgb2short(rgb):

""" Find the closest xterm-256 approximation to the given RGB value.

@param rgb: Hex code representing an RGB value, eg, 'abcdef'

@returns: String between 0 and 255, compatible with xterm.

>>> rgb2short('123456')

('23', '005f5f')

>>> rgb2short('ffffff')

('231', 'ffffff')

>>> rgb2short('0DADD6') # vimeo logo

('38', '00afd7')

"""

rgb = _strip_hash(rgb)

incs = (0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff)

# Break 6-char RGB code into 3 integer vals.

parts = [ int(h, 16) for h in re.split(r'(..)(..)(..)', rgb)[1:4] ]

res = []

for part in parts:

i = 0

while i < len(incs)-1:

s, b = incs[i], incs[i+1] # smaller, bigger

if s <= part <= b:

s1 = abs(s - part)

b1 = abs(b - part)

if s1 < b1: closest = s

else: closest = b

res.append(closest)

break

i += 1

#print '***', res

res = ''.join([ ('%02.x' % i) for i in res ])

equiv = RGB2SHORT_DICT[ res ]

#print '***', res, equiv

return equiv, res

RGB2SHORT_DICT, SHORT2RGB_DICT = _create_dicts()

#---------------------------------------------------------------------

if __name__ == '__main__':

import doctest

doctest.testmod()

if len(sys.argv) == 1:

print_all()

raise SystemExit

arg = sys.argv[1]

if len(arg) < 4 and int(arg) < 256:

rgb = short2rgb(arg)

sys.stdout.write('xterm color \033[38;5;%sm%s\033[0m -> RGB exact \033[38;5;%sm%s\033[0m' % (arg, arg, arg, rgb))

sys.stdout.write("\033[0m\n")

else:

short, rgb = rgb2short(arg)

sys.stdout.write('RGB %s -> xterm color approx \033[38;5;%sm%s (%s)' % (arg, short, short, rgb))

sys.stdout.write("\033[0m\n")

以及我几乎完全翻译的Lua代码:

^{pr2}$

我意识到我遗漏了代码的打印部分,但我不确定这是否相关,我知道我翻译的一些代码根本就不正确,因为脚本会正常工作。我得到的失败是由于rgb2short函数没有返回正确的equiv和{}值。我离复习还有多远?我需要做些什么改变才能让它完全正常工作?在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值