Skip to content

Commit cabb727

Browse files
author
Steve Canny
committed
dml: add ColorFormat.rgb setter
1 parent 1fd6077 commit cabb727

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

docx/dml/color.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ def rgb(self):
5050
return None
5151
return color.val
5252

53+
@rgb.setter
54+
def rgb(self, value):
55+
if value is None and self._color is None:
56+
return
57+
rPr = self._element.get_or_add_rPr()
58+
rPr._remove_color()
59+
if value is not None:
60+
rPr.get_or_add_color().val = value
61+
5362
@property
5463
def type(self):
5564
"""

features/txt-font-color.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ Feature: Get and set font color
2828
| a theme | 4f81bd |
2929

3030

31-
@wip
3231
Scenario Outline: Set font RGB color
3332
Given a font having <type> color
3433
When I assign <value> to font.color.rgb

tests/dml/test_color.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from docx.dml.color import ColorFormat
1313
from docx.shared import RGBColor
1414

15-
from ..unitutil.cxml import element
15+
from ..unitutil.cxml import element, xml
1616

1717
import pytest
1818

@@ -27,6 +27,11 @@ def it_knows_its_RGB_value(self, rgb_get_fixture):
2727
color_format, expected_value = rgb_get_fixture
2828
assert color_format.rgb == expected_value
2929

30+
def it_can_change_its_RGB_value(self, rgb_set_fixture):
31+
color_format, new_value, expected_xml = rgb_set_fixture
32+
color_format.rgb = new_value
33+
assert color_format._element.xml == expected_xml
34+
3035
# fixtures ---------------------------------------------
3136

3237
@pytest.fixture(params=[
@@ -43,6 +48,25 @@ def rgb_get_fixture(self, request):
4348
expected_value = None if rgb is None else RGBColor.from_string(rgb)
4449
return color_format, expected_value
4550

51+
@pytest.fixture(params=[
52+
('w:r', RGBColor(10, 20, 30), 'w:r/w:rPr/w:color{w:val=0A141E}'),
53+
('w:r/w:rPr', RGBColor(1, 2, 3), 'w:r/w:rPr/w:color{w:val=010203}'),
54+
('w:r/w:rPr/w:color{w:val=123abc}', RGBColor(42, 24, 99),
55+
'w:r/w:rPr/w:color{w:val=2A1863}'),
56+
('w:r/w:rPr/w:color{w:val=auto}', RGBColor(16, 17, 18),
57+
'w:r/w:rPr/w:color{w:val=101112}'),
58+
('w:r/w:rPr/w:color{w:val=234bcd,w:themeColor=dark1}',
59+
RGBColor(24, 42, 99), 'w:r/w:rPr/w:color{w:val=182A63}'),
60+
('w:r/w:rPr/w:color{w:val=234bcd,w:themeColor=dark1}',
61+
None, 'w:r/w:rPr'),
62+
('w:r', None, 'w:r'),
63+
])
64+
def rgb_set_fixture(self, request):
65+
r_cxml, new_value, expected_cxml = request.param
66+
color_format = ColorFormat(element(r_cxml))
67+
expected_xml = xml(expected_cxml)
68+
return color_format, new_value, expected_xml
69+
4670
@pytest.fixture(params=[
4771
('w:r', None),
4872
('w:r/w:rPr', None),

0 commit comments

Comments
 (0)