logo

utils

~/.local/bin tools and git-hooks

lolcat.c (684B)


      1 // Copyright 2018 Haelwenn (lanodan) Monnier <contact@hacktivis.me>
      2 // Distributed under the terms of the CC-BY-SA-4.0 license
      3 
      4 #include <stdio.h> /* getchar(), putchar(), snprintf(), printf() */
      5 #include <math.h> /* sin() */
      6 
      7 void rainbow(double freq, int i) {
      8 	int red, green, blue;
      9 	double pi = 3.14159;
     10 
     11 	red = sin(freq*i + 0) * 127 + 128;
     12 	green = sin(freq*i + 2*pi/3) * 127 + 128;
     13 	blue = sin(freq*i + 4*pi/3) * 127 + 128;
     14 
     15 	printf("[38;2;%02d;%02d;%02dm", red, green, blue);
     16 	// TODO: Replace to sprintf?
     17 }
     18 
     19 int main(void) {
     20 	int c, i;
     21 	double freq = 0.1;
     22 
     23 	i = 0;
     24 
     25 	while((c = getchar()) != EOF)
     26 	{
     27 		rainbow(freq, i);
     28 		i++;
     29 		putchar(c);
     30 	}
     31 
     32 	printf("");
     33 
     34 	return 0;
     35 }