Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion RGBMatrixEmulator/adapters/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ class BaseAdapter:
"RGBME v{} - {}x{} Matrix | {}x{} Chain | {}px per LED ({}) | {}"
)

ICON_FORMATS = ["PNG", "ICO", "JPEG"]
ICON_MAX_SIZE = (512, 512)

def __init__(self, width, height, options):
self.width = width
self.height = height
Expand All @@ -33,7 +36,7 @@ def __init__(self, width, height, options):
self.emulator_title = self.options.emulator_title or str(self)

self.default_icon_path = (Path(__file__).parent / ".." / "icon.png").resolve()
self.icon_path = self.options.icon_path or self.default_icon_path
self._set_icon_path()

@classmethod
def get_instance(cls, *args, **kwargs):
Expand Down Expand Up @@ -180,6 +183,39 @@ def _gradient_add(self, g1, g2):
"""
return np.clip((g1 - 128) + (g2 - 128), 0, 255)

def _set_icon_path(self):
self.icon_path = self.default_icon_path
custom_path = self.options.icon_path

if not custom_path:
return

try:
icon = Image.open(custom_path)

if icon.format not in self.ICON_FORMATS:
Logger.info(
f"Custom icon format '{icon.format}' is not in allowed formats ({self.ICON_FORMATS}). Using default icon instead."
)
return

if (
icon.width > self.ICON_MAX_SIZE[0]
or icon.height > self.ICON_MAX_SIZE[1]
):
Logger.info(
f"Icon of size '{icon.size}' is too large (max size is {self.ICON_MAX_SIZE}). Using default icon instead."
)
return
except Exception as e:
Logger.exception(
"Encountered exception while loading custom icon. Using default icon instead."
)
Logger.exception(e)
return

self.icon_path = self.options.icon_path

def __str__(self):
return self.DEBUG_TEXT_TEMPLATE.format(
version.__version__,
Expand Down
2 changes: 1 addition & 1 deletion RGBMatrixEmulator/adapters/browser_adapter/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def __init__(self, width, height, options):
self.default_icon_path = str(
(Path(__file__).parent / "static" / "assets" / "icon.ico").resolve()
)
self.icon_path = self.options.icon_path or self.default_icon_path
self._set_icon_path()

def load_emulator_window(self):
if self.loaded:
Expand Down