diff --git a/challenges/09-lolcat/solutions/bytecommander/raincat1.png b/challenges/09-lolcat/solutions/bytecommander/raincat1.png new file mode 100644 index 0000000..bb2b6b7 Binary files /dev/null and b/challenges/09-lolcat/solutions/bytecommander/raincat1.png differ diff --git a/challenges/09-lolcat/solutions/bytecommander/raincat1.py b/challenges/09-lolcat/solutions/bytecommander/raincat1.py new file mode 100644 index 0000000..78c2a87 --- /dev/null +++ b/challenges/09-lolcat/solutions/bytecommander/raincat1.py @@ -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 diff --git a/challenges/09-lolcat/solutions/bytecommander/raincat2.png b/challenges/09-lolcat/solutions/bytecommander/raincat2.png new file mode 100644 index 0000000..728db98 Binary files /dev/null and b/challenges/09-lolcat/solutions/bytecommander/raincat2.png differ diff --git a/challenges/09-lolcat/solutions/bytecommander/raincat2.py b/challenges/09-lolcat/solutions/bytecommander/raincat2.py new file mode 100644 index 0000000..8b00564 --- /dev/null +++ b/challenges/09-lolcat/solutions/bytecommander/raincat2.py @@ -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 diff --git a/challenges/09-lolcat/solutions/bytecommander/readme.md b/challenges/09-lolcat/solutions/bytecommander/readme.md new file mode 100644 index 0000000..57bf185 --- /dev/null +++ b/challenges/09-lolcat/solutions/bytecommander/readme.md @@ -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.