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