Add option to set foreground color
This commit is contained in:
parent
ac5a746762
commit
105acfa65e
2 changed files with 31 additions and 5 deletions
35
app/Main.hs
35
app/Main.hs
|
@ -4,6 +4,7 @@ import Graphics.Gloss
|
||||||
import Options.Applicative
|
import Options.Applicative
|
||||||
import Data.Semigroup ((<>))
|
import Data.Semigroup ((<>))
|
||||||
import Data.List
|
import Data.List
|
||||||
|
import Safe
|
||||||
|
|
||||||
import Lib
|
import Lib
|
||||||
import Examples
|
import Examples
|
||||||
|
@ -11,6 +12,7 @@ import Examples
|
||||||
data Options = Options
|
data Options = Options
|
||||||
{ optionLSystem :: LSystem Char
|
{ optionLSystem :: LSystem Char
|
||||||
, optionIterations :: Integer
|
, optionIterations :: Integer
|
||||||
|
, optionColor :: Color
|
||||||
}
|
}
|
||||||
|
|
||||||
selectLSystem :: [LSystem a] -> String -> Either String (LSystem a)
|
selectLSystem :: [LSystem a] -> String -> Either String (LSystem a)
|
||||||
|
@ -43,8 +45,31 @@ listLSystems = infoOption (printList lsystems)
|
||||||
<> help "List all available L-systems")
|
<> help "List all available L-systems")
|
||||||
where printList xs = "Available L-systems:\n" ++ unlines (map name xs)
|
where printList xs = "Available L-systems:\n" ++ unlines (map name xs)
|
||||||
|
|
||||||
|
splitOn :: Char -> String -> [String]
|
||||||
|
splitOn c s = case dropWhile (== c) s of
|
||||||
|
"" -> []
|
||||||
|
s' -> w:(splitOn c s'')
|
||||||
|
where (w, s'') = break (== c) s'
|
||||||
|
|
||||||
|
colorParser :: Parser Color
|
||||||
|
colorParser = option (eitherReader readRGB)
|
||||||
|
(long "color"
|
||||||
|
<> short 'c'
|
||||||
|
<> help "Foreground color RGBA (0-255)"
|
||||||
|
<> showDefault
|
||||||
|
<> value white
|
||||||
|
<> metavar "R,G,B")
|
||||||
|
where readRGB s = do
|
||||||
|
case mapM readEitherSafe $ splitOn ',' s of
|
||||||
|
Right (r:g:b:a:_) -> Right $ makeColorI r g b a
|
||||||
|
Right (r:g:b:_) -> Right $ makeColorI r g b 255
|
||||||
|
Right (r:g:_) -> Right $ makeColorI r g 255 255
|
||||||
|
Right (r:_) -> Right $ makeColorI r 255 255 255
|
||||||
|
Right _ -> Right $ makeColorI 255 255 255 255
|
||||||
|
Left s -> Left s
|
||||||
|
|
||||||
options :: Parser Options
|
options :: Parser Options
|
||||||
options = Options <$> lsystem <*> iterations
|
options = Options <$> lsystem <*> iterations <*> colorParser
|
||||||
|
|
||||||
opts :: ParserInfo Options
|
opts :: ParserInfo Options
|
||||||
opts = info (options <**> listLSystems <**> helper)
|
opts = info (options <**> listLSystems <**> helper)
|
||||||
|
@ -52,11 +77,11 @@ opts = info (options <**> listLSystems <**> helper)
|
||||||
<> progDesc "Generate and draw an L-system"
|
<> progDesc "Generate and draw an L-system"
|
||||||
<> header "lsystems -- Generate L-systems")
|
<> header "lsystems -- Generate L-systems")
|
||||||
|
|
||||||
createDisplay :: (Eq a, Integral p) => p -> LSystem a -> IO ()
|
createDisplay :: (Eq a, Integral p) => Color -> p -> LSystem a -> IO ()
|
||||||
createDisplay n ls = display (InWindow "L-System" (200, 200) (10, 10)) black (color white pic)
|
createDisplay c n ls = display (InWindow "L-System" (200, 200) (10, 10)) black (color c pic)
|
||||||
where pic = drawLSystem $ iterateLSystem n ls
|
where pic = drawLSystem $ iterateLSystem n ls
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
Options ls n <- execParser opts
|
Options ls n c <- execParser opts
|
||||||
createDisplay n ls
|
createDisplay c n ls
|
||||||
|
|
|
@ -22,6 +22,7 @@ dependencies:
|
||||||
- base >= 4.7 && < 5
|
- base >= 4.7 && < 5
|
||||||
- gloss
|
- gloss
|
||||||
- optparse-applicative
|
- optparse-applicative
|
||||||
|
- safe
|
||||||
|
|
||||||
library:
|
library:
|
||||||
source-dirs: src
|
source-dirs: src
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue