From d8509a45e19dac4aba8a688444bad4388742dcfc Mon Sep 17 00:00:00 2001 From: Stefan Dresselhaus Date: Sun, 6 Dec 2015 01:48:59 +0100 Subject: [PATCH] added flag for 256-color support for older terminals and screen --- rose_256.img | 40 ++++++++++++++++++++++++++++++++++++++++ src/Main.hs | 26 +++++++++++++++++++++++--- 2 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 rose_256.img diff --git a/rose_256.img b/rose_256.img new file mode 100644 index 0000000..1c073d3 --- /dev/null +++ b/rose_256.img @@ -0,0 +1,40 @@ +                                                                                                     +                                                                                                     +                                                                                                     +                                                                                                     +                                                                                                     +                                                                                                     +                                                               ooo.                                  +                                                           oo oooooo                                 +                                  oox                   ooooooooooooo                                +                                 xxoooooooo.   oooooooooooooooooooooooo                              +                                 ooooooooooooooooooooooooooooooooooooooo .                           +                                 oooooooooooooooooxoooooooooooooooooooooooo                          +                                oooooooooooooooooooooooooooooooooooooooooooooooooo                   +                               oooooooooooooxxoooooooooxxoooooxooooooooooooooooox.                   +                               ooooooooooooooooooooxxxoooooooooxoooooooooooooooxx                    +                              ooooooooooooxoooxxxxxooxxxxxoooooooooxooooooooooxx                     +                            .ooooooooooooxxoooxxxooxooxxxooooooxooooooooooooooxx                     +                          oooooooooooooooooooxoooxxooxxxooxooooooooooxoooooooxxx                     +                  .xooooooooooooooooooooooooooooooooooooooooooooooooxxooooooooxx                     +                    xxxxooooooooooooooooooooooooooooooxxxxoooxoooooo.xooooooooox                     +                     xxxooooooooooooooooooooooxooooxxxxoooooxoooooox xoooooooooo                     +                      xxxxoooooooooooooooooooooooooooooooxooooooox.  ooooooxxoo                      +                      .xooooooooooooooooooooooooooxoooxoooooooooxxxxoooooxxx.                        +                       xxxxxooooooxooooooxx.oooooooxooooooooxxxxxxooooooxxxx                         +                       xxxoxxxoooooxxoooooooxxxxxxxooox.xxxxooooooooooxxxxx                          +                       xxxxxxxxxxoooxxxxxxxxoooooooooooooooooooxxoooxxxxx.                           +                       xx..xxxxxxxxoxxxooooooooooooooooooooxxx..oooxxx..                             +                            xxxxxxxxxx.xxxxxxxxxxx...xxxx......xoxxx..                               +                            xxxxx.xxxxxx.........        ......xxx..                                 +                            ooooooooxxxxxx.........     .xx......                                    +                            oooooooooooxxx...        .........                                       +                           ooooooooooooooooooxx.......x....x..                                       +                          oooooooooooooooooooooooooxx.......                                         +                         ooxxxxxxxxxxxxxoooooooooooooo....                                           +                            x...xxxxxxxxxxxxxxoooooxxxx.                                             +                                 ...........xxxxooxxx.                                               +                                               .xox                                                  +                                                                                                     +                                                                                                     +                                                                                                     diff --git a/src/Main.hs b/src/Main.hs index fed4a33..918b70a 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -16,13 +16,15 @@ data Options = Options { srcFile :: String , width :: Int , height :: Int + , trueColor :: Bool } options :: Parser Options options = Options <$> argument str (metavar "SRC" <> help "source file (or - for stdin)") <*> argument auto (metavar "WIDTH" <> help "resulting width") - <*> argument auto (metavar "HEIGH" <> help "resulting height") + <*> argument auto (metavar "HEIGHT" <> help "resulting height") + <*> flag True False (long "256-colors" <> short 'c' <> help "only use 256-color-mode for old terminals") opthelp :: ParserInfo Options opthelp = info (helper <*> options) @@ -35,7 +37,7 @@ main :: IO () main = execParser opthelp >>= run run :: Options -> IO () -run (Options src w h) = do +run (Options src w h c) = do src' <- if src == "-" then B.getContents else B.readFile src case decodeImage src' of Left err -> putStrLn err @@ -43,7 +45,7 @@ run (Options src w h) = do case extractDynImage img >>= pixelize w h of Nothing -> return () Just (f,b) -> - let str = img2ascii conv (f,b) + let str = if c then img2ascii conv (f,b) else img2ascii conv256 (f,b) in mapM_ (\x -> putStr x >> putStrLn "\x1b[0m") (concat <$> str) chunksof :: Int -> [a] -> [[a]] @@ -63,6 +65,24 @@ conv (fp@(PixelRGB8 fr fg fb),PixelRGB8 br bg bb) = printf "\x1b[48;2;%d;%d;%dm\ | x > 10 = '.' | otherwise = ' ' +conv256 :: (PixelRGB8,PixelRGB8) -> String +conv256 (fp@(PixelRGB8 fr fg fb),PixelRGB8 br bg bb) = printf "\x1b[48;5;%dm\x1b[38;5;%dm%c" bcolor fcolor (lumi.computeLuma $ fp) + where + -- converts [0..255] -> [0..5] + s = (`div` 51) + -- conversion: 6x6x6 rgb-cube so color is red * 36 + green * 6 + blue + 16 offset with red/green/blue in [0..5] + bcolor = s br * 36 + s bg * 6 + s bb + 16 + fcolor = s fr * 36 + s fg * 6 + s fb + 16 + lumi :: Word8 -> Char + lumi x + | x > 225 = '@' + | x > 180 = 'O' + | x > 150 = 'X' + | x > 50 = 'o' + | x > 25 = 'x' + | x > 10 = '.' + | otherwise = ' ' + img2ascii :: ((PixelRGB8,PixelRGB8) -> String) -> (Image PixelRGB8,Image PixelRGB8) -> [[String]] img2ascii c (fg@(Image w h _),bg@(Image w' h' _)) = (fmap.fmap) (c.(uncurry (pixelAt fg) &&& uncurry (pixelAt bg))) [[(x,y) | x <- [0..w-1]] | y <- [0..h-1]]