Skip to content
This repository was archived by the owner on Oct 5, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions challenges/09-lolcat/solutions/bytecommander/raincat1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import sys
h,s,v=0,1,1
def R():H=h%360;C=v*s;M=v-C;X=int(255*(M+C*(1-abs((H/60)%2-1))));C=int((C+M)*255);return[[[[[(C,0,X),(X,0,C)][H<300],(0,X,C)][H<240],(0,C,X)][H<180],(X,C,0)][H<120],(C,X,0)][H<60]
f=open(sys.argv[1])
for l in f:print(end="\033[38;2;%d;%d;%dm%%s\033[0m"%R()%l);h+=10
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions challenges/09-lolcat/solutions/bytecommander/raincat2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import sys
def R(H):X=int(255*(1-abs((H/60)%2-1)));C=255;return[[[[[(C,0,X),(X,0,C)][H<300],(0,X,C)][H<240],(0,C,X)][H<180],(X,C,0)][H<120],(C,X,0)][H<60]
i=0
for l in open(*(sys.argv+[0])[1:2]):print("".join("\033[38;2;%d;%d;%dm"%R(h%360)+c for h,c in enumerate(l,i)),end="\033[0m");i+=8
30 changes: 30 additions & 0 deletions challenges/09-lolcat/solutions/bytecommander/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# `raincat.py` by ByteCommander

This script acts similar to `lolcat` as it prints its input to the terminal in a rainbow-colored way.
As the challenge includes code-golf, it is written as concise as possible, though surely further optimizations
could still be made.

It contains a HSV (Hue-Saturation-Value) to RGB (Red-Green-Blue) color conversion function inspired by
https://www.rapidtables.com/convert/color/hsv-to-rgb.html so that we can easily cycle through the hue degrees
for a nice rainbow gradient.

For coloring the output, we use ANSI escape codes to display 24-bit RGB colors,
as described e.g. on https://stackoverflow.com/a/33206814/4464570.

- The first version [`raincat1.py`](/challenges/09-lolcat/solutions/bytecommander/raincat1.py)
only produced a plain vertical gradient and required exactly one input file path as command-line argument.
It contains the full color conversion formula that would accept varying Saturation and Value parameters,
but these are constantly 1 anyway.

![raincat1.png](/challenges/09-lolcat/solutions/bytecommander/raincat1.png)

- The second version [`raincat2.py`](/challenges/09-lolcat/solutions/bytecommander/raincat2.py)
creates a much finer, diagonal gradient by coloring each letter individually,
with a small hue delta per character and a slightly larger starting offset per line.
It also takes either one single file path as command-line argument (more arguments will be ignored)
or alternatively reads from stdin if none are given.
The conversion function has been simplified by removing the Saturation and Value parameters.

![raincat2.png](/challenges/09-lolcat/solutions/bytecommander/raincat2.png)

Tested and compatible with at least Python 3.5+ on Ubuntu, using gnome-terminal.