|
| 1 | +from typing import Self, cast |
| 2 | + |
| 3 | +from pptx.dml.fill import FillFormat as PptxFillFormat |
| 4 | +from pptx.dml.fill import _GradFill as PptxGradFill |
| 5 | +from pptx.dml.fill import _GradientStop as PptxGradientStop |
| 6 | +from pptx.dml.fill import _GradientStops as PptxGradientStops |
| 7 | +from pptx.dml.fill import _NoFill as PptxNoFill |
| 8 | +from pptx.dml.fill import _PattFill as PptxPattFill |
| 9 | +from pptx.dml.fill import _SolidFill as PptxSolidFill |
| 10 | +from pptx.enum.dml import MSO_PATTERN_TYPE |
| 11 | + |
| 12 | +from tppt.pptx.converter import PptxConvertible, to_pptx_angle, to_tppt_angle |
| 13 | +from tppt.pptx.dml.color import ColorFormat |
| 14 | +from tppt.types._angle import Angle |
| 15 | + |
| 16 | + |
| 17 | +class FillFormat(PptxConvertible[PptxFillFormat]): |
| 18 | + """Fill format.""" |
| 19 | + |
| 20 | + def __init__(self, pptx_obj: PptxFillFormat) -> None: |
| 21 | + self._pptx = pptx_obj |
| 22 | + |
| 23 | + @property |
| 24 | + def fore_color(self) -> ColorFormat: |
| 25 | + """Fore color.""" |
| 26 | + return ColorFormat(self._pptx.fore_color) |
| 27 | + |
| 28 | + @property |
| 29 | + def back_color(self) -> ColorFormat: |
| 30 | + """Back color.""" |
| 31 | + return ColorFormat(self._pptx.back_color) |
| 32 | + |
| 33 | + @property |
| 34 | + def pattern(self) -> MSO_PATTERN_TYPE: |
| 35 | + """Pattern.""" |
| 36 | + return self._pptx.pattern |
| 37 | + |
| 38 | + @pattern.setter |
| 39 | + def pattern(self, value: MSO_PATTERN_TYPE) -> None: |
| 40 | + self._pptx.pattern = value |
| 41 | + |
| 42 | + def patterned(self) -> "PattFill": |
| 43 | + """Set the fill type to patterned.""" |
| 44 | + self._pptx.patterned() |
| 45 | + return PattFill(cast(PptxPattFill, self._pptx._fill)) |
| 46 | + |
| 47 | + def background(self) -> "NoFill": |
| 48 | + """Set the fill type to noFill, i.e. transparent.""" |
| 49 | + self._pptx.background() |
| 50 | + return NoFill(cast(PptxNoFill, self._pptx._fill)) |
| 51 | + |
| 52 | + def solid(self) -> "SolidFill": |
| 53 | + """Set the fill type to solid.""" |
| 54 | + self._pptx.solid() |
| 55 | + return SolidFill(cast(PptxSolidFill, self._pptx._fill)) |
| 56 | + |
| 57 | + def gradient(self) -> "GradFill": |
| 58 | + """Sets the fill type to gradient.""" |
| 59 | + self._pptx.gradient() |
| 60 | + return GradFill(cast(PptxGradFill, self._pptx._fill)) |
| 61 | + |
| 62 | + def to_pptx(self) -> PptxFillFormat: |
| 63 | + """Convert to pptx fill format.""" |
| 64 | + return self._pptx |
| 65 | + |
| 66 | + @classmethod |
| 67 | + def from_pptx(cls, pptx_obj: PptxFillFormat) -> Self: |
| 68 | + """Create from pptx fill format.""" |
| 69 | + return cls(pptx_obj) |
| 70 | + |
| 71 | + |
| 72 | +class NoFill(PptxConvertible[PptxNoFill]): |
| 73 | + """No fill.""" |
| 74 | + |
| 75 | + def __init__(self, pptx_obj: PptxNoFill) -> None: |
| 76 | + self._pptx = pptx_obj |
| 77 | + |
| 78 | + def to_pptx(self) -> PptxNoFill: |
| 79 | + """Convert to pptx no fill.""" |
| 80 | + return self._pptx |
| 81 | + |
| 82 | + @classmethod |
| 83 | + def from_pptx(cls, pptx_obj: PptxNoFill) -> Self: |
| 84 | + """Create from pptx no fill.""" |
| 85 | + return cls(pptx_obj) |
| 86 | + |
| 87 | + |
| 88 | +class PattFill(PptxConvertible[PptxPattFill]): |
| 89 | + """Pattern fill.""" |
| 90 | + |
| 91 | + def __init__(self, pptx_obj: PptxPattFill) -> None: |
| 92 | + self._pptx = pptx_obj |
| 93 | + |
| 94 | + def to_pptx(self) -> PptxPattFill: |
| 95 | + """Convert to pptx pattern fill.""" |
| 96 | + return self._pptx |
| 97 | + |
| 98 | + @classmethod |
| 99 | + def from_pptx(cls, pptx_obj: PptxPattFill) -> Self: |
| 100 | + """Create from pptx pattern fill.""" |
| 101 | + return cls(pptx_obj) |
| 102 | + |
| 103 | + |
| 104 | +class SolidFill(PptxConvertible[PptxSolidFill]): |
| 105 | + """Solid fill.""" |
| 106 | + |
| 107 | + def __init__(self, pptx_obj: PptxSolidFill) -> None: |
| 108 | + self._pptx = pptx_obj |
| 109 | + |
| 110 | + def to_pptx(self) -> PptxSolidFill: |
| 111 | + """Convert to pptx solid fill.""" |
| 112 | + return self._pptx |
| 113 | + |
| 114 | + @classmethod |
| 115 | + def from_pptx(cls, pptx_obj: PptxSolidFill) -> Self: |
| 116 | + """Create from pptx solid fill.""" |
| 117 | + return cls(pptx_obj) |
| 118 | + |
| 119 | + |
| 120 | +class GradFill(PptxConvertible[PptxGradFill]): |
| 121 | + """Gradient fill.""" |
| 122 | + |
| 123 | + def __init__(self, pptx_obj: PptxGradFill) -> None: |
| 124 | + self._pptx = pptx_obj |
| 125 | + |
| 126 | + @property |
| 127 | + def gradient_angle(self) -> Angle | None: |
| 128 | + """Angle in float degrees of line of a linear gradient.""" |
| 129 | + return to_tppt_angle(cast(float | None, self._pptx.gradient_angle)) |
| 130 | + |
| 131 | + @gradient_angle.setter |
| 132 | + def gradient_angle(self, value: Angle) -> None: |
| 133 | + self._pptx.gradient_angle = to_pptx_angle(value) |
| 134 | + |
| 135 | + @property |
| 136 | + def gradient_stops(self) -> "GradientStops": |
| 137 | + """Gradient stops.""" |
| 138 | + return GradientStops(self._pptx.gradient_stops) |
| 139 | + |
| 140 | + def to_pptx(self) -> PptxGradFill: |
| 141 | + """Convert to pptx gradient fill.""" |
| 142 | + return self._pptx |
| 143 | + |
| 144 | + @classmethod |
| 145 | + def from_pptx(cls, pptx_obj: PptxGradFill) -> Self: |
| 146 | + """Create from pptx gradient fill.""" |
| 147 | + return cls(pptx_obj) |
| 148 | + |
| 149 | + |
| 150 | +class GradientStops(PptxConvertible[PptxGradientStops]): |
| 151 | + """Gradient stops.""" |
| 152 | + |
| 153 | + def __init__(self, pptx_obj: PptxGradientStops) -> None: |
| 154 | + self._pptx = pptx_obj |
| 155 | + |
| 156 | + def __getitem__(self, idx: int) -> "GradientStop": |
| 157 | + return GradientStop(self._pptx[idx]) |
| 158 | + |
| 159 | + def __len__(self) -> int: |
| 160 | + return len(self._pptx) |
| 161 | + |
| 162 | + def to_pptx(self) -> PptxGradientStops: |
| 163 | + """Convert to pptx gradient stops.""" |
| 164 | + return self._pptx |
| 165 | + |
| 166 | + @classmethod |
| 167 | + def from_pptx(cls, pptx_obj: PptxGradientStops) -> Self: |
| 168 | + """Create from pptx gradient stops.""" |
| 169 | + return cls(pptx_obj) |
| 170 | + |
| 171 | + |
| 172 | +class GradientStop(PptxConvertible[PptxGradientStop]): |
| 173 | + """Gradient stop.""" |
| 174 | + |
| 175 | + def __init__(self, pptx_obj: PptxGradientStop) -> None: |
| 176 | + self._pptx = pptx_obj |
| 177 | + |
| 178 | + @property |
| 179 | + def color(self) -> ColorFormat: |
| 180 | + """Color.""" |
| 181 | + return ColorFormat(self._pptx.color) |
| 182 | + |
| 183 | + @property |
| 184 | + def position(self) -> float: |
| 185 | + """Location of stop in gradient path as float between 0.0 and 1.0. |
| 186 | +
|
| 187 | + The value represents a percentage, where 0.0 (0%) represents the |
| 188 | + start of the path and 1.0 (100%) represents the end of the path. For |
| 189 | + a linear gradient, these would represent opposing extents of the |
| 190 | + filled area. |
| 191 | + """ |
| 192 | + return self._pptx.position |
| 193 | + |
| 194 | + @position.setter |
| 195 | + def position(self, value: float) -> None: |
| 196 | + self._pptx.position = value |
| 197 | + |
| 198 | + def to_pptx(self) -> PptxGradientStop: |
| 199 | + """Convert to pptx gradient stop.""" |
| 200 | + return self._pptx |
| 201 | + |
| 202 | + @classmethod |
| 203 | + def from_pptx(cls, pptx_obj: PptxGradientStop) -> Self: |
| 204 | + """Create from pptx gradient stop.""" |
| 205 | + return cls(pptx_obj) |
0 commit comments