Skip to content

Instantly share code, notes, and snippets.

@barbaramartina
Last active April 7, 2016 10:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barbaramartina/46821e9b120a13c2a481225141d5dd68 to your computer and use it in GitHub Desktop.
Save barbaramartina/46821e9b120a13c2a481225141d5dd68 to your computer and use it in GitHub Desktop.
Haskell piece to generate a SVG file with a set of lambda paths
module LambdasSVG where
import System.IO
import Data.String
import System.Random
writeSVG :: [String] -> String
writeSVG [] = error "No data to be written"
writeSVG xs = "<svg xmlns=\"http://www.w3.org/2000/svg\">" ++ foldr (++) "" xs ++ "</svg>"
-- | create a lambda path
lambda n (r1,g1,b1) (r2,g2,b2) = "<path style=\"fill:rgb("++ show r1 ++","++ show g1 ++","++ show b1 ++" );fill-rule:evenodd;\
\stroke:rgb("++ show r2 ++","++ show g2 ++","++ show b2 ++");stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1\" \
\d=\"m 52.745144,9.9489141 -10.392586,0 13.573991,23.9239199 -13.680037,23.31977 10.180492,0.24166 8.801885,-14.62017 8.589791,\
\14.62017 10.392588,-0.24166 z\" id=\"path" ++ show n ++"\"/>"
-- | Returns a list of random numbers in the given range 'r', generated with seed 's'
rand s r = [ x | x <- (randomRs (1,r) (mkStdGen s) ) :: [Int]]
-- | List of 20 colors to fill the paths. You can change the seeds given to random to change the generated sequence.
fillColors = take 20 [(r, g, b) | r <- take 5 (rand 5 255), g <- take 3 (rand 67 255), b <- take 6 (rand 78 255)]
-- | List of 2 colors to paint the borders. You can change the seeds given to random to change the generated sequence.
borderColors = take 2 [(r, g, b) | r <- take 5 (rand 7777 255), g <- take 3 (rand 456 255), b <- take 6 (rand 6 255)]
-- | write a SVG file with the generated lambdas
main = writeFile "lambdas.svg" $ writeSVG [lambda n fill border | n <- [1..15], border <- borderColors, fill <- fillColors]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment