def ColorScaleRule(start_type=None, start_value=None, start_color=None, mid_type=None, mid_value=None, mid_color=None, end_type=None, end_value=None, end_color=None): """Backwards compatibility""" formats = [] if start_type is not None: formats.append(FormatObject(type=start_type, val=start_value)) if mid_type is not None: formats.append(FormatObject(type=mid_type, val=mid_value)) if end_type is not None: formats.append(FormatObject(type=end_type, val=end_value)) colors = [] for v in (start_color, mid_color, end_color): if v is not None: if not isinstance(v, Color): v = Color(v) colors.append(v) cs = ColorScale(cfvo=formats, color=colors) rule = Rule(type="colorScale", colorScale=cs) return rule
def test_serialise(self, Border, Side): medium_blue = Side(border_style='medium', color=Color(colors.BLUE)) bd = Border( left=medium_blue, right=medium_blue, top=medium_blue, bottom=medium_blue, outline=False, diagonalDown=True, ) xml = tostring(bd.to_tree()) expected = """ <border diagonalDown="1" outline="0"> <left style="medium"> <color rgb="000000FF"></color> </left> <right style="medium"> <color rgb="000000FF"></color> </right> <top style="medium"> <color rgb="000000FF"></color> </top> <bottom style="medium"> <color rgb="000000FF"></color> </bottom> <diagonal /> </border> """ diff = compare_xml(xml, expected) assert diff is None, diff
def __init__(self, patternType=None, fgColor=Color(), bgColor=Color(), fill_type=None, start_color=None, end_color=None): if fill_type is not None: patternType = fill_type self.patternType = patternType if start_color is not None: fgColor = start_color self.fgColor = fgColor if end_color is not None: bgColor = end_color self.bgColor = bgColor
def test_create(self, GradientFill, Stop): src = """ <fill> <gradientFill xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" degree="90"> <stop position="0"> <color theme="0"/> </stop> <stop position="1"> <color theme="4"/> </stop> </gradientFill> </fill> """ xml = fromstring(src) fill = GradientFill.from_tree(xml) assert fill.stop == [ Stop(Color(theme=0), position=0), Stop(Color(theme=4), position=1) ]
def to_tree(self, tagname=None, idx=None): parent = Element("fill") el = Element(self.tagname) if self.patternType is not None: el.set('patternType', self.patternType) for c in self.__elements__: value = getattr(self, c) if value != Color(): el.append(value.to_tree(c)) parent.append(el) return parent
def test_cannot_add_other_types(self): c = Color() with pytest.raises(TypeError): c + 4
def test_adding(self): c1 = Color(rgb="FF0000") c2 = Color(rgb="00FF00") c3 = c1 + c2 assert c3.rgb == "00FF0000"
def test_validation(self): c = Color() with pytest.raises(TypeError): c.value = 4
def test_highlander(self): c = Color(rgb="FFFFFFF", indexed=4, theme=2, auto=False) assert c.value == 4 assert c.type == "indexed"
def test_tint(self): c = Color(tint=0.5) assert c.tint == 0.5 assert dict(c) == {'rgb': '00000000', 'tint': "0.5"}
def test_ctor(self, PatternFill): pf = PatternFill() assert pf.patternType is None assert pf.fgColor == Color() assert pf.bgColor == Color()
def test_indexed(self): c = Color(indexed=4) assert c.value == 4 assert c.type == "indexed" assert dict(c) == {'indexed': "4"}
def test_rgb(self): c = Color(rgb="FFFFFFFF") assert c.value == "FFFFFFFF" assert c.type == "rgb" assert dict(c) == {'rgb': 'FFFFFFFF'}
def test_ctor(self): c = Color() assert c.value == "00000000" assert c.type == "rgb" assert dict(c) == {'rgb': '00000000'}
self.i = i if underline is not None: u = underline self.u = u if strikethrough is not None: strike = strikethrough self.strike = strike self.color = color self.vertAlign = vertAlign self.charset = charset self.outline = outline self.shadow = shadow self.condense = condense self.extend = extend self.scheme = scheme @classmethod def from_tree(cls, node): """ Set default value for underline if child element is present """ underline = node.find("{%s}u" % SHEET_MAIN_NS) if underline is not None and underline.get('val') is None: underline.set("val", "single") return super(Font, cls).from_tree(node) DEFAULT_FONT = Font(name="Calibri", sz=11, family=2, b=False, i=False, color=Color(theme=1), scheme="minor")
def test_auto(self): c = Color(auto=1) assert c.type is "auto" assert c.value is True assert dict(c) == {'auto': "1"}
class TestPatternFill: def test_ctor(self, PatternFill): pf = PatternFill() assert pf.patternType is None assert pf.fgColor == Color() assert pf.bgColor == Color() def test_dict_interface(self, PatternFill): pf = PatternFill(fill_type='solid') assert dict(pf) == {'patternType': 'solid'} def test_serialise(self, PatternFill): pf = PatternFill('solid', 'FF0000', 'FFFF00') xml = tostring(pf.to_tree()) expected = """ <fill> <patternFill patternType="solid"> <fgColor rgb="00FF0000"/> <bgColor rgb="00FFFF00"/> </patternFill> </fill> """ diff = compare_xml(xml, expected) assert diff is None, diff @pytest.mark.parametrize("src, args", [ (""" <fill> <patternFill xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" patternType="solid"> <fgColor theme="0" tint="-0.14999847407452621"/> <bgColor indexed="64"/> </patternFill> </fill> """, dict(patternType='solid', start_color=Color(theme=0, tint=-0.14999847407452621), end_color=Color(indexed=64))), (""" <fill> <patternFill xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" patternType="solid"> <fgColor theme="0"/> <bgColor indexed="64"/> </patternFill> </fill> """, dict(patternType='solid', start_color=Color(theme=0), end_color=Color(indexed=64))), (""" <fill> <patternFill xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" patternType="solid"> <fgColor indexed="62"/> <bgColor indexed="64"/> </patternFill> </fill> """, dict(patternType='solid', start_color=Color(indexed=62), end_color=Color(indexed=64))), ]) def test_create(self, PatternFill, src, args): xml = fromstring(src) assert PatternFill.from_tree(xml) == PatternFill(**args)
def test_theme(self): c = Color(theme="1") assert c.value == 1 assert c.type == "theme" assert dict(c) == {'theme': "1"}
class TestGradientFill: def test_empty_ctor(self, GradientFill): gf = GradientFill() assert gf.type == 'linear' assert gf.degree == 0 assert gf.left == 0 assert gf.right == 0 assert gf.top == 0 assert gf.bottom == 0 assert gf.stop == [] def test_ctor(self, GradientFill): gf = GradientFill(degree=90, left=1, right=2, top=3, bottom=4) assert gf.degree == 90 assert gf.left == 1 assert gf.right == 2 assert gf.top == 3 assert gf.bottom == 4 @pytest.mark.parametrize("colors", [ [Color(BLACK), Color(WHITE)], [BLACK, WHITE], ]) def test_stop_sequence(self, GradientFill, Stop, colors): gf = GradientFill(stop=[Stop(colors[0], 0), Stop(colors[1], .5)]) assert gf.stop[0].color.rgb == BLACK assert gf.stop[1].color.rgb == WHITE assert gf.stop[0].position == 0 assert gf.stop[1].position == .5 @pytest.mark.parametrize("colors,rgbs,positions", [ ([Color(BLACK), Color(WHITE)], [BLACK, WHITE], [0, 1]), ([BLACK, WHITE], [BLACK, WHITE], [0, 1]), ([BLACK, WHITE, BLACK], [BLACK, WHITE, BLACK], [0, .5, 1]), ([WHITE], [WHITE], [0]), ]) def test_color_sequence(self, Stop, colors, rgbs, positions): from ..fills import _assign_position stops = _assign_position(colors) assert [stop.color.rgb for stop in stops] == rgbs assert [stop.position for stop in stops] == positions def test_invalid_stop_color_mix(self, Stop): from ..fills import _assign_position with pytest.raises(ValueError): _assign_position([Stop(BLACK, .1), WHITE]) def test_duplicate_position(self, Stop): from ..fills import _assign_position with pytest.raises(ValueError): _assign_position([Stop(BLACK, 0.5), Stop(BLACK, 0.5)]) def test_dict_interface(self, GradientFill): gf = GradientFill(degree=90, left=1, right=2, top=3, bottom=4) assert dict(gf) == { 'bottom': "4", 'degree': "90", 'left': "1", 'right': "2", 'top': "3", 'type': 'linear' } def test_serialise(self, GradientFill, Stop): gf = GradientFill(degree=90, left=1, right=2, top=3, bottom=4, stop=[Stop(BLACK, 0), Stop(WHITE, 1)]) xml = tostring(gf.to_tree()) expected = """ <fill> <gradientFill bottom="4" degree="90" left="1" right="2" top="3" type="linear"> <stop position="0"> <color rgb="00000000"></color> </stop> <stop position="1"> <color rgb="00FFFFFF"></color> </stop> </gradientFill> </fill> """ diff = compare_xml(xml, expected) assert diff is None, diff def test_create(self, GradientFill, Stop): src = """ <fill> <gradientFill xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" degree="90"> <stop position="0"> <color theme="0"/> </stop> <stop position="1"> <color theme="4"/> </stop> </gradientFill> </fill> """ xml = fromstring(src) fill = GradientFill.from_tree(xml) assert fill.stop == [ Stop(Color(theme=0), position=0), Stop(Color(theme=4), position=1) ]
def _from_tree(cls, el): attrib = dict(el.attrib) for child in el: desc = localname(child) attrib[desc] = Color.from_tree(child) return cls(**attrib)