Skip to content

Commit

Permalink
PB-302: Restructure calculation of default icon size
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasJoss committed Aug 29, 2024
1 parent 2b92c95 commit 263cfba
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 21 deletions.
20 changes: 14 additions & 6 deletions app/helpers/icons.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
from app.settings import DEFAULT_ICON_SIZE


def get_icon_set_template_url(base_url=''):
"""
Generate and return a template URL to access icon_sets' metadata
Expand All @@ -19,14 +16,25 @@ def get_icon_template_url(base_url='', with_color=True):
f"@{{icon_scale}}{color_part}.png"


def calculate_icon_size(size, scale=1):
def calculate_icon_size(size, icon_set, scale=1):
"""
Calculate icon size so that the size of the smaller dimension is equal to the standard icon size
multiplied by the scaling
"""
width, height = size
min_size = min(width, height)
return (
int(scale * DEFAULT_ICON_SIZE * width / min_size),
int(scale * DEFAULT_ICON_SIZE * height / min_size)
int(scale * get_default_pixel_size(icon_set.name) * width / min_size),
int(scale * get_default_pixel_size(icon_set.name) * height / min_size)
)


def get_default_pixel_size(icon_set):
"""
Returns:
the size in pixel of this icon set's icon (icons are always square images). This is
helpful to calculate if an icon requires a resize before being served to the user.
"""
if icon_set == 'default':
return 96
return 48
2 changes: 1 addition & 1 deletion app/icon.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def get_size(self):
A tuple with the size of the specified icon (x,y)
"""
with Image.open(self.get_icon_filepath()) as img:
return calculate_icon_size(img.size)
return calculate_icon_size(img.size, self.icon_set)

def serialize(self):
"""
Expand Down
10 changes: 0 additions & 10 deletions app/icon_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,6 @@ def get_icon(self, icon_name):
"""
return Icon(f"{icon_name}", self)

def get_default_pixel_size(self):
"""
Returns:
the size in pixel of this icon set's icon (icons are always square images). This is
helpful to calculate if an icon requires a resize before being served to the user.
"""
if self.name == 'default':
return 96
return 48

def get_all_icons(self):
"""
Generate a list of all icons belonging to this icon set.
Expand Down
5 changes: 3 additions & 2 deletions app/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from app.helpers.check_functions import get_and_check_icon
from app.helpers.check_functions import get_and_check_icon_set
from app.helpers.icons import calculate_icon_size
from app.helpers.icons import get_default_pixel_size
from app.icon import Icon
from app.icon_set import IconSet
from app.icon_set import get_all_icon_sets
Expand Down Expand Up @@ -88,8 +89,8 @@ def colorized_icon(
if image.mode == 'P':
image = image.convert('RGBA')

new_size = calculate_icon_size(image.size, scale)
if new_size[0] != new_size[1] or new_size[0] != icon_set.get_default_pixel_size():
new_size = calculate_icon_size(image.size, icon_set, scale)
if min(new_size[0], new_size[1]) != get_default_pixel_size(icon_set.name):
image = image.resize((new_size[0], new_size[1]))
if icon_set.colorable:
image = Image.composite(Image.new("RGB", image.size, (red, green, blue)), image, image)
Expand Down
1 change: 0 additions & 1 deletion app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

COLORABLE_ICON_SETS = ['default']
DEFAULT_COLOR = {"r": '255', "g": '0', "b": '0'}
DEFAULT_ICON_SIZE = 48
TRAP_HTTP_EXCEPTIONS = True
LOGS_DIR = os.getenv('LOGS_DIR', str(BASE_DIR / 'logs'))
os.environ['LOGS_DIR'] = LOGS_DIR # Set default if not set
Expand Down
3 changes: 2 additions & 1 deletion tests/unit_tests/test_all_icons.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from flask import url_for

from app.helpers.icons import get_default_pixel_size
from app.helpers.icons import get_icon_template_url
from app.helpers.url import get_base_url
from app.icon_set import get_icon_set
Expand Down Expand Up @@ -267,7 +268,7 @@ def test_all_icon_metadata_endpoint(self):
self.assertTrue(elem >= 48, msg='"size" should be >= 48')
self.assertEqual(
min(json_response['size'][0], json_response['size'][1]),
48,
get_default_pixel_size(icon_set_name),
msg='size of smaller dimension of icon should be equal to 48'
)

Expand Down

0 comments on commit 263cfba

Please sign in to comment.