3

I have the following string taken from the 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
"""

I would like to use python to exchange the hsl values for hex. So far I've concocted a pretty nasty looking set of loops to parse out the values into a numpy array:

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")

See like I said nasty. Does anyone have a more pythonic way of parsing this? and most importantly how would you recommend I go about converting hsl to hex?

6
  • I'm voting to close this question as off-topic because it belongs in CodeReview. Dec 30, 2016 at 23:33
  • Your HEX is not actually HEX in every possible meaning. First, you confuse apples (hue, a number in the range 0-359) and saturation/value (numbers in the range 0-100). Second, your numbers are still decimal, not hexadecimal. Module colorsys docs.python.org/2/library/colorsys.html has tools for conversion between HSL and RGB.
    – DYZ
    Dec 30, 2016 at 23:35
  • you have nice looking lines and you could use slicing to get numbers - ie '@hue-1: hsl(187, 47%, 55%); // <-cyan'[14:27] gives 187, 47%, 55%
    – furas
    Dec 30, 2016 at 23:45
  • @scotthunter why is this off topic? Dec 31, 2016 at 0:08
  • You have working code you wish to improve; that's what Code Review is for. Dec 31, 2016 at 13:12

2 Answers 2

5

I would use regular expressions:

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)]

Finally, convert RGB triplets to a HEX string:

rgbhex = ["".join("%02X" % round(i*255) for i in rgb) for rgb in rgbs]
# ['1D3437', '0C2A42', '3A1B43', '303E26', '41171A', '331D1B', '3D2C1C', '453517']
3
  • For rgbhex, shouldn't i be multiplied by 255 instead of 100?
    – tghw
    Dec 11, 2019 at 17:52
  • @tghw No, these are fractions of 1.
    – DYZ
    Dec 11, 2019 at 18:31
  • They are fractions of 1, but the output needs to be on the scale of 0x00 to 0xff, which is 0 to 255. For example, white is (1.0, 1.0, 1.0) in rgb and #FFFFFF in hex. But if you only multiply by 100, (1.0, 1.0, 1.0) will turn into #646464, which is a mid-dark gray.
    – tghw
    Dec 11, 2019 at 18:47
2

Thank you, @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)
1
  • why all result is 000000
    – E.Coms
    Oct 13, 2018 at 20:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.