Skip to content

Instantly share code, notes, and snippets.

@lest
Created September 5, 2011 20:45
Show Gist options
  • Save lest/1195877 to your computer and use it in GitHub Desktop.
Save lest/1195877 to your computer and use it in GitHub Desktop.
simple lolcat powered by haskell
import Data.Word
freq = 0.3
spread = 8.0
unbase :: Integral int => int -> Word8 -> Word8 -> Word8 -> int
unbase base r g b = (fi r*base+fi g)*base+fi b
where fi = fromIntegral
-- | Approximate a 24-bit Rgb colour with a colour in the xterm256 6x6x6 colour cube, returning its index.
rgb24bit_to_xterm256 :: (Integral t) => Word8 -> Word8 -> Word8 -> t
rgb24bit_to_xterm256 r g b = let f = (`div` 43)
in 16 + unbase 6 (f r) (f g) (f b)
lolcat _ [] = ""
lolcat (color, lineColor, ansi) ('\n':xs) =
"\n" ++ lolcat (color', color', ansi) xs
where color' = lineColor + 1
lolcat (color, lineColor, ansi) ('\ESC':xs) =
lolcat (color, lineColor, True) xs
lolcat (color, lineColor, True) ('m':xs) =
lolcat (color, lineColor, False) xs
lolcat (color, lineColor, True) (x:xs) =
lolcat (color, lineColor, True) xs
lolcat (color, lineColor, False) (x:xs) =
"\ESC[38;5;" ++ colored ++ "m" ++ [x] ++ "\ESC[0m" ++ lolcat (color + 1 / spread, lineColor, False) xs
where red = round (sin (freq * color) * 127) + 128
green = round (sin (freq * color + 2 * pi / 3) * 127) + 128
blue = round (sin (freq * color + 4 * pi / 3) * 127) + 128
colored = show $ rgb24bit_to_xterm256 red green blue
main = interact $ lolcat (0, 0, False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment