在Python3中将HSL转换为Hex

3

我有以下字符串,来自Atom One-Dark Syntax

one_dark_syn = """
@hue-1:   hsl(187, 47%, 55%); // <-cyan
@hue-2:   hsl(207, 82%, 66%); // <-blue
@hue-3:   hsl(286, 60%, 67%); // <-purple
@hue-4:   hsl( 95, 38%, 62%); // <-green
@hue-5:   hsl(355, 65%, 65%); // <-red 1
@hue-5-2: hsl(  5, 48%, 51%); // <-red 2
@hue-6:   hsl( 29, 54%, 61%); // <-orange 1
@hue-6-2: hsl( 39, 67%, 69%); // <-orange 2
"""

我想使用Python将HSL值转换为十六进制。到目前为止,我已经设计了一组相当丑陋的循环来解析这些值并将它们放入一个Numpy数组中:

od_colors = [i.split(";")[0].split(":")[1] for i in one_dark_syn.split("\n") if "hsl" in i]
od_colors = [i.strip().replace("%","").replace("hsl","").replace(" ","") for i in od_colors]
od_colors = [i.replace("(","").replace(")","").split(",") for i in od_colors]
od_colors = np.array(od_colors,dtype="int32")

就像我说的那样不太友好。有没有更具Python风格的解析方法?最重要的是,你会建议我如何将hsl转换为hex?


我投票关闭此问题,因为它属于CodeReview。 - Scott Hunter
你的HEX并不在所有可能的意义上都是HEX。首先,你混淆了色调(hue)和饱和度/明度(saturation/value),色调是0-359范围内的数字,而饱和度/明度是0-100范围内的数字。其次,你的数字仍然是十进制,而不是十六进制。模块colorsyshttps://docs.python.org/2/library/colorsys.html提供了在HSL和RGB之间转换的工具。 - DYZ
你的代码看起来很不错,你可以使用切片功能来获取数字 - 比如 '@hue-1: hsl(187, 47%, 55%); // <-cyan'[14:27] 可以得到 187, 47%, 55%. - furas
@scotthunter,为什么这个不是主题? - James Draper
你有正在运行的代码希望改进;这就是 Code Review 的用途。 - Scott Hunter
@scotthunter 我包含了一个我还没有解决的问题的部分解决方案。这怎么是离题的? - James Draper
2个回答

5
我会使用正则表达式:
regex = r'hsl\(\s*(\d+),\s*(\d+)%,\s*(\d+)%\s*\);'
lines = [re.findall(regex,line) for line in one_dark_syn.split('\n')]
rgbs = [colorsys.hsv_to_rgb(int(line[0][0])/360,
                            int(line[0][1])/100,
                            int(line[0][2])/100) for line in lines if line]
# [(0.2915, 0.519842, 0.55), (0.1188, 0.41646, 0.66), (0.5762, 0.268, 0.67), 
#  (0.48257, 0.62, 0.3844), (0.65, 0.2275, 0.2627), (0.51, 0.2856, 0.2652), 
#  (0.61, 0.43981, 0.2806), (0.69, 0.528195, 0.2277)]

最后,将RGB三元组转换为十六进制字符串:
rgbhex = ["".join("%02X" % round(i*255) for i in rgb) for rgb in rgbs]
# ['1D3437', '0C2A42', '3A1B43', '303E26', '41171A', '331D1B', '3D2C1C', '453517']

对于 rgbhex,应该将 i 乘以 255 而不是 100 吗? - tghw
@tghw 不,这些是小数。 - DYZ
它们是1的分数,但输出需要在0x00到0xff的范围内,即0到255。例如,白色在RGB中为(1.0, 1.0, 1.0),在十六进制中为#FFFFFF。但是,如果您只乘以100,(1.0, 1.0, 1.0)将变成#646464,这是一种中等深灰色。 - tghw

2
感谢您,@DYZ:
import re
import colorsys

json = '''
```json
{
    "variables":
    {
        "blue": "hsl(210, 50%, 60%)",
        "blue2": "hsl(180, 36%, 54%)",
        "green": "hsl(114, 31%, 68%)",
        "grey": "hsl(0, 0%, 73%)",
        "grey2": "hsl(0, 0%, 60%)",
        "grey3": "hsl(0, 0%, 20%)",
        "orange": "hsl(32, 93%, 66%)",
        "orange2": "hsl(32, 85%, 55%)",
        "orange3": "hsl(40, 94%, 68%)",
        "pink": "hsl(300, 30%, 68%)",
        "red": "hsl(357, 79%, 65%)",
        "red2": "hsl(13, 93%, 66%)",
        "red3": "hsl(16, 29%, 54%)",
        "white": "hsl(0, 0%, 97%)",
        "white2": "hsl(210, 11%, 85%)",
        "white3": "hsl(204, 10%, 90%)",
        "white4": "hsl(195, 11%, 93%)",
        "white5": "hsl(180, 9%, 98%)"
    }
}
'''

regex = r'hsl\(\s*(\d+),\s*(\d+)%,\s*(\d+)%\s*\)'
lines = re.findall(regex,json)
rgbs = [colorsys.hls_to_rgb(int(line[0])/360,int(line[2])/100,int(line[1])/100) for line in lines]
hexs = ['#%02x%02x%02x'%(round(rgb[0]*255),round(rgb[1]*255),round(rgb[2]*255)) for rgb in rgbs]
print(hexs)

为什么所有的结果都是000000? - E.Coms

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接