Skip to content

Commit d2d51a1

Browse files
committed
refactor: add theme color handling with Literal support in color module
1 parent 22e5a25 commit d2d51a1

File tree

2 files changed

+71
-5
lines changed

2 files changed

+71
-5
lines changed

src/tppt/pptx/dml/color.py

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Self, cast
1+
from typing import Literal, Self, assert_never, cast
22

33
from lxml.etree import _Element
44
from pptx.dml.color import ColorFormat as PptxColorFormat
@@ -11,6 +11,72 @@
1111
from tppt.pptx.converter import PptxConvertible, to_pptx_rgb_color, to_tppt_rgb_color
1212
from tppt.types import Color, LiteralColor
1313

14+
LiteralThemeColor = Literal[
15+
"accent1",
16+
"accent2",
17+
"accent3",
18+
"accent4",
19+
"accent5",
20+
"accent6",
21+
"background1",
22+
"background2",
23+
"dark1",
24+
"dark2",
25+
"followed_hyperlink",
26+
"hyperlink",
27+
"light1",
28+
"light2",
29+
"text1",
30+
"text2",
31+
"mixed",
32+
]
33+
34+
35+
def to_pptx_theme_color(
36+
value: LiteralThemeColor | MSO_THEME_COLOR | None,
37+
) -> MSO_THEME_COLOR:
38+
match value:
39+
case MSO_THEME_COLOR():
40+
return value
41+
case None:
42+
return MSO_THEME_COLOR.NOT_THEME_COLOR
43+
case "accent1":
44+
return MSO_THEME_COLOR.ACCENT_1
45+
case "accent2":
46+
return MSO_THEME_COLOR.ACCENT_2
47+
case "accent3":
48+
return MSO_THEME_COLOR.ACCENT_3
49+
case "accent4":
50+
return MSO_THEME_COLOR.ACCENT_4
51+
case "accent5":
52+
return MSO_THEME_COLOR.ACCENT_5
53+
case "accent6":
54+
return MSO_THEME_COLOR.ACCENT_6
55+
case "background1":
56+
return MSO_THEME_COLOR.BACKGROUND_1
57+
case "background2":
58+
return MSO_THEME_COLOR.BACKGROUND_2
59+
case "dark1":
60+
return MSO_THEME_COLOR.DARK_1
61+
case "dark2":
62+
return MSO_THEME_COLOR.DARK_2
63+
case "followed_hyperlink":
64+
return MSO_THEME_COLOR.FOLLOWED_HYPERLINK
65+
case "hyperlink":
66+
return MSO_THEME_COLOR.HYPERLINK
67+
case "light1":
68+
return MSO_THEME_COLOR.LIGHT_1
69+
case "light2":
70+
return MSO_THEME_COLOR.LIGHT_2
71+
case "text1":
72+
return MSO_THEME_COLOR.TEXT_1
73+
case "text2":
74+
return MSO_THEME_COLOR.TEXT_2
75+
case "mixed":
76+
return MSO_THEME_COLOR.MIXED
77+
case _:
78+
assert_never(value)
79+
1480

1581
class ColorFormat(PptxConvertible[PptxColorFormat]):
1682
def __init__(self, pptx_obj: PptxColorFormat) -> None:
@@ -53,7 +119,7 @@ def rgb(self, color: Color | LiteralColor):
53119
srgbClr.remove(alpha)
54120

55121
@property
56-
def theme_color(self) -> MSO_THEME_COLOR:
122+
def theme_color(self) -> MSO_THEME_COLOR | None:
57123
"""Theme color value of this color.
58124
59125
Value is a member of :ref:`MsoThemeColorIndex`, e.g.
@@ -65,8 +131,8 @@ def theme_color(self) -> MSO_THEME_COLOR:
65131
return self._pptx.theme_color
66132

67133
@theme_color.setter
68-
def theme_color(self, value: MSO_THEME_COLOR) -> None:
69-
self._pptx.theme_color = value
134+
def theme_color(self, value: LiteralThemeColor | MSO_THEME_COLOR | None) -> None:
135+
self._pptx.theme_color = to_pptx_theme_color(value)
70136

71137
def to_pptx(self) -> PptxColorFormat:
72138
return self._pptx

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)