RGB processing

This commit is contained in:
Kiryl
2022-06-13 12:59:12 +03:00
parent 5caec46f3c
commit 180631d33b
2 changed files with 11 additions and 13 deletions

View File

@@ -5,7 +5,7 @@ from ebooklib import epub
from bs4 import BeautifulSoup
from itertools import takewhile
from src.util.color_reader import str2hex
from src.util.color_reader import str2hex, rgb2color_name
from src.livecarta_config import LiveCartaConfig
@@ -129,8 +129,7 @@ LIVECARTA_STYLE_ATTRS_MAPPING = {
'font-variant': lambda x: x,
'text-align': lambda x: x,
'font': lambda x: '',
'font-family': lambda x: LiveCartaConfig.FONT_CORRESPONDANCE_TABLE.get(x) or
LiveCartaConfig.FONT_CORRESPONDANCE_TABLE.get(x.capitalize()),
'font-family': lambda x: LiveCartaConfig.FONT_CORRESPONDANCE_TABLE.get(re.sub(r"^\s+|\s+$", "", x.title())),
'font-size': convert_tag_style_values,
'color': get_text_color,
'background-color': get_bg_color,
@@ -198,7 +197,7 @@ def update_css_styles_to_livecarta_convention(css_rule: cssutils.css.CSSStyleRul
css_rule.style[style_type.name] = ''
return
cleaned_value = style_type.value.replace('\"', '').split(', ')[-1]
cleaned_value = style_type.value.replace('\"', '')
constraints_on_value = LIVECARTA_STYLE_ATTRS.get(
style_type.name)
value_not_in_possible_values_list = cleaned_value not in LIVECARTA_STYLE_ATTRS[
@@ -215,7 +214,7 @@ def update_css_styles_to_livecarta_convention(css_rule: cssutils.css.CSSStyleRul
def build_css_file_content(css_content: str) -> str:
"""Build css content with livecarta convention"""
sheet = cssutils.parseString(css_content, validate=False)
sheet = cssutils.parseString(css_content.lower(), validate=False)
for css_rule in sheet:
if css_rule.type == css_rule.STYLE_RULE:

View File

@@ -103,11 +103,10 @@ 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)
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))
rgba = re.findall('([0-9] *\.?[0-9]+)', s)
r, g, b = int(rgba[0]), int(rgba[1]), int(rgba[2])
if len(rgba) == 4:
alpha = float(rgba[3])
r, g, b = rgba2rgb(r, g, b, alpha)
return rgb_to_hex((r, g, b))