Skip to content

Instantly share code, notes, and snippets.

@lucaspcamargo
Last active October 20, 2022 02:23
Show Gist options
  • Save lucaspcamargo/f363a7ec59e4ea62fd0e47a36e122e8a to your computer and use it in GitHub Desktop.
Save lucaspcamargo/f363a7ec59e4ea62fd0e47a36e122e8a to your computer and use it in GitHub Desktop.
#! /usr/bin/python3
"""
rainbow_vtrgb
animates vt colors like a rainbow
only useful with something else that updates the whole screen
(wondering if fbconsole has an ioctl or something like that to force the whole bitmap to update)
"""
import subprocess as sp
import time
import copy
MUL_SAT = 0.5
MUL_VAL = 1.0
MIN_SAT = 1.0
MIN_VAL = 0.2
ANGLE_STEP = 2.0
DELAY=1.0/30.0
DBG=False
VGA_VALS = [
[0,170,0,170,0,170,0,170,85,255,85,255,85,255,85,255], #R
[0,0,170,85,0,0,170,170,85,85,255,255,85,85,255,255 ], #G
[0,0,0,0,170,170,170,170,85,85,85,85,255,255,255,255], #B
]
TRIPLES = list(zip(*VGA_VALS))
def hsv_to_rgb(h, s, v):
# stolen from https://stackoverflow.com/questions/24852345/hsv-to-rgb-color-conversion
if s == 0.0: return (v, v, v)
i = int(h*6.) # XXX assume int() truncates!
f = (h*6.)-i; p,q,t = v*(1.-s), v*(1.-s*f), v*(1.-s*(1.-f)); i%=6
if i == 0: return (v, t, p)
if i == 1: return (q, v, p)
if i == 2: return (p, v, t)
if i == 3: return (p, q, v)
if i == 4: return (t, p, v)
if i == 5: return (v, p, q)
def main():
angle = 0.0
while (True):
final_colors = []
for triple in TRIPLES:
mulval = hsv_to_rgb(angle/360.0, MUL_SAT, MUL_VAL)
minval = [x*255 for x in hsv_to_rgb(angle/360.0, MIN_SAT, MIN_VAL)]
if DBG: print(triple, mulval, minval)
color = [triple[0] * mulval[0], triple[1]*mulval[1], triple[2]*mulval[2],]
color = [max(color[0], minval[0]), max(color[1], minval[1]), max(color[2], minval[2]),]
color = [int(x) for x in color]
final_colors.append(color)
final_split = zip(*final_colors)
if DBG: print(f"final_split: {final_split}")
strout=" " # setvtrgb BUG @linux/kbd/src/setvtrgb.c:144~154 :: rewind() after fgetc() won't work for stdin, hence the space
for idx, channel in enumerate(final_split):
if DBG: print(f"channel[{idx}]: {channel}")
strout += f"{','.join([str(x) for x in channel])}\n"
if DBG: print('strout=',strout)
try:
sp.check_output(['setvtrgb', '-'], input=strout, text=True)
except:
import traceback as tb
import sys
tb.print_exc()
sys.exit(1)
angle += ANGLE_STEP
angle %= 360.0
if DBG: print(f"angle={angle}")
time.sleep(DELAY/2.0)
if __name__=="__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment