From eb89cac14c9c9917ae7773d0991bbf3c1a38668f Mon Sep 17 00:00:00 2001 From: David Himmelstrup Date: Sun, 29 Sep 2019 10:06:24 +0800 Subject: [PATCH] Add colormaps to documentation. Former-commit-id: d9db76c4f0715800ef6f94e6618d6682a2c8112b --- docs/gen_doc_gifs.sh | 8 +++++++- examples/doc_cividis.hs | 18 ++++++++++++++++++ examples/doc_greyscale.hs | 18 ++++++++++++++++++ examples/doc_hsv.hs | 18 ++++++++++++++++++ examples/doc_hsvMatlab.hs | 18 ++++++++++++++++++ examples/doc_inferno.hs | 18 ++++++++++++++++++ examples/doc_jet.hs | 18 ++++++++++++++++++ examples/doc_magma.hs | 18 ++++++++++++++++++ examples/doc_parula.hs | 18 ++++++++++++++++++ examples/doc_plasma.hs | 18 ++++++++++++++++++ examples/doc_sinebow.hs | 18 ++++++++++++++++++ examples/doc_turbo.hs | 18 ++++++++++++++++++ examples/doc_viridis.hs | 18 ++++++++++++++++++ reanimate.cabal | 2 +- src/Reanimate/Animation.hs | 7 +++++-- src/Reanimate/ColorMap.hs | 26 ++++++++++++++++++++++++++ stack-lts-11.yaml | 2 +- stack-lts-12.yaml | 2 +- stack.yaml | 2 +- stack.yaml.lock | 6 +++--- 20 files changed, 261 insertions(+), 10 deletions(-) create mode 100644 examples/doc_cividis.hs create mode 100644 examples/doc_greyscale.hs create mode 100644 examples/doc_hsv.hs create mode 100644 examples/doc_hsvMatlab.hs create mode 100644 examples/doc_inferno.hs create mode 100644 examples/doc_jet.hs create mode 100644 examples/doc_magma.hs create mode 100644 examples/doc_parula.hs create mode 100644 examples/doc_plasma.hs create mode 100644 examples/doc_sinebow.hs create mode 100644 examples/doc_turbo.hs create mode 100644 examples/doc_viridis.hs diff --git a/docs/gen_doc_gifs.sh b/docs/gen_doc_gifs.sh index 376b759..e6920bf 100755 --- a/docs/gen_doc_gifs.sh +++ b/docs/gen_doc_gifs.sh @@ -1,11 +1,17 @@ #!/usr/bin/env bash ROOT=`stack path --project-root` +TINY='^(doc_turbo|doc_viridis|doc_magma|doc_inferno|doc_plasma|doc_sinebow|doc_cividis|doc_jet|doc_hsv|doc_hsvMatlab|doc_greyscale|doc_parula)$' + for src in $ROOT/examples/doc_*.hs; do BASE=`basename $src .hs` DST=$ROOT/docs/gifs/$BASE.gif if [[ "$src" -nt "$DST" ]]; then echo stack $src render -o $DST - stack $src render -o $DST + if [[ "$BASE" =~ $TINY ]]; then + stack $src render -o $DST --width=320 --height=50 + else + stack $src render -o $DST + fi fi done diff --git a/examples/doc_cividis.hs b/examples/doc_cividis.hs new file mode 100644 index 0000000..53db6b6 --- /dev/null +++ b/examples/doc_cividis.hs @@ -0,0 +1,18 @@ +#!/usr/bin/env stack +-- stack runghc --package reanimate +module Main(main) where + +import Codec.Picture +import Reanimate +import Reanimate.Builtin.Documentation + +main :: IO () +main = reanimate $ docEnv $ animate $ const $ mkColorMap cividis + +mkColorMap :: (Double -> PixelRGB8) -> SVG +mkColorMap f = center $ scaleToSize screenWidth screenHeight $ embedImage img + where + width = 256 + height = 1 + img = generateImage pixelRenderer width height + pixelRenderer x _y = f (fromIntegral x / fromIntegral (width-1)) diff --git a/examples/doc_greyscale.hs b/examples/doc_greyscale.hs new file mode 100644 index 0000000..acf34c4 --- /dev/null +++ b/examples/doc_greyscale.hs @@ -0,0 +1,18 @@ +#!/usr/bin/env stack +-- stack runghc --package reanimate +module Main(main) where + +import Codec.Picture +import Reanimate +import Reanimate.Builtin.Documentation + +main :: IO () +main = reanimate $ docEnv $ animate $ const $ mkColorMap greyscale + +mkColorMap :: (Double -> PixelRGB8) -> SVG +mkColorMap f = center $ scaleToSize screenWidth screenHeight $ embedImage img + where + width = 256 + height = 1 + img = generateImage pixelRenderer width height + pixelRenderer x _y = f (fromIntegral x / fromIntegral (width-1)) diff --git a/examples/doc_hsv.hs b/examples/doc_hsv.hs new file mode 100644 index 0000000..b3dda43 --- /dev/null +++ b/examples/doc_hsv.hs @@ -0,0 +1,18 @@ +#!/usr/bin/env stack +-- stack runghc --package reanimate +module Main(main) where + +import Codec.Picture +import Reanimate +import Reanimate.Builtin.Documentation + +main :: IO () +main = reanimate $ docEnv $ animate $ const $ mkColorMap hsv + +mkColorMap :: (Double -> PixelRGB8) -> SVG +mkColorMap f = center $ scaleToSize screenWidth screenHeight $ embedImage img + where + width = 256 + height = 1 + img = generateImage pixelRenderer width height + pixelRenderer x _y = f (fromIntegral x / fromIntegral (width-1)) diff --git a/examples/doc_hsvMatlab.hs b/examples/doc_hsvMatlab.hs new file mode 100644 index 0000000..b642c0b --- /dev/null +++ b/examples/doc_hsvMatlab.hs @@ -0,0 +1,18 @@ +#!/usr/bin/env stack +-- stack runghc --package reanimate +module Main(main) where + +import Codec.Picture +import Reanimate +import Reanimate.Builtin.Documentation + +main :: IO () +main = reanimate $ docEnv $ animate $ const $ mkColorMap hsvMatlab + +mkColorMap :: (Double -> PixelRGB8) -> SVG +mkColorMap f = center $ scaleToSize screenWidth screenHeight $ embedImage img + where + width = 256 + height = 1 + img = generateImage pixelRenderer width height + pixelRenderer x _y = f (fromIntegral x / fromIntegral (width-1)) diff --git a/examples/doc_inferno.hs b/examples/doc_inferno.hs new file mode 100644 index 0000000..9c2bb16 --- /dev/null +++ b/examples/doc_inferno.hs @@ -0,0 +1,18 @@ +#!/usr/bin/env stack +-- stack runghc --package reanimate +module Main(main) where + +import Codec.Picture +import Reanimate +import Reanimate.Builtin.Documentation + +main :: IO () +main = reanimate $ docEnv $ animate $ const $ mkColorMap inferno + +mkColorMap :: (Double -> PixelRGB8) -> SVG +mkColorMap f = center $ scaleToSize screenWidth screenHeight $ embedImage img + where + width = 256 + height = 1 + img = generateImage pixelRenderer width height + pixelRenderer x _y = f (fromIntegral x / fromIntegral (width-1)) diff --git a/examples/doc_jet.hs b/examples/doc_jet.hs new file mode 100644 index 0000000..3ce28cc --- /dev/null +++ b/examples/doc_jet.hs @@ -0,0 +1,18 @@ +#!/usr/bin/env stack +-- stack runghc --package reanimate +module Main(main) where + +import Codec.Picture +import Reanimate +import Reanimate.Builtin.Documentation + +main :: IO () +main = reanimate $ docEnv $ animate $ const $ mkColorMap jet + +mkColorMap :: (Double -> PixelRGB8) -> SVG +mkColorMap f = center $ scaleToSize screenWidth screenHeight $ embedImage img + where + width = 256 + height = 1 + img = generateImage pixelRenderer width height + pixelRenderer x _y = f (fromIntegral x / fromIntegral (width-1)) diff --git a/examples/doc_magma.hs b/examples/doc_magma.hs new file mode 100644 index 0000000..b6e9747 --- /dev/null +++ b/examples/doc_magma.hs @@ -0,0 +1,18 @@ +#!/usr/bin/env stack +-- stack runghc --package reanimate +module Main(main) where + +import Codec.Picture +import Reanimate +import Reanimate.Builtin.Documentation + +main :: IO () +main = reanimate $ docEnv $ animate $ const $ mkColorMap magma + +mkColorMap :: (Double -> PixelRGB8) -> SVG +mkColorMap f = center $ scaleToSize screenWidth screenHeight $ embedImage img + where + width = 256 + height = 1 + img = generateImage pixelRenderer width height + pixelRenderer x _y = f (fromIntegral x / fromIntegral (width-1)) diff --git a/examples/doc_parula.hs b/examples/doc_parula.hs new file mode 100644 index 0000000..471aebc --- /dev/null +++ b/examples/doc_parula.hs @@ -0,0 +1,18 @@ +#!/usr/bin/env stack +-- stack runghc --package reanimate +module Main(main) where + +import Codec.Picture +import Reanimate +import Reanimate.Builtin.Documentation + +main :: IO () +main = reanimate $ docEnv $ animate $ const $ mkColorMap parula + +mkColorMap :: (Double -> PixelRGB8) -> SVG +mkColorMap f = center $ scaleToSize screenWidth screenHeight $ embedImage img + where + width = 256 + height = 1 + img = generateImage pixelRenderer width height + pixelRenderer x _y = f (fromIntegral x / fromIntegral (width-1)) diff --git a/examples/doc_plasma.hs b/examples/doc_plasma.hs new file mode 100644 index 0000000..94e691c --- /dev/null +++ b/examples/doc_plasma.hs @@ -0,0 +1,18 @@ +#!/usr/bin/env stack +-- stack runghc --package reanimate +module Main(main) where + +import Codec.Picture +import Reanimate +import Reanimate.Builtin.Documentation + +main :: IO () +main = reanimate $ docEnv $ animate $ const $ mkColorMap plasma + +mkColorMap :: (Double -> PixelRGB8) -> SVG +mkColorMap f = center $ scaleToSize screenWidth screenHeight $ embedImage img + where + width = 256 + height = 1 + img = generateImage pixelRenderer width height + pixelRenderer x _y = f (fromIntegral x / fromIntegral (width-1)) diff --git a/examples/doc_sinebow.hs b/examples/doc_sinebow.hs new file mode 100644 index 0000000..9f84558 --- /dev/null +++ b/examples/doc_sinebow.hs @@ -0,0 +1,18 @@ +#!/usr/bin/env stack +-- stack runghc --package reanimate +module Main(main) where + +import Codec.Picture +import Reanimate +import Reanimate.Builtin.Documentation + +main :: IO () +main = reanimate $ docEnv $ animate $ const $ mkColorMap sinebow + +mkColorMap :: (Double -> PixelRGB8) -> SVG +mkColorMap f = center $ scaleToSize screenWidth screenHeight $ embedImage img + where + width = 256 + height = 1 + img = generateImage pixelRenderer width height + pixelRenderer x _y = f (fromIntegral x / fromIntegral (width-1)) diff --git a/examples/doc_turbo.hs b/examples/doc_turbo.hs new file mode 100644 index 0000000..81783c8 --- /dev/null +++ b/examples/doc_turbo.hs @@ -0,0 +1,18 @@ +#!/usr/bin/env stack +-- stack runghc --package reanimate +module Main(main) where + +import Codec.Picture +import Reanimate +import Reanimate.Builtin.Documentation + +main :: IO () +main = reanimate $ docEnv $ animate $ const $ mkColorMap turbo + +mkColorMap :: (Double -> PixelRGB8) -> SVG +mkColorMap f = center $ scaleToSize screenWidth screenHeight $ embedImage img + where + width = 256 + height = 1 + img = generateImage pixelRenderer width height + pixelRenderer x _y = f (fromIntegral x / fromIntegral (width-1)) diff --git a/examples/doc_viridis.hs b/examples/doc_viridis.hs new file mode 100644 index 0000000..5154920 --- /dev/null +++ b/examples/doc_viridis.hs @@ -0,0 +1,18 @@ +#!/usr/bin/env stack +-- stack runghc --package reanimate +module Main(main) where + +import Codec.Picture +import Reanimate +import Reanimate.Builtin.Documentation + +main :: IO () +main = reanimate $ docEnv $ animate $ const $ mkColorMap viridis + +mkColorMap :: (Double -> PixelRGB8) -> SVG +mkColorMap f = center $ scaleToSize screenWidth screenHeight $ embedImage img + where + width = 256 + height = 1 + img = generateImage pixelRenderer width height + pixelRenderer x _y = f (fromIntegral x / fromIntegral (width-1)) diff --git a/reanimate.cabal b/reanimate.cabal index 6855025..1b603c8 100644 --- a/reanimate.cabal +++ b/reanimate.cabal @@ -76,7 +76,7 @@ library Paths_reanimate build-depends: base >=4.10 && <4.13, time, text, filepath, process, directory, - containers, reanimate-svg >= 0.9.2.1, xml, bytestring, lens, linear, mtl, matrix, + containers, reanimate-svg >= 0.9.3.0, xml, bytestring, lens, linear, mtl, matrix, JuicyPixels, attoparsec, parallel, diagrams, diagrams-svg, diagrams-core, diagrams-lib, diagrams-contrib, svg-builder, cubicbezier, palette, websockets, diff --git a/src/Reanimate/Animation.hs b/src/Reanimate/Animation.hs index 400f21d..ba29fcc 100644 --- a/src/Reanimate/Animation.hs +++ b/src/Reanimate/Animation.hs @@ -3,12 +3,14 @@ module Reanimate.Animation where import Control.Arrow () import Data.Fixed (mod') import qualified Data.Map as M -import Graphics.SvgTree (Document (..), Number (..), +import Graphics.SvgTree (Alignment (..), Document (..), + Number (..), + PreserveAspectRatio (..), Tree (..), xmlOfTree) import Graphics.SvgTree.Printer import Reanimate.Constants -import Reanimate.Svg.Constructors import Reanimate.Signal +import Reanimate.Svg.Constructors import Text.XML.Light.Output -- | Duration of an animation or effect. Usually measured in seconds. @@ -149,6 +151,7 @@ renderSvg w h t = ppDocument doc , _definitions = M.empty , _description = "" , _documentLocation = "" + , _documentAspectRatio = PreserveAspectRatio False AlignNone Nothing } -- | Map over the SVG produced by an animation at every frame. diff --git a/src/Reanimate/ColorMap.hs b/src/Reanimate/ColorMap.hs index 8a375da..073c2bf 100644 --- a/src/Reanimate/ColorMap.hs +++ b/src/Reanimate/ColorMap.hs @@ -26,6 +26,8 @@ import Data.Colour.RGBSpace -- | Given a number t in the range [0,1], returns the corresponding color from -- the “turbo” color scheme by Anton Mikhailov. +-- +-- <> turbo :: Double -> PixelRGB8 turbo t = PixelRGB8 red green blue where @@ -38,6 +40,8 @@ turbo t = PixelRGB8 red green blue -- | Given a number t in the range [0,1], returns the corresponding color from -- the “viridis” perceptually-uniform color scheme designed by van der Walt, -- Smith and Firing for matplotlib, represented as an RGB string. +-- +-- <> viridis :: Double -> PixelRGB8 viridis = ramp (colors "44015444025645045745055946075a46085c460a5d460b5e470d60470e614710634711644713\ @@ -65,6 +69,8 @@ viridis = ramp (colors -- | Given a number t in the range [0,1], returns the corresponding color from -- the “magma” perceptually-uniform color scheme designed by van der Walt and -- Smith for matplotlib, represented as an RGB string. +-- +-- <> magma :: Double -> PixelRGB8 magma = ramp (colors "00000401000501010601010802010902020b02020d03030f0303120404140504160605180605\ @@ -92,6 +98,8 @@ magma = ramp (colors -- | Given a number t in the range [0,1], returns the corresponding color from -- the “inferno” perceptually-uniform color scheme designed by van der Walt -- and Smith for matplotlib, represented as an RGB string. +-- +-- <> inferno :: Double -> PixelRGB8 inferno = ramp (colors "00000401000501010601010802010a02020c02020e0302100403120403140504170604190705\ @@ -119,6 +127,8 @@ inferno = ramp (colors -- | Given a number t in the range [0,1], returns the corresponding color from -- the “plasma” perceptually-uniform color scheme designed by van der Walt and -- Smith for matplotlib, represented as an RGB string. +-- +-- <> plasma :: Double -> PixelRGB8 plasma = ramp (colors "0d088710078813078916078a19068c1b068d1d068e20068f2206902406912605912805922a05\ @@ -145,6 +155,8 @@ plasma = ramp (colors -- | Given a number t in the range [0,1], returns the corresponding color from -- the “sinebow” color scheme by Jim Bumgardner and Charlie Loyd. +-- +-- <> sinebow :: Double -> PixelRGB8 sinebow t = PixelRGB8 r g b where @@ -158,6 +170,8 @@ sinebow t = PixelRGB8 r g b -- | Given a number t in the range [0,1], returns the corresponding color from -- the “cividis” color vision deficiency-optimized color scheme designed by -- Nuñez, Anderton, and Renslow, represented as an RGB string. +-- +-- <> cividis :: Double -> PixelRGB8 cividis t = PixelRGB8 red green blue where @@ -168,6 +182,8 @@ cividis t = PixelRGB8 red green blue trunc = fromIntegral . min 255 . max 0 -- | Jet colormap. Used to be the default in matlab. Obsolete. +-- +-- <> jet :: Double -> PixelRGB8 jet t = PixelRGB8 red green blue where @@ -178,22 +194,32 @@ jet t = PixelRGB8 red green blue trunc = round . min 255 . max 0 . (*) 255 -- | hsv colormap. Goes from 0 degrees to 360 degrees. +-- +-- <> hsv :: Double -> PixelRGB8 hsv t = PixelRGB8 (round $ r*255) (round $ g*255) (round $ b*255) where RGB r g b = HSV.hsv (t * 360) 1 1 -- | Matlab hsv colormap. Goes from 0 degrees to 330 degrees. +-- +-- <> hsvMatlab :: Double -> PixelRGB8 hsvMatlab t = PixelRGB8 (round $ r*255) (round $ g*255) (round $ b*255) where RGB r g b = HSV.hsv (t * 330) 1 1 +-- | Greyscale colormap. +-- +-- <> greyscale :: Double -> PixelRGB8 greyscale t = PixelRGB8 v v v where v = round $ t * 255 +-- | Parula is the default colormap for matlab. +-- +-- <> parula :: Double -> PixelRGB8 parula = ramp vec where diff --git a/stack-lts-11.yaml b/stack-lts-11.yaml index 8b13c22..1a9ca3b 100644 --- a/stack-lts-11.yaml +++ b/stack-lts-11.yaml @@ -6,7 +6,7 @@ packages: - . extra-deps: -- reanimate-svg-0.9.2.1 +- reanimate-svg-0.9.3.0 - palette-0.3.0.2 - git: https://github.com/Lemmih/chiphunk.git commit: 8328b1adb903abf034562ad95d1bfdaba1ce6397 diff --git a/stack-lts-12.yaml b/stack-lts-12.yaml index 02d3887..e458f94 100644 --- a/stack-lts-12.yaml +++ b/stack-lts-12.yaml @@ -6,7 +6,7 @@ packages: - . extra-deps: -- reanimate-svg-0.9.2.1 +- reanimate-svg-0.9.3.0 - palette-0.3.0.2 - git: https://github.com/Lemmih/chiphunk.git commit: 8328b1adb903abf034562ad95d1bfdaba1ce6397 diff --git a/stack.yaml b/stack.yaml index 8d12679..ac12d48 100644 --- a/stack.yaml +++ b/stack.yaml @@ -6,7 +6,7 @@ packages: - . extra-deps: -- reanimate-svg-0.9.2.1 +- reanimate-svg-0.9.3.0 - palette-0.3.0.2 - git: https://github.com/Lemmih/chiphunk.git commit: 8328b1adb903abf034562ad95d1bfdaba1ce6397 diff --git a/stack.yaml.lock b/stack.yaml.lock index 1ecb6d3..b305b6c 100644 --- a/stack.yaml.lock +++ b/stack.yaml.lock @@ -5,12 +5,12 @@ packages: - completed: - hackage: reanimate-svg-0.9.2.1@sha256:4af574c329cd81d5daab016cdc0e340a311367cf218db6e342b0db7e641b4d62,2448 + hackage: reanimate-svg-0.9.3.0@sha256:ba0414bce09a0f298b1ab5e1f0f9f75f4f47498e12dd0c6a75c2e63f33e34ca4,2448 pantry-tree: size: 1117 - sha256: e28f412e94b02a113e872dc4f0b487231870598acb6e194c85268c8d0b53e376 + sha256: 1b559678d25e1fa98c75fad0315851c4c805f3cb6f2779ab4be7eb6868b4838e original: - hackage: reanimate-svg-0.9.2.1 + hackage: reanimate-svg-0.9.3.0 - completed: hackage: palette-0.3.0.2@sha256:50e210b7d21a0c394a6d672cc3c6d3bffcfa1be07419d72b99e68cfb65d6ca3c,1485 pantry-tree: