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 30, 2024
1 parent 65a733c commit d928cee
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 15 deletions.
2 changes: 1 addition & 1 deletion app/helpers/icons.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ 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
Expand Down
2 changes: 1 addition & 1 deletion app/icon.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,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 @@ -100,16 +100,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 @@ -18,6 +18,7 @@
from app.icon_set import IconSet
from app.icon_set import get_all_icon_sets
from app.settings import DEFAULT_COLOR
from app.settings import DEFAULT_ICON_SIZE
from app.version import APP_VERSION

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -96,8 +97,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]) != DEFAULT_ICON_SIZE:
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
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 @@ -11,6 +11,7 @@
from app.helpers.url import get_base_url
from app.icon_set import get_icon_set
from app.settings import COLORABLE_ICON_SETS
from app.settings import DEFAULT_ICON_SIZE
from app.settings import IMAGE_FOLDER
from tests.unit_tests.base_test import ServiceIconsUnitTests

Expand Down Expand Up @@ -278,7 +279,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,
DEFAULT_ICON_SIZE,
msg='size of smaller dimension of icon should be equal to 48'
)

Expand Down

0 comments on commit d928cee

Please sign in to comment.