Skip to content

Commit

Permalink
new attributes for separate fill & stroke opacity
Browse files Browse the repository at this point in the history
See #248.
  • Loading branch information
byorgey committed Oct 5, 2015
1 parent 774265b commit 3c31b38
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/Diagrams/Attributes.hs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ module Diagrams.Attributes (
, Opacity, _Opacity
, getOpacity, opacity, _opacity

, FillOpacity, _FillOpacity
, getFillOpacity, fillOpacity, _fillOpacity

, StrokeOpacity, _StrokeOpacity
, getStrokeOpacity, strokeOpacity, _strokeOpacity

-- ** Converting colors
, colorToSRGBA, colorToRGBA

Expand Down Expand Up @@ -345,6 +351,58 @@ opacity = applyAttr . Opacity . Product
_opacity :: Lens' (Style v n) Double
_opacity = atAttr . mapping _Opacity . non 1

-- fill opacity --------------------------------------------------------

-- | Like 'Opacity', but set the opacity only for fills (as opposed to strokes).
-- As with 'Opacity', the fill opacity is a value between 1
-- (completely opaque, the default) and 0 (completely transparent),
-- and is multiplicative.
newtype FillOpacity = FillOpacity (Product Double)
deriving (Typeable, Semigroup)
instance AttributeClass FillOpacity

_FillOpacity :: Iso' FillOpacity Double
_FillOpacity = iso getFillOpacity (FillOpacity . Product)

getFillOpacity :: FillOpacity -> Double
getFillOpacity (FillOpacity (Product d)) = d

-- | Multiply the fill opacity (see 'FillOpacity') by the given value. For
-- example, @fillOpacity 0.8@ means \"decrease this diagram's fill opacity to
-- 80% of its previous value\".
fillOpacity :: HasStyle a => Double -> a -> a
fillOpacity = applyAttr . FillOpacity . Product

-- | Lens onto the fill opacity in a style.
_fillOpacity :: Lens' (Style v n) Double
_fillOpacity = atAttr . mapping _FillOpacity . non 1

-- stroke opacity --------------------------------------------------------

-- | Like 'Opacity', but set the opacity only for strokes (as opposed to fills).
-- As with 'Opacity', the fill opacity is a value between 1
-- (completely opaque, the default) and 0 (completely transparent),
-- and is multiplicative.
newtype StrokeOpacity = StrokeOpacity (Product Double)
deriving (Typeable, Semigroup)
instance AttributeClass StrokeOpacity

_StrokeOpacity :: Iso' StrokeOpacity Double
_StrokeOpacity = iso getStrokeOpacity (StrokeOpacity . Product)

getStrokeOpacity :: StrokeOpacity -> Double
getStrokeOpacity (StrokeOpacity (Product d)) = d

-- | Multiply the stroke opacity (see 'StrokeOpacity') by the given value. For
-- example, @strokeOpacity 0.8@ means \"decrease this diagram's
-- stroke opacity to 80% of its previous value\".
strokeOpacity :: HasStyle a => Double -> a -> a
strokeOpacity = applyAttr . StrokeOpacity . Product

-- | Lens onto the stroke opacity in a style.
_strokeOpacity :: Lens' (Style v n) Double
_strokeOpacity = atAttr . mapping _StrokeOpacity . non 1

------------------------------------------------------------------------
-- Line stuff
------------------------------------------------------------------------
Expand Down

0 comments on commit 3c31b38

Please sign in to comment.