forked from LiveCarta/BookConverter
epub converter: add rgba to rgb conversion
This commit is contained in:
@@ -78,6 +78,14 @@ def str2closest_html_color_name(s: str):
|
||||
return ''
|
||||
|
||||
|
||||
def rgba2rgb(r, g, b, alpha):
|
||||
r_background, g_background, b_background = 255, 255, 255
|
||||
r_new = int((1 - alpha) * r_background + alpha * r)
|
||||
g_new = int((1 - alpha) * g_background + alpha * g)
|
||||
b_new = int((1 - alpha) * b_background + alpha * b)
|
||||
return r_new, g_new, b_new
|
||||
|
||||
|
||||
def str2hex(s: str):
|
||||
if '#' in s:
|
||||
return s.lower()
|
||||
@@ -89,20 +97,26 @@ def str2hex(s: str):
|
||||
return rgb_percent_to_hex((r, g, b))
|
||||
|
||||
if 'rgb' in s:
|
||||
match = re.search(r'rgba*\(((\d+), *(\d+), *(\d+)(, \d\.\d+)*)\)', s)
|
||||
match = re.search(r'rgba*\(((\d+), *(\d+), *(\d+), (\d\.\d+)*)\)', s)
|
||||
if match:
|
||||
r, g, b = int(match.group(2)), int(match.group(3)), int(match.group(4))
|
||||
if match.group(5):
|
||||
alpha = float(match.group(5))
|
||||
r, g, b = rgba2rgb(r, g, b, alpha)
|
||||
return rgb_to_hex((r, g, b))
|
||||
|
||||
if 'hsl' in s:
|
||||
# hsl(hue in {0,360}, saturation [0, 100%], lightness [0, 100%])
|
||||
match = re.search(r'hsla*\(((\d+), *(\d+)%, *(\d+)%(, \d\.\d+)*)\)', s)
|
||||
match = re.search(r'hsla*\(((\d+), *(\d+)%, *(\d+)%, (\d\.\d+)*)\)', s)
|
||||
if match:
|
||||
h, s, l = int(match.group(2)), int(match.group(3)), int(match.group(4))
|
||||
h /= 360
|
||||
s /= 10
|
||||
l /= 100
|
||||
rgb = tuple(hls_to_rgb(h, s, l))
|
||||
if match.group(5):
|
||||
alpha = match.group(5)
|
||||
rgb = rgba2rgb(*rgb, alpha)
|
||||
return rgb_to_hex(rgb)
|
||||
|
||||
if s.lower() in css3_names_to_hex:
|
||||
|
||||
Reference in New Issue
Block a user