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']
Answer from DYZ on Stack Overflow
Top answer
1 of 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']
2 of 2
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)
Discussions

I wrote a plugin to cycle between `hex`, `rgb` and `hsl` color formats
I really liked the idea of this plugin, but I wasn't too much of a fan of using a python dependency. So it inspired me to put this together: https://github.com/amadeus/vim-convert-color-to It converts between `rgb`, `hsl` and `hex` (including transparent colors) and is all written in viml. More on reddit.com
🌐 r/vim
30
116
February 23, 2019
HEX, RGB or HSL?
I use hex, unless I also use an alpha channel, then I use rgba(). Why? I've used it for years, before rgb() was available. More on reddit.com
🌐 r/webdev
8
2
July 1, 2014
People also ask

How do I convert RGB to HSL in Python?
Use `colorsys.rgb_to_hls(r, g, b)` with values normalized to 0–1. Note the return order is (H, L, S) — hue, lightness, saturation — not (H, S, L). Multiply H by 360 for degrees, L and S by 100 for percentages. Example: `colorsys.rgb_to_hls(26/255, 115/255, 232/255)` returns `(0.603, 0.506, 0.798)` → H=217°, L=51%, S=80%.
🌐
theproductguy.in
theproductguy.in › home › blogs › color conversion in python
Color Conversion in Python | theproductguy.in
How do I convert hex to RGB in Python?
Remove the `#` prefix, then slice the six-character string into three two-character substrings and convert each with `int(s, 16)`. Example: `hex = '#1a73e8'; rgb = tuple(int(hex.lstrip('#')[i:i+2], 16) for i in (0, 2, 4))` returns `(26, 115, 232)`. For 3-digit shorthand hex, expand each digit before parsing.
🌐
theproductguy.in
theproductguy.in › home › blogs › color conversion in python
Color Conversion in Python | theproductguy.in
How do I generate a random hex color in Python?
Use `'#{:06x}'.format(random.randint(0, 0xFFFFFF))` for a random hex color. For a vivid random color, randomize only the hue: convert a random HSV value `(random.random(), 0.7, 0.9)` to RGB with `colorsys.hsv_to_rgb()`, then to hex. The fixed saturation/value ensures bright, readable output.
🌐
theproductguy.in
theproductguy.in › home › blogs › color conversion in python
Color Conversion in Python | theproductguy.in
🌐
GitHub
gist.github.com › mathebox › e0805f72e7db3269ec22
Python methods to convert colors between RGB, HSV and HSL · GitHub
@mathebox & @ArtemBernatskyy, According to https://en.wikipedia.org/wiki/HSL_and_HSV, add l = (high + low) / 2, and not low.
🌐
Towards Data Science
towardsdatascience.com › home › latest › how to convert a single hex color code into a monochrome color palette with python
How to Convert a Single HEX Color Code into a Monochrome Color Palette with Python | Towards Data Science
January 13, 2025 - It can generate an entire monochromatic color palette that’ll look stunning on any stacked chart. The downside is that getting there requires a fair bit of coding. You have to write custom functions to convert a color from HEX to HSL ...
🌐
PyPI
pypi.org › project › colorcycle
colorcycle · PyPI
November 9, 2024 - We convert the HEX color "#FF5733" to HSL values, where H is 9°, S is 100%, and L is 60%.
Find elsewhere
🌐
PyPI
pypi.org › project › colorutils
colorutils · PyPI
The current color models supported by colorutils are: RGB, HEX, WEB, YIQ, and HSV.
🌐
PyPI
pypi.org › project › colour
colour
JavaScript is disabled in your browser. Please enable JavaScript to proceed · A required part of this site couldn’t load. This may be due to a browser extension, network issues, or browser settings. Please check your connection, disable any ad blockers, or try using a different browser
🌐
Theproductguy
theproductguy.in › home › blogs › color conversion in python
Color Conversion in Python | theproductguy.in
January 29, 2026 - Convert colors between HEX, RGB, HSL, and CMYK in Python — with colorsys and custom functions.
🌐
TinyFn
tinyfn.io › home › tools › color utilities › hex to hsl
Hex To Hsl - Free Online Color Tool & API | TinyFn
March 8, 2026 - Converts hex color codes to HSL (Hue, Saturation, Lightness) format with precise calculations. Access via MCP in Cursor or Windsurf for instant color space conversions, or call GET /v1/color/hex-to-hsl directly.
🌐
GitHub
github.com › vikpe › chromato
GitHub - vikpe/chromato: Color utilities for Python. · GitHub
from chromato.spaces import Color red = Color(255, 0, 0) red.cmyk # CMYK(c=0, m=100, y=100, k=0) red.hex # HEX(ff0000) red.hsl # HSL(h=0, s=1, l=0.5) red.hsv # HSV(h=0, s=1, v=1) red.rgb # RGB(r=255, g=0, b=0) ... # examples below are equal ...
Author: vikpe
🌐
Readthedocs
python-colormath.readthedocs.io › en › latest › conversions.html
Color Conversions — python-colormath 3.0.0 documentation
If you would like to use a different ... from colormath.color_conversions import convert_color xyz = XYZColor(0.1, 0.2, 0.3) hsl = convert_color(xyz, HSLColor, through_rgb_type=AdobeRGBColor) # If you are going to convert back to XYZ, make sure you use the same # RGB color space ...
🌐
Color Designer
colordesigner.io › convert › hextohsl
Convert HEX to HSL | Color Designer
This tool is a free online color converter between two color formats HEX and HSL. The tool is fast and very simple to use.
🌐
Capytoolkit
capytoolkit.com › home › tools › design › color format converter › convert hex to hsl
HEX to HSL Color Converter - CapyToolkit
May 9, 2026 - Convert HEX color codes to HSL values for CSS. Get hsl() output from any #rrggbb code to build color scales, adjust lightness, or work with CSS color functions.
🌐
GitHub
github.com › SunilVeeravalli › rgb_hex_hsl_generator
GitHub - SunilVeeravalli/rgb_hex_hsl_generator: Generates distinct RGB, HEX and HSL format color codes · GitHub
In a command line terminal, move to rgb_hex_hsl_generator folder location on your system. C:\users\xyz\Desktop> cd rgb_hex_hsl_generator C:\users\xyz\Desktop\rgb_hex_hsl_generator> Run the script file rgb_hex_hsl_generator.py as shown below: C:\users\xyz\Desktop\rgb_hex_hsl_generator> python rgb_hex_hsl_generator.py
Author: SunilVeeravalli
🌐
Educative
educative.io › answers › how-to-convert-hex-to-rgb-and-rgb-to-hex-in-python
How to convert hex to RGB and RGB to hex in Python
We need to take two hex values for one RGB value, convert those two hex values to decimal values, and then perform the same step with the other values.
🌐
w3resource
w3resource.com › python-exercises › math › python-math-exercise-77.php
Python Math: Convert RGB color to HSV color - w3resource
Write a Python function that takes three integer values representing an RGB color and returns the HSV equivalent as a tuple.
🌐
Reddit
reddit.com › r/vim › i wrote a plugin to cycle between `hex`, `rgb` and `hsl` color formats
r/vim on Reddit: I wrote a plugin to cycle between `hex`, `rgb` and `hsl` color formats
February 23, 2019 - I really liked the idea of this plugin, but I wasn't too much of a fan of using a python dependency. So it inspired me to put this together: ... It converts between `rgb`, `hsl` and `hex` (including transparent colors) and is all written in viml.