Gradient
Module: terminaltexteffects.utils.graphics
Basic Usage
from terminaltexteffects.utils.graphics import Color, Gradient
rgb = Gradient(Color("#ff0000"), Color("#00ff00"), Color("#0000ff"), steps=5)
for color in rgb:
assert isinstance(color, Color)
print(color.rgb_color)
Iteration and indexing return Color objects. Use color.rgb_color when a six-digit RGB string is needed.
Stops, Steps, and Loops
Provide one or more Color stops. A step count is the number of transitions between two adjacent stops, so a
two-stop gradient with steps=4 contains five colors, including both stops:
from terminaltexteffects.utils.graphics import Color, Gradient
gradient = Gradient(Color("000000"), Color("ffffff"), steps=4)
assert [color.rgb_color for color in gradient] == [
"000000",
"404040",
"808080",
"bfbfbf",
"ffffff",
]
A single integer applies to every transition. A tuple assigns counts in stop-pair order; when it is shorter than the number of transitions, its final value is repeated. A tuple cannot be empty or longer than the number of transitions, and every count must be a positive, non-boolean integer. A single-stop gradient always contains that one stop after validating its step values.
With loop=True, a gradient with multiple stops adds a final transition from the last stop back to the first. The
first stop therefore also appears as the final spectrum color:
loop = Gradient(Color("ffffff"), Color("000000"), steps=(2, 3), loop=True)
assert [color.rgb_color for color in loop] == [
"ffffff",
"808080",
"000000",
"555555",
"aaaaaa",
"ffffff",
]
Invalid stops raise TypeError. Missing stops, invalid step counts, and overlong step tuples raise ValueError.
Fractional Lookup
get_color_at_fraction() accepts a finite number from 0 through 1 and selects the nearest precomputed spectrum
sample. Half-sample ties use Python's ties-to-even round() behavior. Non-numeric values raise TypeError; non-finite
or out-of-range values raise ValueError.
Coordinate Mappings
Coordinate mappings include every row and column bound and return dict[Coord, Color]:
from terminaltexteffects.utils.geometry import Coord
from terminaltexteffects.utils.graphics import Color, Gradient
gradient = Gradient(Color("000000"), Color("ffffff"), steps=2)
mapping = gradient.build_coordinate_color_mapping(
min_row=1,
max_row=1,
min_column=4,
max_column=6,
direction=Gradient.Direction.HORIZONTAL,
)
assert [mapping[Coord(column, 1)].rgb_color for column in range(4, 7)] == [
"000000",
"808080",
"ffffff",
]
HORIZONTALmaps the first and final colors tomin_columnandmax_column.VERTICALmaps the first and final colors tomin_rowandmax_row.DIAGONALmaps the first color at(min_column, min_row)and the final color at(max_column, max_row).RADIALmeasures from the rectangle's geometric center toward its corners; the center is fraction0and every corner is fraction1.
A direction with no span uses the first color. Bounds must be non-boolean integers greater than zero, each minimum must
not exceed its maximum, and direction must be a Gradient.Direction. Invalid bound types or directions raise
TypeError; invalid bound values raise ValueError.
Printing Gradients
Gradients can be printed to the terminal to show information about the stops, steps, and resulting spectrum.

Gradient Reference
A Gradient is a list of Color objects transitioning from one color stop to another.
The spectrum is calculated using linear interpolation between adjacent stops. Iteration and indexing return
Color objects. If there is only one stop, the gradient contains that color once because there are no transitions.
Ex: stops = ("ffffff", "aaaaaa", "000000"), steps = (6, 3)
"ffffff" -> (6 transitions) -> "aaaaaa" -> (3 transitions) -> "000000"
A step count is the number of transitions between a pair of adjacent stops. Total number of colors in a multi-stop gradient spectrum is the sum of the effective step counts plus 1. A single integer applies to every transition. A tuple assigns counts in order; if it contains fewer values than transitions, its last value is repeated. When transitions exist, a tuple cannot contain more values than there are transitions. For a single-stop gradient, step values are validated but tuple cardinality does not change the one-color spectrum.
With loop=True and multiple stops, a closing transition is generated from the last stop back to the first, and
the first stop is repeated as the final spectrum color.
Attributes:
| Name | Type | Description |
|---|---|---|
spectrum |
list[Color]
|
The generated |
Source code in terminaltexteffects/utils/graphics.py
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 | |
Direction
__getitem__(index)
Return the color at the given index or a list of colors based on the slice.
__init__(*stops, steps=1, loop=False)
Initialize a Gradient object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stops
|
Color
|
One or more |
()
|
steps
|
int | tuple[int, ...]
|
Number of transitions or a tuple of transition counts for generating the spectrum. A single value is repeated for every adjacent stop pair. Defaults to 1. |
1
|
loop
|
bool
|
Add a closing transition from the final stop back to the first when multiple stops are present. The first stop is repeated as the final spectrum color. Defaults to False. |
False
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If any stop is not a |
ValueError
|
If no color stops are provided, a step count is invalid, or a steps tuple has more entries than the gradient has transitions. |
Attributes:
| Name | Type | Description |
|---|---|---|
_stops |
tuple[Color]
|
Tuple of Color objects representing the color stops. |
_steps |
int | tuple[int, ...]
|
Transition count or tuple of transition counts used to generate the spectrum. |
_loop |
bool
|
Whether to generate a closing transition back to the first stop. |
spectrum |
list[Color]
|
List of generated |
Returns:
| Type | Description |
|---|---|
None
|
None |
Source code in terminaltexteffects/utils/graphics.py
__iter__()
__len__()
__str__()
Return a string representation of the Gradient object.
Source code in terminaltexteffects/utils/graphics.py
build_coordinate_color_mapping(min_row, max_row, min_column, max_column, direction)
Build a mapping of coordinates to colors based on the gradient and a direction.
For example, a vertical gradient will have the same color for each character in a row. When applied across all characters in the canvas, the gradient will be visible as a vertical gradient. The mapping respects the provided row and column bounds for every direction. Vertical, horizontal, and diagonal mappings assign the first and final gradient colors to the endpoints of each non-degenerate directional span. Radial mappings assign the first color at the center and the final color at the corners. A mapping containing only one coordinate receives the first color in every direction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
min_row
|
int
|
The minimum row value. Must be greater than 0 and less than or equal to max_row. |
required |
max_row
|
int
|
The maximum row value. Must be greater than 0 and greater than or equal to min_row. |
required |
min_column
|
int
|
The minimum column value. Must be greater than 0 and less than or equal to max_column. |
required |
max_column
|
int
|
The maximum column value. Must be greater than 0 and greater than or equal to min_column. |
required |
direction
|
Direction
|
The direction of the gradient. |
required |
Returns:
| Type | Description |
|---|---|
dict[Coord, Color]
|
dict[geometry.Coord, Color]: A mapping of coordinates to |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If any coordinate bound is less than one or a minimum bound exceeds its maximum. |
Source code in terminaltexteffects/utils/graphics.py
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 | |
get_color_at_fraction(fraction)
Return the precomputed spectrum color corresponding to a normalized fraction.
The fraction is matched to the nearest precomputed spectrum color from start
to end. Ties use Python's round-to-even behavior. A fraction of 0 returns
the first color and 1 returns the final color.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fraction
|
float
|
The fraction of the gradient to get the color for. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Color |
Color
|
The color at the fraction of the gradient. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |