Created
July 24, 2024 19:23
-
-
Save CodeByAidan/f138eeffec39e0f6dbafac34b22c381a to your computer and use it in GitHub Desktop.
create a colored gradient text using Rich in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def gradient_text(text: str, start_color: str, end_color: str) -> Text: | |
| gradient = Text() | |
| start: ColorTriplet | None = Color.parse(start_color).triplet | |
| end: ColorTriplet | None = Color.parse(end_color).triplet | |
| for i, char in enumerate(text): | |
| ratio: float = i / (len(text) - 1) | |
| blended_color = tuple( | |
| int(start[j] + (end[j] - start[j]) * ratio) for j in range(3) | |
| ) | |
| color_code: str = f"#{''.join(f'{value:02x}' for value in blended_color)}" | |
| gradient.append(char, style=color_code) | |
| return gradient |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment