openalex_mapper / color_utils.py
m7n's picture
Implement DOI list handling in CSV uploads and add highlight color feature in predictions. Refactor embedding creation function for clarity and enhance OpenAlex record fetching with new utility function.
cabc445
raw
history blame
644 Bytes
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'