Skip to content

Commit 063396b

Browse files
author
Steve Canny
committed
dml: add ColorFormat.theme_color getter
1 parent 426cad4 commit 063396b

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

docx/dml/color.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,26 @@ def rgb(self, value):
5959
if value is not None:
6060
rPr.get_or_add_color().val = value
6161

62+
@property
63+
def theme_color(self):
64+
"""
65+
A member of :ref:`MsoThemeColorIndex` or |None| if no theme color is
66+
specified. When :attr:`type` is `MSO_COLOR_TYPE.THEME`, the value of
67+
this property will always be a member of :ref:`MsoThemeColorIndex`.
68+
When :attr:`type` has any other value, the value of this property is
69+
|None|.
70+
71+
Assigning a member of :ref:`MsoThemeColorIndex` causes :attr:`type`
72+
to become `MSO_COLOR_TYPE.THEME`. Any existing RGB value is retained
73+
but ignored by Word. Assigning |None| causes any color specification
74+
to be removed such that the effective color is inherited from the
75+
style hierarchy.
76+
"""
77+
color = self._color
78+
if color is None or color.themeColor is None:
79+
return None
80+
return color.themeColor
81+
6282
@property
6383
def type(self):
6484
"""

features/txt-font-color.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ Feature: Get and set font color
4444
| a theme | None | None | None |
4545

4646

47-
@wip
4847
Scenario Outline: Get font theme color
4948
Given a font having <type> color
5049
Then font.color.theme_color is <value>

tests/dml/test_color.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
absolute_import, division, print_function, unicode_literals
99
)
1010

11-
from docx.enum.dml import MSO_COLOR_TYPE
11+
from docx.enum.dml import MSO_COLOR_TYPE, MSO_THEME_COLOR
1212
from docx.dml.color import ColorFormat
1313
from docx.shared import RGBColor
1414

@@ -32,6 +32,10 @@ def it_can_change_its_RGB_value(self, rgb_set_fixture):
3232
color_format.rgb = new_value
3333
assert color_format._element.xml == expected_xml
3434

35+
def it_knows_its_theme_color(self, theme_color_get_fixture):
36+
color_format, expected_value = theme_color_get_fixture
37+
assert color_format.theme_color == expected_value
38+
3539
# fixtures ---------------------------------------------
3640

3741
@pytest.fixture(params=[
@@ -67,6 +71,22 @@ def rgb_set_fixture(self, request):
6771
expected_xml = xml(expected_cxml)
6872
return color_format, new_value, expected_xml
6973

74+
@pytest.fixture(params=[
75+
('w:r', None),
76+
('w:r/w:rPr', None),
77+
('w:r/w:rPr/w:color{w:val=auto}', None),
78+
('w:r/w:rPr/w:color{w:val=4224FF}', None),
79+
('w:r/w:rPr/w:color{w:themeColor=accent1}', 'ACCENT_1'),
80+
('w:r/w:rPr/w:color{w:val=F00BA9,w:themeColor=dark1}', 'DARK_1'),
81+
])
82+
def theme_color_get_fixture(self, request):
83+
r_cxml, value = request.param
84+
color_format = ColorFormat(element(r_cxml))
85+
expected_value = (
86+
None if value is None else getattr(MSO_THEME_COLOR, value)
87+
)
88+
return color_format, expected_value
89+
7090
@pytest.fixture(params=[
7191
('w:r', None),
7292
('w:r/w:rPr', None),

0 commit comments

Comments
 (0)