1212from docx .dml .color import ColorFormat
1313from docx .shared import RGBColor
1414
15- from ..unitutil .cxml import element
15+ from ..unitutil .cxml import element , xml
1616
1717import 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