epub converter: prettify color_reader.py

This commit is contained in:
shirshasa
2021-04-29 09:32:46 +03:00
parent 39bc2b92a9
commit e06bc58257

View File

@@ -13,11 +13,11 @@ def closest_colour_rgb(requested_colour):
return min_colours[min(min_colours.keys())] return min_colours[min(min_colours.keys())]
def get_rgb_colour_name(c): def get_rgb_color_name(color):
try: try:
closest_name = actual_name = rgb_to_name(c, 'html4') closest_name = actual_name = rgb_to_name(color, 'html4')
except ValueError: except ValueError:
closest_name = closest_colour_rgb(c) closest_name = closest_colour_rgb(color)
actual_name = None actual_name = None
if actual_name: if actual_name:
return actual_name return actual_name
@@ -25,16 +25,16 @@ def get_rgb_colour_name(c):
return closest_name return closest_name
def get_hex_colour_name(c): def get_hex_color_name(color):
try: try:
c = hex_to_rgb(c) color = hex_to_rgb(color)
except ValueError: except ValueError:
return '' return ''
try: try:
closest_name = actual_name = rgb_to_name(c, 'html4') closest_name = actual_name = rgb_to_name(color, 'html4')
except ValueError: except ValueError:
closest_name = closest_colour_rgb(c) closest_name = closest_colour_rgb(color)
actual_name = None actual_name = None
if actual_name: if actual_name:
return actual_name return actual_name
@@ -52,11 +52,11 @@ def str2color_name(s: str):
return '' return ''
if len(rgb) != 3: if len(rgb) != 3:
return '' return ''
name = get_rgb_colour_name(rgb) name = get_rgb_color_name(rgb)
return name return name
elif '#' in s: elif '#' in s:
name = get_hex_colour_name(s) name = get_hex_color_name(s)
return name return name
elif s in html4_hex_to_names.items(): elif s in html4_hex_to_names.items():
@@ -80,10 +80,10 @@ if __name__ == '__main__':
] ]
for c in colors: for c in colors:
name = get_rgb_colour_name(c) n = get_rgb_color_name(c)
print("Actual colour:", c, ", closest colour name:", name) print("Actual colour:", c, ", closest colour name:", n)
for c in hex_colors: for c in hex_colors:
name = get_hex_colour_name(c) n = get_hex_color_name(c)
print("Actual colour:", c, ", closest colour name:", name) print("Actual colour:", c, ", closest colour name:", n)
print() print()