diff --git a/api/advent22_api/core/advent_image.py b/api/advent22_api/core/advent_image.py index 8a071a0..4a14774 100644 --- a/api/advent22_api/core/advent_image.py +++ b/api/advent22_api/core/advent_image.py @@ -1,10 +1,13 @@ import colorsys from dataclasses import dataclass -from typing import Self +from typing import Self, TypeAlias, cast import numpy as np from PIL import Image, ImageDraw, ImageFont +_RGB: TypeAlias = tuple[int, int, int] +_XY: TypeAlias = tuple[float, float] + @dataclass(slots=True, frozen=True) class AdventImage: @@ -43,7 +46,7 @@ class AdventImage: async def get_text_box( self, - xy: tuple[float, float], + xy: _XY, text: str | bytes, font: "ImageFont._Font", anchor: str | None = "mm", @@ -81,13 +84,13 @@ class AdventImage: """ pixel_data = self.img.crop(box).getdata() - mean_color: np.ndarray = np.mean(a=pixel_data, axis=0) + mean_color: np.ndarray = np.mean(pixel_data, axis=0) - return tuple(mean_color.astype(int)[:3]) + return cast(_RGB, tuple(mean_color.astype(int))) async def hide_text( self, - xy: tuple[float, float], + xy: _XY, text: str | bytes, font: "ImageFont._Font", anchor: str | None = "mm", @@ -127,7 +130,7 @@ class AdventImage: xy=xy, text=text, font=font, - fill=text_color, + fill=cast(_RGB, text_color), anchor=anchor, **text_kwargs, )