11"""Type definitions for pptx wrapper."""
22
33from typing import (
4- Protocol ,
4+ Generic ,
55 Self ,
66 TypeAlias ,
77 TypeVar ,
88 assert_never ,
99 overload ,
10- runtime_checkable ,
1110)
1211
1312from pptx .dml .color import RGBColor as PptxRGBColor
1918from pptx .util import Pt as PptxPt
2019
2120from tppt .types ._angle import Angle , Degrees , LiteralAngle
22- from tppt .types ._color import LiteralColor , RGBColor , to_rgb_color
21+ from tppt .types ._color import Color , LiteralColor , to_color
2322from tppt .types ._length import (
2423 CentiMeters ,
2524 EnglishMetricUnits ,
3433PT = TypeVar ("PT" )
3534
3635
37- @runtime_checkable
38- class PptxConvertible (Protocol [PT ]):
36+ class PptxConvertible (Generic [PT ]):
3937 """Protocol for objects that can be converted to and from pptx objects."""
4038
39+ def __init__ (self , pptx_obj : PT , / ) -> None :
40+ self ._pptx : PT = pptx_obj
41+
4142 def to_pptx (self ) -> PT :
4243 """Convert to pptx object."""
43- ...
44+ return self . _pptx
4445
4546 @classmethod
4647 def from_pptx (cls , pptx_obj : PT ) -> Self :
4748 """Create from pptx object."""
48- ...
49+ return cls ( pptx_obj )
4950
5051
5152@overload
52- def to_pptx_length (length : Length | LiteralLength ) -> PptxLength : ...
53+ def to_pptx_length (length : Length | LiteralLength | PptxLength ) -> PptxLength : ...
5354
5455
5556@overload
56- def to_pptx_length (length : Length | LiteralLength | None ) -> PptxLength | None : ...
57+ def to_pptx_length (
58+ length : Length | LiteralLength | PptxLength | None ,
59+ ) -> PptxLength | None : ...
5760
5861
59- def to_pptx_length (length : Length | LiteralLength | None ) -> PptxLength | None :
62+ def to_pptx_length (
63+ length : Length | LiteralLength | PptxLength | None ,
64+ ) -> PptxLength | None :
6065 if isinstance (length , tuple ):
6166 length = to_length (length )
6267
@@ -71,6 +76,8 @@ def to_pptx_length(length: Length | LiteralLength | None) -> PptxLength | None:
7176 return PptxMm (length .value )
7277 case EnglishMetricUnits ():
7378 return PptxEmu (length .value )
79+ case PptxLength ():
80+ return length
7481 case None :
7582 return None
7683 case _:
@@ -90,32 +97,46 @@ def to_tppt_length(length: PptxLength | None) -> Length | None:
9097
9198
9299@overload
93- def to_pptx_rgb_color (color : RGBColor | LiteralColor ) -> PptxRGBColor : ...
100+ def to_pptx_rgb_color (
101+ color : Color | LiteralColor | PptxRGBColor ,
102+ ) -> tuple [PptxRGBColor , int | None ]: ...
94103
95104
96105@overload
97- def to_pptx_rgb_color (color : RGBColor | LiteralColor | None ) -> PptxRGBColor | None : ...
98-
99-
100- def to_pptx_rgb_color (color : RGBColor | LiteralColor | None ) -> PptxRGBColor | None :
106+ def to_pptx_rgb_color (
107+ color : Color | LiteralColor | PptxRGBColor | None ,
108+ ) -> tuple [PptxRGBColor , int | None ] | None : ...
109+
110+
111+ def to_pptx_rgb_color (
112+ color : Color | LiteralColor | PptxRGBColor | None ,
113+ ) -> (
114+ tuple [
115+ PptxRGBColor ,
116+ int | None ,
117+ ]
118+ | None
119+ ):
101120 if color is None :
102121 return None
103122
104- color = to_rgb_color (color )
123+ color = to_color (color )
105124
106- return PptxRGBColor (color .r , color .g , color .b )
125+ return PptxRGBColor (color .r , color .g , color .b ), color . a
107126
108127
109128@overload
110- def to_tppt_rgb_color (color : PptxRGBColor ) -> RGBColor : ...
129+ def to_tppt_rgb_color (color : PptxRGBColor , alpha : int | None ) -> Color : ...
111130
112131
113132@overload
114- def to_tppt_rgb_color (color : PptxRGBColor | None ) -> RGBColor | None : ...
133+ def to_tppt_rgb_color (
134+ color : PptxRGBColor | None , alpha : int | None
135+ ) -> Color | None : ...
115136
116137
117- def to_tppt_rgb_color (color : PptxRGBColor | None ) -> RGBColor | None :
118- return RGBColor ( * color ) if color else None
138+ def to_tppt_rgb_color (color : PptxRGBColor | None , alpha : int | None ) -> Color | None :
139+ return Color ( color [ 0 ], color [ 1 ], color [ 2 ], alpha ) if color else None
119140
120141
121142PptxAngle : TypeAlias = float
0 commit comments