2023-12-21 14:13:31 +03:00
|
|
|
from __future__ import annotations
|
2024-01-20 14:23:03 +03:00
|
|
|
|
2022-02-17 02:04:43 +03:00
|
|
|
from PIL import Image
|
|
|
|
|
2019-11-04 01:18:55 +03:00
|
|
|
from .helper import hopper
|
2012-10-16 00:26:38 +04:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_palette() -> None:
|
2024-02-12 01:28:53 +03:00
|
|
|
def palette(mode: str) -> list[int] | None:
|
2019-11-04 01:18:55 +03:00
|
|
|
p = hopper(mode).getpalette()
|
|
|
|
if p:
|
|
|
|
return p[:10]
|
|
|
|
return None
|
2019-06-13 18:54:24 +03:00
|
|
|
|
2019-11-04 01:18:55 +03:00
|
|
|
assert palette("1") is None
|
|
|
|
assert palette("L") is None
|
|
|
|
assert palette("I") is None
|
|
|
|
assert palette("F") is None
|
|
|
|
assert palette("P") == [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
|
|
|
assert palette("RGB") is None
|
|
|
|
assert palette("RGBA") is None
|
|
|
|
assert palette("CMYK") is None
|
|
|
|
assert palette("YCbCr") is None
|
2022-02-17 02:04:43 +03:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_palette_rawmode() -> None:
|
2022-02-17 02:04:43 +03:00
|
|
|
im = Image.new("P", (1, 1))
|
|
|
|
im.putpalette((1, 2, 3))
|
|
|
|
|
2022-02-17 02:35:13 +03:00
|
|
|
for rawmode in ("RGB", None):
|
|
|
|
rgb = im.getpalette(rawmode)
|
2022-02-20 15:54:12 +03:00
|
|
|
assert rgb == [1, 2, 3]
|
2022-02-17 02:04:43 +03:00
|
|
|
|
|
|
|
# Convert the RGB palette to RGBA
|
|
|
|
rgba = im.getpalette("RGBA")
|
2022-02-20 15:54:12 +03:00
|
|
|
assert rgba == [1, 2, 3, 255]
|
2022-02-17 02:04:43 +03:00
|
|
|
|
|
|
|
im.putpalette((1, 2, 3, 4), "RGBA")
|
|
|
|
|
|
|
|
# Convert the RGBA palette to RGB
|
|
|
|
rgb = im.getpalette("RGB")
|
2022-02-20 15:54:12 +03:00
|
|
|
assert rgb == [1, 2, 3]
|
2022-02-17 02:04:43 +03:00
|
|
|
|
2022-02-17 02:35:13 +03:00
|
|
|
for rawmode in ("RGBA", None):
|
|
|
|
rgba = im.getpalette(rawmode)
|
2022-02-20 15:54:12 +03:00
|
|
|
assert rgba == [1, 2, 3, 4]
|