|
| 1 | +from tagstudio.core.enums import Theme |
| 2 | +from tagstudio.qt.views.styles.palette import ColorType, UiColor, get_ui_color |
| 3 | +from PIL import ImageDraw, ImageFont, UnidentifiedImageError |
| 4 | +from PIL.Image import DecompressionBombError, Image, Resampling |
| 5 | +from PIL.Image import new as new_image |
| 6 | +from PIL.Image import open as open_image |
| 7 | + |
| 8 | + |
| 9 | +# TODO: Split out Qt color palette stuff from anything needed by the core. |
| 10 | +def apply_overlay_color(image: Image, color: UiColor, theme: Theme) -> Image: |
| 11 | + """Apply a color overlay effect to an image based on its color channel data. |
| 12 | +
|
| 13 | + Red channel for foreground, green channel for outline, none for background. |
| 14 | +
|
| 15 | + Args: |
| 16 | + image (Image.Image): The image to apply an overlay to. |
| 17 | + color (UiColor): The name of the ColorType color to use. |
| 18 | + theme (Theme): A theme enum to determine the light/dark theme. |
| 19 | + """ |
| 20 | + bg_color: str = ( |
| 21 | + get_ui_color(ColorType.DARK_ACCENT, color) |
| 22 | + if theme == Theme.DARK |
| 23 | + else get_ui_color(ColorType.PRIMARY, color) |
| 24 | + ) |
| 25 | + fg_color: str = ( |
| 26 | + get_ui_color(ColorType.PRIMARY, color) |
| 27 | + if theme == Theme.DARK |
| 28 | + else get_ui_color(ColorType.LIGHT_ACCENT, color) |
| 29 | + ) |
| 30 | + ol_color: str = ( |
| 31 | + get_ui_color(ColorType.BORDER, color) |
| 32 | + if theme == Theme.DARK |
| 33 | + else get_ui_color(ColorType.LIGHT_ACCENT, color) |
| 34 | + ) |
| 35 | + |
| 36 | + bg: Image = new_image(image.mode, image.size, color=bg_color) |
| 37 | + fg: Image = new_image(image.mode, image.size, color=fg_color) |
| 38 | + ol: Image = new_image(image.mode, image.size, color=ol_color) |
| 39 | + |
| 40 | + bg.paste(fg, (0, 0), mask=image.getchannel(0)) |
| 41 | + bg.paste(ol, (0, 0), mask=image.getchannel(1)) |
| 42 | + |
| 43 | + if image.mode == "RGBA": |
| 44 | + alpha_bg: Image = bg.copy() |
| 45 | + alpha_bg.convert("RGBA") |
| 46 | + alpha_bg.putalpha(0) |
| 47 | + alpha_bg.paste(bg, (0, 0), mask=image.getchannel(3)) |
| 48 | + bg = alpha_bg |
| 49 | + |
| 50 | + return bg |
0 commit comments