Skip to content

Instantly share code, notes, and snippets.

@CodeByAidan
Created July 24, 2024 19:23
Show Gist options
  • Select an option

  • Save CodeByAidan/f138eeffec39e0f6dbafac34b22c381a to your computer and use it in GitHub Desktop.

Select an option

Save CodeByAidan/f138eeffec39e0f6dbafac34b22c381a to your computer and use it in GitHub Desktop.
create a colored gradient text using Rich in Python
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