我有以下字符串,来自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?
colorsys
https://docs.python.org/2/library/colorsys.html提供了在HSL和RGB之间转换的工具。 - DYZ'@hue-1: hsl(187, 47%, 55%); // <-cyan'[14:27]
可以得到187, 47%, 55%
. - furas