Skip to content

Commit f19b9b8

Browse files
committed
feat: enhance color handling by introducing LiteralColor and to_color function
1 parent 54605b3 commit f19b9b8

File tree

5 files changed

+55
-8
lines changed

5 files changed

+55
-8
lines changed

examples/dataflame_table.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from pathlib import Path
44

55
import tppt
6-
from tppt.types import Color
76

87
# Flag to determine whether to use Polars
98
EXAMPLE_DIR = Path(__file__).parent
@@ -44,7 +43,7 @@ def main():
4443
height=(50, "pt"),
4544
size=(44, "pt"),
4645
bold=True,
47-
color=Color("#0066CC"),
46+
color="#0066CC",
4847
)
4948
.text(
5049
"Easily convert Polars dataframes to presentations with tppt library",

src/tppt/_pptx/converter.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from pptx.util import Length as PptxLength
99
from pptx.util import Pt as PptxPt
1010

11-
from tppt.types._color import Color
11+
from tppt.types._color import Color, LiteralColor, to_color
1212
from tppt.types._length import (
1313
Centimeter,
1414
Inch,
@@ -60,5 +60,7 @@ def to_pptx_length(length: Length | LiteralLength | None) -> PptxLength | None:
6060
assert_never(length)
6161

6262

63-
def to_pptx_color(color: Color) -> PptxRGBColor:
63+
def to_pptx_color(color: Color | LiteralColor) -> PptxRGBColor:
64+
color = to_color(color)
65+
6466
return PptxRGBColor(color.r, color.g, color.b)

src/tppt/_pptx/shape/text.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from pptx.shapes.autoshape import Shape as PptxShape
55

66
from tppt._pptx.converter import to_pptx_color, to_pptx_length
7-
from tppt.types._color import Color
7+
from tppt.types._color import Color, LiteralColor
88
from tppt.types._length import Length, LiteralLength
99

1010
from . import RangeProps, Shape
@@ -16,7 +16,7 @@ class TextProps(RangeProps):
1616
size: NotRequired[Length | LiteralLength]
1717
bold: NotRequired[bool]
1818
italic: NotRequired[bool]
19-
color: NotRequired[Color]
19+
color: NotRequired[Color | LiteralColor]
2020
margin_bottom: NotRequired[Length | LiteralLength]
2121
margin_left: NotRequired[Length | LiteralLength]
2222
vertical_anchor: NotRequired[MSO_ANCHOR]

src/tppt/types/_color.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
"""Color types for tppt."""
22

3-
from typing import overload
3+
from typing import TypeAlias, overload
44

55
from tppt.exception import ColorInvalidFormatError
66

7+
LiteralColor: TypeAlias = tuple[int, int, int] | str
8+
79

810
class Color:
911
"""Represents a color in RGB or by hex code.
@@ -49,3 +51,13 @@ def __init__(self, r: str | int, g: int | None = None, b: int | None = None):
4951
def __repr__(self) -> str:
5052
"""Return string representation."""
5153
return f"Color({self.r}, {self.g}, {self.b})"
54+
55+
56+
def to_color(color: Color | LiteralColor) -> Color:
57+
if isinstance(color, Color):
58+
return color
59+
else:
60+
if isinstance(color, tuple):
61+
return Color(*color)
62+
else:
63+
return Color(color)

tests/test_color.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import unittest
44

55
from tppt.exception import ColorInvalidFormatError
6-
from tppt.types._color import Color
6+
from tppt.types._color import Color, to_color
77

88

99
class TestColor(unittest.TestCase):
@@ -44,3 +44,37 @@ def test_repr(self):
4444
"""Test string representation."""
4545
color = Color(10, 20, 30)
4646
assert repr(color) == "Color(10, 20, 30)"
47+
48+
49+
class TestToColor(unittest.TestCase):
50+
"""Test cases for to_color function."""
51+
52+
def test_to_color_with_color_instance(self):
53+
"""Test to_color with a Color instance."""
54+
original = Color(10, 20, 30)
55+
result = to_color(original)
56+
assert result is original
57+
58+
def test_to_color_with_tuple(self):
59+
"""Test to_color with a RGB tuple."""
60+
result = to_color((10, 20, 30))
61+
assert isinstance(result, Color)
62+
assert result.r == 10
63+
assert result.g == 20
64+
assert result.b == 30
65+
66+
def test_to_color_with_str(self):
67+
"""Test to_color with a hex string."""
68+
result = to_color("#123456")
69+
assert isinstance(result, Color)
70+
assert result.r == 0x12
71+
assert result.g == 0x34
72+
assert result.b == 0x56
73+
74+
def test_to_color_with_short_hex(self):
75+
"""Test to_color with a short hex string."""
76+
result = to_color("#123")
77+
assert isinstance(result, Color)
78+
assert result.r == 0x11
79+
assert result.g == 0x22
80+
assert result.b == 0x33

0 commit comments

Comments
 (0)