1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
"""Displays some RGB colorgrids in the terminal."""
import colorsys
import time
from argparse import ArgumentParser, Namespace
from pytermgui import ColorSystem, terminal, tim
def _normalize(_rgb: tuple[float, float, float]) -> str:
normalized = tuple(str(int(_col * 255)) for _col in _rgb)
return normalized[0], normalized[1], normalized[2]
def _get_colorbox(width: int) -> str:
_buff = ""
for y_pos in range(0, 5):
for x_pos in range(width):
# Mmmm, spiky code
_hue = x_pos / width
_lightness = 0.1 + ((y_pos / 5) * 0.7)
_rgb1 = colorsys.hls_to_rgb(_hue, _lightness, 1.0)
_rgb2 = colorsys.hls_to_rgb(_hue, _lightness + 0.7 / 10, 1.0)
_bg_color = ";".join(_normalize(_rgb1))
_color = ";".join(_normalize(_rgb2))
_buff += f"[{_bg_color} @{_color}]▀"
_buff += "[/]\n"
return _buff
def _highlight(timing: float) -> str:
"""Highlights a timing based on whether it is under 60 fps."""
if timing < 1 / 60:
return f"[#57A773] {timing}"
return f"[red] {timing}"
def print_colorboxes(args: Namespace) -> None:
"""Prints some color boxes."""
tim.print("[210 bold]Note:")
tim.print(
" These results are gathered using [dim italic]tim.should_cache = False[/]."
)
tim.print(" Thus, these only show unoptimized, one cold and one warm cache")
tim.print(" timings, which are magnitudes slower than the optimized end-user")
tim.print(" performance.")
tim.print()
if not args.cache:
tim.should_cache = False
_buff = _get_colorbox(args.width)
for system in reversed(ColorSystem):
terminal.forced_colorsystem = system
tim.print(str(system).center(args.width))
start = time.time()
tim.print(_buff)
if not args.no_timings:
tim.print(
"[dim italic]Rendered in ([blue]cold[/]):"
+ _highlight(time.time() - start)
)
warm_start = time.time()
tim.parse(_buff)
tim.print(
"[dim italic]Rendered in ([208]warm[/]):"
+ _highlight(time.time() - warm_start)
)
tim.print()
terminal.forced_colorsystem = None
def main() -> None:
"""Main method."""
open("colorgrids.html", "w").close()
parser = ArgumentParser()
parser.add_argument(
"--no-timings", help="Don't show timing information.", action="store_true"
)
parser.add_argument(
"-c",
"--cache",
help="Use TIM caching. Will likely cause some issues.",
action="store_true",
)
parser.add_argument(
"-w", "--width", help="Width of color grids.", default=70, type=int
)
parser.add_argument(
"--export", help="Export to SVG file `colorgrids.svg`.", action="store_true"
)
args = parser.parse_args()
if args.export:
with terminal.record() as recording:
print_colorboxes(args)
recording.save_svg("colorgrids.svg")
return
print_colorboxes(args)
if __name__ == "__main__":
main()
|