module Graphics.Curves.Style
( CurveAttribute(..)
, Style
, lineStyle, fillStyle, brushStyle
, gradient, dashedOpen, dashedClosed, dashed
) where
import Graphics.Curves.Math
import Graphics.Curves.Image
import Graphics.Curves.Colour
import Graphics.Curves.Curve
import Graphics.Curves.Attribute
import Debug.Trace
type Style = [Assignment Image]
lineStyle :: Scalar -> Scalar -> Colour -> Style
lineStyle w b c = [LineWidth := w, LineBlur := b, LineColour := c]
fillStyle :: Scalar -> Colour -> Style
fillStyle b c = [FillColour := c, FillBlur := b]
brushStyle :: Scalar -> Scalar -> Style
brushStyle w d =
[VarLineWidth := \x r _ ->
let total = x/r in
if | r == 0 -> 0
| x < d -> w * f (2 * x / d 1)
| x > total d -> w * f (2 * (total x) / d 1)
| otherwise -> w
]
where
f t = (1 + t + sin (pi * t) / pi) / 2
modDouble a b = a b * fromIntegral (floor (a / b))
gradient :: Colour -> Colour -> Scalar -> Style
gradient c1 c2 a =
[VarLineColour := \d _ _ ->
case modDouble d (2 * a) of
x | x <= a -> blend (setAlpha (x / a) c2) c1
| otherwise -> blend (setAlpha ((2 * a x) / a) c2) c1
]
dashedOpen :: Scalar -> Scalar -> Style
dashedOpen a b =
dashed a b ++
[VarLineColour :~ \old d r p ->
if | r == 0 -> old d r p
| otherwise ->
let total = d/r
n = round (total / (a + b))
k = total / (fromIntegral n * (a + b) + a)
in old (d / k) r p
]
dashedClosed :: Scalar -> Scalar -> Style
dashedClosed a b =
dashed a b ++
[VarLineColour :~ \old d r p ->
if | r == 0 -> old d r p
| otherwise ->
let total = d/r
n = round (total / (a + b))
k = total / (fromIntegral n * (a + b))
in old (d / k) r p
]
dashed :: Scalar -> Scalar -> Style
dashed a b =
[VarLineColour :~ \old d r p ->
case modDouble d (a + b) of
x | x <= a -> old d r p
| otherwise -> transparent
]