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 OverflowPython
docs.python.org › 3 › library › colorsys.html
colorsys — Conversions between color systems
Convert the color from RGB coordinates to HLS coordinates.
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)
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
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
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
Top answer 1 of 15
282
I believe that this does what you are looking for:
h = input('Enter hex: ').lstrip('#')
print('RGB =', tuple(int(h[i:i+2], 16) for i in (0, 2, 4)))
(The above was written for Python 3)
Sample run:
Enter hex: #B4FBB8
RGB = (180, 251, 184)
Writing to a file
To write to a file with handle fhandle while preserving the formatting:
fhandle.write('RGB = {}'.format( tuple(int(h[i:i+2], 16) for i in (0, 2, 4)) ))
2 of 15
60
You can use ImageColor from Pillow.
>>> from PIL import ImageColor
>>> ImageColor.getcolor("#23a9dd", "RGB")
(35, 169, 221)
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
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 ...
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.