Debian Codegraph @

src/Diagrams/TwoD/Attributes.hs

raw symbols SCIP
1 {-# LANGUAGE ConstraintKinds #-}
2 {-# LANGUAGE DeriveDataTypeable #-}
3 {-# LANGUAGE DeriveFunctor #-}
4 {-# LANGUAGE FlexibleContexts #-}
5 {-# LANGUAGE FlexibleInstances #-}
6 {-# LANGUAGE GeneralizedNewtypeDeriving #-}
7 {-# LANGUAGE MultiParamTypeClasses #-}
8 {-# LANGUAGE ScopedTypeVariables #-}
9 {-# LANGUAGE StandaloneDeriving #-}
10 {-# LANGUAGE TemplateHaskell #-}
11 {-# LANGUAGE TypeFamilies #-}
12 {-# LANGUAGE UndecidableInstances #-}
13
14 -----------------------------------------------------------------------------
15 -- |
16 -- Module : Diagrams.TwoD.Attributes
17 -- Copyright : (c) 2013-2015 diagrams-lib team (see LICENSE)
18 -- License : BSD-style (see LICENSE)
19 -- Maintainer : [email protected]
20 --
21 -- Diagrams may have /attributes/ which affect the way they are
22 -- rendered. This module defines /Textures/ (Gradients and Colors) in two
23 -- dimensions. Like the attributes defined in the Diagrams.Attributes module,
24 -- all attributes defined here use the 'Last' or 'Recommend' /semigroup/ structure.
25 -- 'FillColor' and 'LineColor' attributes are provided so that backends that
26 -- don't support gradients need not be concerned with using textures. Backends
27 -- should only implement color attributes or textures attributes, not both.
28 --
29 -----------------------------------------------------------------------------
30
31 module Diagrams.TwoD.Attributes (
32 -- * Textures
33 Texture(..), solid, _SC, _AC, _LG, _RG, defaultLG, defaultRG
34 , GradientStop(..), stopColor, stopFraction, mkStops
35 , SpreadMethod(..), lineLGradient, lineRGradient
36
37 -- ** Linear Gradients
38 , LGradient(..), lGradStops, lGradTrans, lGradStart, lGradEnd
39 , lGradSpreadMethod, mkLinearGradient
40
41 -- ** Radial Gradients
42 , RGradient(..), rGradStops, rGradTrans
43 , rGradCenter0, rGradRadius0, rGradCenter1, rGradRadius1
44 , rGradSpreadMethod, mkRadialGradient
45
46 -- ** Line texture
47 , LineTexture(..), _LineTexture, getLineTexture, lineTexture, lineTextureA
48 , mkLineTexture, _lineTexture
49
50 -- ** Line color
51 , lineColor, lc, lcA
52
53 -- ** Fill texture
54 , FillTexture(..), _FillTexture, getFillTexture, fillTexture
55 , mkFillTexture, _fillTexture, _fillTextureR
56
57 -- ** Fill color
58 , fillColor, fc, fcA, recommendFillColor
59
60 -- * Compilation utilities
61 , splitTextureFills
62
63 ) where
64
65 import Control.Lens hiding (transform)
66 import Data.Colour hiding (AffineSpace, over)
67 import Data.Data
68 import Data.Default
69 import Data.Monoid.Recommend
70 import Data.Semigroup
71
72 import Diagrams.Attributes
73 import Diagrams.Attributes.Compile
74 import Diagrams.Core
75 import Diagrams.Core.Types (RTree)
76 import Diagrams.Located (unLoc)
77 import Diagrams.Path (Path, pathTrails)
78 import Diagrams.Trail (isLoop)
79 import Diagrams.TwoD.Types
80 import Diagrams.Util
81
82
83 -----------------------------------------------------------------
84 -- Gradients --------------------------------------------------
85 -----------------------------------------------------------------
86
87 -- | A gradient stop contains a color and fraction (usually between 0 and 1)
88 data GradientStop d = GradientStop
89 { _stopColor :: SomeColor
90 , _stopFraction :: d
91 }
92
93 makeLensesWith (lensRules & generateSignatures .~ False) ''GradientStop
94
95 -- | A color for the stop.
96 stopColor :: Lens' (GradientStop n) SomeColor
97
98 -- | The fraction for stop.
99 stopFraction :: Lens' (GradientStop n) n
100
101 -- | The 'SpreadMethod' determines what happens before 'lGradStart' and after
102 -- 'lGradEnd'. 'GradPad' fills the space before the start of the gradient
103 -- with the color of the first stop and the color after end of the gradient
104 -- with the color of the last stop. 'GradRepeat' restarts the gradient and
105 -- 'GradReflect' restarts the gradient with the stops in reverse order.
106 data SpreadMethod = GradPad | GradReflect | GradRepeat
107
108 -- | Linear Gradient
109 data LGradient n = LGradient
110 { _lGradStops :: [GradientStop n]
111 , _lGradStart :: Point V2 n
112 , _lGradEnd :: Point V2 n
113 , _lGradTrans :: Transformation V2 n
114 , _lGradSpreadMethod :: SpreadMethod }
115
116 type instance V (LGradient n) = V2
117 type instance N (LGradient n) = n
118
119 makeLensesWith (lensRules & generateSignatures .~ False) ''LGradient
120
121 instance Fractional n => Transformable (LGradient n) where
122 transform = over lGradTrans . transform
123
124 -- | A list of stops (colors and fractions).
125 lGradStops :: Lens' (LGradient n) [GradientStop n]
126
127 -- | A transformation to be applied to the gradient. Usually this field will
128 -- start as the identity transform and capture the transforms that are applied
129 -- to the gradient.
130 lGradTrans :: Lens' (LGradient n) (Transformation V2 n)
131
132 -- | The starting point for the first gradient stop. The coordinates are in
133 -- 'local' units and the default is (-0.5, 0).
134 lGradStart :: Lens' (LGradient n) (Point V2 n)
135
136 -- | The ending point for the last gradient stop.The coordinates are in
137 -- 'local' units and the default is (0.5, 0).
138 lGradEnd :: Lens' (LGradient n) (Point V2 n)
139
140 -- | For setting the spread method.
141 lGradSpreadMethod :: Lens' (LGradient n) SpreadMethod
142
143 -- | Radial Gradient
144 data RGradient n = RGradient
145 { _rGradStops :: [GradientStop n]
146 , _rGradCenter0 :: Point V2 n
147 , _rGradRadius0 :: n
148 , _rGradCenter1 :: Point V2 n
149 , _rGradRadius1 :: n
150 , _rGradTrans :: Transformation V2 n
151 , _rGradSpreadMethod :: SpreadMethod }
152
153 makeLensesWith (lensRules & generateSignatures .~ False) ''RGradient
154
155 type instance V (RGradient n) = V2
156 type instance N (RGradient n) = n
157
158 instance Fractional n => Transformable (RGradient n) where
159 transform = over rGradTrans . transform
160
161 -- | A list of stops (colors and fractions).
162 rGradStops :: Lens' (RGradient n) [GradientStop n]
163
164 -- | The center point of the inner circle.
165 rGradCenter0 :: Lens' (RGradient n) (Point V2 n)
166
167 -- | The radius of the inner cirlce in 'local' coordinates.
168 rGradRadius0 :: Lens' (RGradient n) n
169
170 -- | The center of the outer circle.
171 rGradCenter1 :: Lens' (RGradient n) (Point V2 n)
172
173 -- | The radius of the outer circle in 'local' coordinates.
174 rGradRadius1 :: Lens' (RGradient n) n
175
176 -- | A transformation to be applied to the gradient. Usually this field will
177 -- start as the identity transform and capture the transforms that are applied
178 -- to the gradient.
179 rGradTrans :: Lens' (RGradient n) (Transformation V2 n)
180
181 -- | For setting the spread method.
182 rGradSpreadMethod :: Lens' (RGradient n) SpreadMethod
183
184 -----------------------------------------------------------------
185 -- Textures ---------------------------------------------------
186 -----------------------------------------------------------------
187
188 -- | A Texture is either a color 'SC', linear gradient 'LG', or radial gradient 'RG'.
189 -- An object can have only one texture which is determined by the 'Last'
190 -- semigroup structure.
191 data Texture n = SC SomeColor | LG (LGradient n) | RG (RGradient n)
192 deriving Typeable
193
194 type instance V (Texture n) = V2
195 type instance N (Texture n) = n
196
197 makePrisms ''Texture
198
199 -- | Prism onto an 'AlphaColour' 'Double' of a 'SC' texture.
200 _AC :: Prism' (Texture n) (AlphaColour Double)
201 _AC = _SC . _SomeColor
202
203 instance Floating n => Transformable (Texture n) where
204 transform t (LG lg) = LG $ transform t lg
205 transform t (RG rg) = RG $ transform t rg
206 transform _ sc = sc
207
208 -- | Convert a solid colour into a texture.
209 solid :: Color a => a -> Texture n
210 solid = SC . SomeColor
211
212 -- | A default is provided so that linear gradients can easily be created using
213 -- lenses. For example, @lg = defaultLG & lGradStart .~ (0.25 ^& 0.33)@. Note that
214 -- no default value is provided for @lGradStops@, this must be set before
215 -- the gradient value is used, otherwise the object will appear transparent.
216 defaultLG :: Fractional n => Texture n
217 defaultLG = LG LGradient
218 { _lGradStops = []
219 , _lGradStart = mkP2 (-0.5) 0
220 , _lGradEnd = mkP2 0.5 0
221 , _lGradTrans = mempty
222 , _lGradSpreadMethod = GradPad
223 }
224
225 -- | A default is provided so that radial gradients can easily be created using
226 -- lenses. For example, @rg = defaultRG & rGradRadius1 .~ 0.25@. Note that
227 -- no default value is provided for @rGradStops@, this must be set before
228 -- the gradient value is used, otherwise the object will appear transparent.
229 defaultRG :: Fractional n => Texture n
230 defaultRG = RG RGradient
231 { _rGradStops = []
232 , _rGradCenter0 = mkP2 0 0
233 , _rGradRadius0 = 0.0
234 , _rGradCenter1 = mkP2 0 0
235 , _rGradRadius1 = 0.5
236 , _rGradTrans = mempty
237 , _rGradSpreadMethod = GradPad
238 }
239
240 -- | A convenient function for making gradient stops from a list of triples.
241 -- (An opaque color, a stop fraction, an opacity).
242 mkStops :: [(Colour Double, d, Double)] -> [GradientStop d]
243 mkStops = map (\(x, y, z) -> GradientStop (SomeColor (withOpacity x z)) y)
244
245 -- | Make a linear gradient texture from a stop list, start point, end point,
246 -- and 'SpreadMethod'. The 'lGradTrans' field is set to the identity
247 -- transfrom, to change it use the 'lGradTrans' lens.
248 mkLinearGradient :: Num n => [GradientStop n] -> Point V2 n -> Point V2 n -> SpreadMethod -> Texture n
249 mkLinearGradient stops start end spreadMethod
250 = LG (LGradient stops start end mempty spreadMethod)
251
252 -- | Make a radial gradient texture from a stop list, radius, start point,
253 -- end point, and 'SpreadMethod'. The 'rGradTrans' field is set to the identity
254 -- transfrom, to change it use the 'rGradTrans' lens.
255 mkRadialGradient :: Num n => [GradientStop n] -> Point V2 n -> n
256 -> Point V2 n -> n -> SpreadMethod -> Texture n
257 mkRadialGradient stops c0 r0 c1 r1 spreadMethod
258 = RG (RGradient stops c0 r0 c1 r1 mempty spreadMethod)
259
260 -- Line Texture --------------------------------------------------------
261
262 -- | The texture with which lines are drawn. Note that child
263 -- textures always override parent textures.
264 -- More precisely, the semigroup structure on line texture attributes
265 -- is that of 'Last'.
266 newtype LineTexture n = LineTexture (Last (Texture n))
267 deriving (Typeable, Semigroup)
268 instance (Typeable n) => AttributeClass (LineTexture n)
269
270 type instance V (LineTexture n) = V2
271 type instance N (LineTexture n) = n
272
273 _LineTexture :: Iso (LineTexture n) (LineTexture n')
274 (Texture n) (Texture n')
275 _LineTexture = iso getLineTexture (LineTexture . Last)
276
277 -- Only gradients get transformed. The transform is applied to the gradients
278 -- transform field. Colors are left unchanged.
279 instance Floating n => Transformable (LineTexture n) where
280 transform t (LineTexture (Last tx)) = LineTexture (Last $ transform t tx)
281
282 instance Default (LineTexture n) where
283 def = _LineTexture . _SC ## SomeColor black
284
285 mkLineTexture :: Texture n -> LineTexture n
286 mkLineTexture = LineTexture . Last
287
288 getLineTexture :: LineTexture n -> Texture n
289 getLineTexture (LineTexture (Last t)) = t
290
291 lineTexture :: (InSpace V2 n a, Typeable n, Floating n, HasStyle a) => Texture n -> a -> a
292 lineTexture = applyTAttr . LineTexture . Last
293
294 lineTextureA :: (InSpace V2 n a, Typeable n, Floating n, HasStyle a) => LineTexture n -> a -> a
295 lineTextureA = applyTAttr
296
297 _lineTexture :: (Floating n, Typeable n) => Lens' (Style V2 n) (Texture n)
298 _lineTexture = atTAttr . anon def isDef . _LineTexture
299 where
300 isDef = anyOf (_LineTexture . _AC) (== opaque black)
301
302 -- | Set the line (stroke) color. This function is polymorphic in the
303 -- color type (so it can be used with either 'Colour' or
304 -- 'AlphaColour'), but this can sometimes create problems for type
305 -- inference, so the 'lc' and 'lcA' variants are provided with more
306 -- concrete types.
307 lineColor :: (InSpace V2 n a, Color c, Typeable n, Floating n, HasStyle a) => c -> a -> a
308 lineColor = lineTexture . SC . SomeColor
309
310 -- | A synonym for 'lineColor', specialized to @'Colour' Double@
311 -- (i.e. opaque colors). See comment in 'lineColor' about backends.
312 lc :: (InSpace V2 n a, Typeable n, Floating n, HasStyle a) => Colour Double -> a -> a
313 lc = lineColor
314
315 -- | A synonym for 'lineColor', specialized to @'AlphaColour' Double@
316 -- (i.e. colors with transparency). See comment in 'lineColor'
317 -- about backends.
318 lcA :: (InSpace V2 n a, Typeable n, Floating n, HasStyle a) => AlphaColour Double -> a -> a
319 lcA = lineColor
320
321 -- | Apply a linear gradient.
322 lineLGradient :: (InSpace V2 n a, Typeable n, Floating n, HasStyle a) => LGradient n -> a -> a
323 lineLGradient g = lineTexture (LG g)
324
325 -- | Apply a radial gradient.
326 lineRGradient :: (InSpace V2 n a, Typeable n, Floating n, HasStyle a) => RGradient n -> a -> a
327 lineRGradient g = lineTexture (RG g)
328
329 -- Fill Texture --------------------------------------------------------
330
331 -- | The texture with which objects are filled.
332 -- The semigroup structure on fill texture attributes
333 -- is that of 'Recommed . Last'.
334 newtype FillTexture n = FillTexture (Recommend (Last (Texture n)))
335 deriving (Typeable, Semigroup)
336
337 instance Typeable n => AttributeClass (FillTexture n)
338
339 _FillTexture :: Iso' (FillTexture n) (Recommend (Texture n))
340 _FillTexture = iso getter setter
341 where
342 getter (FillTexture (Recommend (Last t))) = Recommend t
343 getter (FillTexture (Commit (Last t))) = Commit t
344 setter (Recommend t) = FillTexture (Recommend (Last t))
345 setter (Commit t) = FillTexture (Commit (Last t))
346 -- = iso (\(FillTexture a) -> a) FillTexture . mapping _Wrapped
347 -- -- once we depend on monoid-extras-0.4
348
349 type instance V (FillTexture n) = V2
350 type instance N (FillTexture n) = n
351
352 -- Only gradients get transformed. The transform is applied to the gradients
353 -- transform field. Colors are left unchanged.
354 instance Floating n => Transformable (FillTexture n) where
355 transform = over (_FillTexture . _recommend) . transform
356
357 instance Default (FillTexture n) where
358 def = mkFillTexture $ _AC ## transparent
359
360 getFillTexture :: FillTexture n -> Texture n
361 getFillTexture (FillTexture tx) = getLast . getRecommend $ tx
362
363 fillTexture :: (InSpace V2 n a, Typeable n, Floating n, HasStyle a) => Texture n -> a -> a
364 fillTexture = applyTAttr . mkFillTexture
365
366 mkFillTexture :: Texture n -> FillTexture n
367 mkFillTexture = FillTexture . Commit . Last
368
369 -- | Lens onto the 'Recommend' of a fill texture in a style.
370 _fillTextureR :: (Typeable n, Floating n) => Lens' (Style V2 n) (Recommend (Texture n))
371 _fillTextureR = atTAttr . anon def isDef . _FillTexture
372 where
373 isDef = anyOf (_FillTexture . _Recommend . _AC) (== transparent)
374
375 -- | Commit a fill texture in a style. This is /not/ a valid setter
376 -- because it doesn't abide the functor law (see 'committed').
377 _fillTexture :: (Typeable n, Floating n) => Lens' (Style V2 n) (Texture n)
378 _fillTexture = _fillTextureR . committed
379
380 -- | Set the fill color. This function is polymorphic in the color
381 -- type (so it can be used with either 'Colour' or 'AlphaColour'),
382 -- but this can sometimes create problems for type inference, so the
383 -- 'fc' and 'fcA' variants are provided with more concrete types.
384 fillColor :: (InSpace V2 n a, Color c, Typeable n, Floating n, HasStyle a) => c -> a -> a
385 fillColor = fillTexture . SC . SomeColor
386
387 -- | Set a \"recommended\" fill color, to be used only if no explicit
388 -- calls to 'fillColor' (or 'fc', or 'fcA') are used.
389 -- See comment after 'fillColor' about backends.
390 recommendFillColor :: (InSpace V2 n a, Color c, Typeable n, Floating n, HasStyle a) => c -> a -> a
391 recommendFillColor =
392 applyTAttr . FillTexture . Recommend . Last . SC . SomeColor
393
394 -- | A synonym for 'fillColor', specialized to @'Colour' Double@
395 -- (i.e. opaque colors). See comment after 'fillColor' about backends.
396 fc :: (InSpace V2 n a, Floating n, Typeable n, HasStyle a) => Colour Double -> a -> a
397 fc = fillColor
398
399 -- | A synonym for 'fillColor', specialized to @'AlphaColour' Double@
400 -- (i.e. colors with transparency). See comment after 'fillColor' about backends.
401 fcA :: (InSpace V2 n a, Floating n, Typeable n, HasStyle a) => AlphaColour Double -> a -> a
402 fcA = fillColor
403
404 -- Split fills ---------------------------------------------------------
405
406 data FillTextureLoops n = FillTextureLoops
407
408 instance Typeable n => SplitAttribute (FillTextureLoops n) where
409 type AttrType (FillTextureLoops n) = FillTexture n
410 type PrimType (FillTextureLoops n) = Path V2 n
411
412 primOK _ = all (isLoop . unLoc) . pathTrails
413
414 -- | Push fill attributes down until they are at the root of subtrees
415 -- containing only loops. This makes life much easier for backends,
416 -- which typically have a semantics where fill attributes are
417 -- applied to lines/non-closed paths as well as loops/closed paths,
418 -- whereas in the semantics of diagrams, fill attributes only apply
419 -- to loops.
420 splitTextureFills
421 :: forall b v n a. (
422 Typeable n) => RTree b v n a -> RTree b v n a
423 splitTextureFills = splitAttr (FillTextureLoops :: FillTextureLoops n)