forked from LiveCarta/BookConverter
Update utils to current project
This commit is contained in:
@@ -9,7 +9,7 @@ def parse_args():
|
|||||||
return args
|
return args
|
||||||
|
|
||||||
|
|
||||||
def check_dir(dir_path):
|
def check_dir(dir_path: str):
|
||||||
if not os.path.exists(dir_path):
|
if not os.path.exists(dir_path):
|
||||||
try:
|
try:
|
||||||
os.mkdir(dir_path)
|
os.mkdir(dir_path)
|
||||||
@@ -20,15 +20,13 @@ def check_dir(dir_path):
|
|||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
folders = parse_args().folders
|
folders = parse_args().folders
|
||||||
if not folders:
|
if not folders:
|
||||||
folders = ["docx", "html", "json", "logs", "config"]
|
folders = ["books/epub", "books/docx", "books/html", "books/json", "logs", "config"]
|
||||||
|
|
||||||
folder_path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
folder_path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||||
folders = [os.path.join(folder_path, folder) for folder in folders]
|
folders = [os.path.join(folder_path, folder) for folder in folders]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
for folder in folders:
|
[check_dir(folder) for folder in folders]
|
||||||
check_dir(folder)
|
|
||||||
|
|
||||||
except OSError as exc:
|
except OSError as exc:
|
||||||
print(exc)
|
print(exc)
|
||||||
raise
|
raise
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ from colorsys import hls_to_rgb
|
|||||||
from webcolors import html4_hex_to_names, hex_to_rgb, rgb_to_name, rgb_percent_to_hex, rgb_to_hex, css3_names_to_hex
|
from webcolors import html4_hex_to_names, hex_to_rgb, rgb_to_name, rgb_percent_to_hex, rgb_to_hex, css3_names_to_hex
|
||||||
|
|
||||||
|
|
||||||
def closest_colour_rgb(requested_color):
|
def closest_colour_rgb(requested_color: Tuple[int, ...]) -> str:
|
||||||
""" Function finds closes colour rgb """
|
""" Function finds closes colour rgb """
|
||||||
min_colours = {}
|
min_colours = {}
|
||||||
for key, name in html4_hex_to_names.items():
|
for key, name in html4_hex_to_names.items():
|
||||||
@@ -17,10 +17,10 @@ def closest_colour_rgb(requested_color):
|
|||||||
return min_colours[min(min_colours.keys())]
|
return min_colours[min(min_colours.keys())]
|
||||||
|
|
||||||
|
|
||||||
def rgb2color_name(color):
|
def rgb2color_name(color: Tuple[int, ...]) -> str:
|
||||||
""" Transform rgb -> color name """
|
""" Transform rgb -> color name """
|
||||||
try:
|
try:
|
||||||
closest_name = actual_name = rgb_to_name(color, 'html4')
|
closest_name = actual_name = rgb_to_name(color, "html4")
|
||||||
except ValueError:
|
except ValueError:
|
||||||
closest_name = closest_colour_rgb(color)
|
closest_name = closest_colour_rgb(color)
|
||||||
actual_name = None
|
actual_name = None
|
||||||
@@ -30,15 +30,15 @@ def rgb2color_name(color):
|
|||||||
return closest_name
|
return closest_name
|
||||||
|
|
||||||
|
|
||||||
def hex2color_name(color):
|
def hex2color_name(color: str) -> str:
|
||||||
""" Transform hex -> color name """
|
""" Transform hex -> color name """
|
||||||
try:
|
try:
|
||||||
color = hex_to_rgb(color)
|
color = hex_to_rgb(color)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
return ''
|
return ""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
closest_name = actual_name = rgb_to_name(color, 'html4')
|
closest_name = actual_name = rgb_to_name(color, "html4")
|
||||||
except ValueError:
|
except ValueError:
|
||||||
closest_name = closest_colour_rgb(color)
|
closest_name = closest_colour_rgb(color)
|
||||||
actual_name = None
|
actual_name = None
|
||||||
@@ -50,36 +50,36 @@ def hex2color_name(color):
|
|||||||
|
|
||||||
def str2closest_html_color_name(s: str) -> str:
|
def str2closest_html_color_name(s: str) -> str:
|
||||||
""" Transform str -> closest color name """
|
""" Transform str -> closest color name """
|
||||||
if 'rgb' in s:
|
if "rgb" in s:
|
||||||
rgb_str = 'rgba' if ('rgba' in s) else 'rgb'
|
rgb_str = "rgba" if ("rgba" in s) else "rgb"
|
||||||
s = s.replace(rgb_str, '').replace('(', '').replace(')', '')
|
s = s.replace(rgb_str, "").replace("(", "").replace(")", "")
|
||||||
try:
|
try:
|
||||||
rgb = [int(x) for x in s.split(',')[:3]]
|
rgb = [int(x) for x in s.split(",")[:3]]
|
||||||
rgb = tuple(rgb)
|
rgb = tuple(rgb)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
return ''
|
return ""
|
||||||
if len(rgb) != 3:
|
if len(rgb) != 3:
|
||||||
return ''
|
return ""
|
||||||
name = rgb2color_name(rgb)
|
name = rgb2color_name(rgb)
|
||||||
return name
|
return name
|
||||||
|
|
||||||
elif '#' in s:
|
elif "#" in s:
|
||||||
if s in ['#996A95', '#D5C9D3', '#E9E2E8', '#70416F']:
|
if s in ["#996A95", "#D5C9D3", "#E9E2E8", "#70416F"]:
|
||||||
return 'purple'
|
return "purple"
|
||||||
if s in ['#FFD472', '#F47B4D', '#FFFBEF', '#F47B4D']:
|
if s in ["#FFD472", "#F47B4D", "#FFFBEF", "#F47B4D"]:
|
||||||
return 'olive'
|
return "olive"
|
||||||
if s in ['#B0DFD7', '#EFF8F6', '#5CC4B7']:
|
if s in ["#B0DFD7", "#EFF8F6", "#5CC4B7"]:
|
||||||
return 'teal'
|
return "teal"
|
||||||
name = hex2color_name(s)
|
name = hex2color_name(s)
|
||||||
if (name == 'white') and (s.lower() not in ['#ffffff', '#fff']):
|
if (name == "white") and (s.lower() not in ["#ffffff", "#fff"]):
|
||||||
name = 'gray'
|
name = "gray"
|
||||||
|
|
||||||
return name
|
return name
|
||||||
|
|
||||||
elif s in html4_hex_to_names.items():
|
elif s in html4_hex_to_names.items():
|
||||||
return s
|
return s
|
||||||
else:
|
else:
|
||||||
return ''
|
return ""
|
||||||
|
|
||||||
|
|
||||||
def rgba2rgb(r: int, g: int, b: int, alpha: float) -> Tuple[int, int, int]:
|
def rgba2rgb(r: int, g: int, b: int, alpha: float) -> Tuple[int, int, int]:
|
||||||
@@ -93,26 +93,26 @@ def rgba2rgb(r: int, g: int, b: int, alpha: float) -> Tuple[int, int, int]:
|
|||||||
|
|
||||||
def str2hex(s: str) -> str:
|
def str2hex(s: str) -> str:
|
||||||
""" Transform str -> hex """
|
""" Transform str -> hex """
|
||||||
if '#' in s and (len(s) <= 7):
|
if "#" in s and (len(s) <= 7):
|
||||||
return s.lower()
|
return s.lower()
|
||||||
|
|
||||||
if ('rgb' in s.lower()) and ('%' in s):
|
if ("rgb" in s.lower()) and ("%" 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:
|
if match:
|
||||||
r, g, b = int(match.group(2)), int(match.group(3)), int(match.group(4))
|
r, g, b = int(match.group(2)), int(match.group(3)), int(match.group(4))
|
||||||
return rgb_percent_to_hex((r, g, b))
|
return rgb_percent_to_hex((r, g, b))
|
||||||
|
|
||||||
if 'rgb' in s.lower():
|
if "rgb" in s.lower():
|
||||||
rgba = re.findall('([0-9] *\.?[0-9]+)', s)
|
rgba = re.findall("([0-9] *\.?[0-9]+)", s)
|
||||||
r, g, b = int(rgba[0]), int(rgba[1]), int(rgba[2])
|
r, g, b = int(rgba[0]), int(rgba[1]), int(rgba[2])
|
||||||
if len(rgba) == 4:
|
if len(rgba) == 4:
|
||||||
alpha = float(rgba[3])
|
alpha = float(rgba[3])
|
||||||
r, g, b = rgba2rgb(r, g, b, alpha)
|
r, g, b = rgba2rgb(r, g, b, alpha)
|
||||||
return rgb_to_hex((r, g, b))
|
return rgb_to_hex((r, g, b))
|
||||||
|
|
||||||
if 'hsl' in s.lower():
|
if "hsl" in s.lower():
|
||||||
# hsl(hue in {0,360}, saturation [0, 100%], lightness [0, 100%])
|
# 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:
|
if match:
|
||||||
h, s, l = int(match.group(2)), int(match.group(3)), int(match.group(4))
|
h, s, l = int(match.group(2)), int(match.group(3)), int(match.group(4))
|
||||||
h /= 360
|
h /= 360
|
||||||
@@ -127,10 +127,10 @@ def str2hex(s: str) -> str:
|
|||||||
if s.lower() in css3_names_to_hex:
|
if s.lower() in css3_names_to_hex:
|
||||||
return css3_names_to_hex[s.lower()]
|
return css3_names_to_hex[s.lower()]
|
||||||
|
|
||||||
return ''
|
return ""
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
colors = [
|
colors = [
|
||||||
(75, 0, 130), (255, 0, 255),
|
(75, 0, 130), (255, 0, 255),
|
||||||
(139, 69, 19), (46, 139, 87),
|
(139, 69, 19), (46, 139, 87),
|
||||||
@@ -138,7 +138,7 @@ if __name__ == '__main__':
|
|||||||
]
|
]
|
||||||
|
|
||||||
hex_colors = [
|
hex_colors = [
|
||||||
'#96F', '#000', '#4C4C4C', '#A00', '#99F'
|
"#96F", "#000", "#4C4C4C", "#A00", "#99F"
|
||||||
]
|
]
|
||||||
|
|
||||||
for c in colors:
|
for c in colors:
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ html4_hex_to_names = {'#00ffff': 'aqua', '#000000': 'black', '#0000ff': 'blue',
|
|||||||
'#ffffff': 'white', '#ffff00': 'yellow'}
|
'#ffffff': 'white', '#ffff00': 'yellow'}
|
||||||
|
|
||||||
|
|
||||||
def rgb2hsv(r, g, b):
|
def rgb2hsv(r: int, g: int, b: int):
|
||||||
r /= 255
|
r /= 255
|
||||||
g /= 255
|
g /= 255
|
||||||
b /= 255
|
b /= 255
|
||||||
|
|||||||
Reference in New Issue
Block a user