Spaces:
Running
on
Zero
Running
on
Zero
File size: 644 Bytes
cabc445 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import re
def rgba_to_hex(color):
"""Convert rgba or rgb string to hex, or return hex if already hex."""
if isinstance(color, str):
color = color.strip()
# If already hex
if color.startswith('#') and (len(color) == 7 or len(color) == 4):
return color
# If rgba or rgb
match = re.match(r"rgba?\\(([^)]+)\\)", color)
if match:
parts = match.group(1).split(',')
r = int(float(parts[0]))
g = int(float(parts[1]))
b = int(float(parts[2]))
return '#{:02x}{:02x}{:02x}'.format(r, g, b)
# fallback
return '#5e2784' |