A list comprehension could be helpful here. You can try something like:
hex_values = ['#b4a795', '#e7e0d6', '#d7cfc8']
hsv_values = [rgb2hsv(*hex2rgb(hex_val)) for hex_val in hex_values]
Answer from jprebys on Stack Overflow Top answer 1 of 4
2
A list comprehension could be helpful here. You can try something like:
hex_values = ['#b4a795', '#e7e0d6', '#d7cfc8']
hsv_values = [rgb2hsv(*hex2rgb(hex_val)) for hex_val in hex_values]
2 of 4
0
put your hex values in a list, then iterate it.
hexValues = ["#b4a795", "#e7e0d6", "#d7cfc8"]
hsvValues = []
for i in hexValues:
hsvValues.append(rgb2hsv(*hex2rgb(i)))
print(hsvValues)
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)
Code Beautify
codebeautify.org › hex-to-hsv-converter
HEX to HSV - Best Online Tool to Convert HEX Color to HSV Color
Free HEX to HSV Color Converter to convert HEX to HSV code.
Color Designer
colordesigner.io › convert › hsvtohex
Convert HSV to HEX | Color Designer
HSL (hue, saturation, lightness) and HSV (hue, saturation, value) are alternative representations of the RGB color model, designed in the 1970s by computer graphics researchers to more closely align with the way human vision perceives color-making attributes. In these models, colors of each hue are arranged in a radial slice, around a central axis of neutral colors which ranges from black at the bottom to white at the top.Read More on Wikipedia · A hex triplet is a six-digit, three-byte hexadecimal number used in HTML, CSS, SVG, and other computing applications to represent colors.
GeeksforGeeks
geeksforgeeks.org › hsv-to-hex-converter
Online HSV to HEX Color Converter %%page%% %%sep%% %%sitename%% - GeeksforGeeks
April 26, 2024 - Each HEX code consists of six alphanumeric characters (0-9 and A-F), representing the intensities of red, green, and blue (RGB). The format is #RRGGBB, where RR, GG, and BB represent the red, green, and blue components, respectively. This Converter translates colors between HSV (Hue, Saturation, Value) and HEX models, allowing users to convert easily between these color representations, offering a more intuitive way to manipulate and understand color conversion.
Color Designer
colordesigner.io › convert › hextohsv
Convert HEX to HSV | Color Designer
This tool is a free online color converter between two color formats HEX and HSV. The tool is fast and very simple to use.
Faker
faker.readthedocs.io › en › master › providers › faker.providers.color.html
faker.providers.color — Faker 40.43.0 documentation
To keep the interface simple, this argument either can be omitted or can accept the following string values:'bright', 'dark', 'light', or 'random'. The argument color_format controls in which color model the color is represented. Valid values are 'hsv', 'hsl', 'rgb', or 'hex' (default).
BFO Tool
bfotool.com › hex-to-hsv
HEX to HSV Converter Online - bfotool
One byte represents a number in the range 00 to FF (in hexadecimal notation), or 0 to 255 in decimal notation. HSL (hue, saturation, lightness) and HSV (hue, saturation, value) are alternative representations of the RGB color model, designed in the 1970s by computer graphics researchers to more closely align with the way human vision perceives color-making attributes.
ActiveState
code.activestate.com › recipes › 576919-python-rgb-and-hsv-conversion
Python RGB and HSV Conversion « Python recipes « ActiveState Code
October 1, 2009 - Implementation based on pseudo-code from Wikipedia. http://en.wikipedia.org/wiki/HSL_and_HSV ... Wouldn't it have been easier to use the "colorsys" module? It's been part of Python since version 1.5.2 in 1998.
RapidTables
rapidtables.com › convert › color › rgb-to-hsv.html
RGB to HSV conversion | color conversion
Home›Conversion›Color conversion› RGB to HSV · Enter 6 digits hex code or enter red, green and blue color levels (0..255) and press the Convert button: HSV to RGB conversion ► · The R,G,B values are divided by 255 to change the range from 0..255 to 0..1: R' = R/255 ·
Minifier
minifier.org › hex-to-hsv-converter
HEX to HSV Converter
Hex to HSV converter is a free online tool that changes HEX color codes to HSV. Enter the hex color code, hit the convert button, and copy the HSV version.