| 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 |
|
| 88 |
data GradientStop d = GradientStop |
| 89 |
{ _stopColor :: SomeColor |
| 90 |
, _stopFraction :: d |
| 91 |
} |
| 92 |
|
| 93 |
makeLensesWith (lensRules & generateSignatures .~ False) ''GradientStop |
| 94 |
|
| 95 |
|
| 96 |
stopColor :: Lens' (GradientStop n) SomeColor |
| 97 |
|
| 98 |
|
| 99 |
stopFraction :: Lens' (GradientStop n) n |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
data SpreadMethod = GradPad | GradReflect | GradRepeat |
| 107 |
|
| 108 |
|
| 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 |
|
| 125 |
lGradStops :: Lens' (LGradient n) [GradientStop n] |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
lGradTrans :: Lens' (LGradient n) (Transformation V2 n) |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
lGradStart :: Lens' (LGradient n) (Point V2 n) |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
lGradEnd :: Lens' (LGradient n) (Point V2 n) |
| 139 |
|
| 140 |
|
| 141 |
lGradSpreadMethod :: Lens' (LGradient n) SpreadMethod |
| 142 |
|
| 143 |
|
| 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 |
|
| 162 |
rGradStops :: Lens' (RGradient n) [GradientStop n] |
| 163 |
|
| 164 |
|
| 165 |
rGradCenter0 :: Lens' (RGradient n) (Point V2 n) |
| 166 |
|
| 167 |
|
| 168 |
rGradRadius0 :: Lens' (RGradient n) n |
| 169 |
|
| 170 |
|
| 171 |
rGradCenter1 :: Lens' (RGradient n) (Point V2 n) |
| 172 |
|
| 173 |
|
| 174 |
rGradRadius1 :: Lens' (RGradient n) n |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
rGradTrans :: Lens' (RGradient n) (Transformation V2 n) |
| 180 |
|
| 181 |
|
| 182 |
rGradSpreadMethod :: Lens' (RGradient n) SpreadMethod |
| 183 |
|
| 184 |
----------------------------------------------------------------- |
| 185 |
-- Textures --------------------------------------------------- |
| 186 |
----------------------------------------------------------------- |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 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 |
|
| 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 |
|
| 209 |
solid :: Color a => a -> Texture n |
| 210 |
solid = SC . SomeColor |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 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 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 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 |
|
| 241 |
|
| 242 |
mkStops :: [(Colour Double, d, Double)] -> [GradientStop d] |
| 243 |
mkStops = map (\(x, y, z) -> GradientStop (SomeColor (withOpacity x z)) y) |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 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 |
|
| 253 |
|
| 254 |
|
| 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 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 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 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
lineColor :: (InSpace V2 n a, Color c, Typeable n, Floating n, HasStyle a) => c -> a -> a |
| 308 |
lineColor = lineTexture . SC . SomeColor |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
lc :: (InSpace V2 n a, Typeable n, Floating n, HasStyle a) => Colour Double -> a -> a |
| 313 |
lc = lineColor |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
lcA :: (InSpace V2 n a, Typeable n, Floating n, HasStyle a) => AlphaColour Double -> a -> a |
| 319 |
lcA = lineColor |
| 320 |
|
| 321 |
|
| 322 |
lineLGradient :: (InSpace V2 n a, Typeable n, Floating n, HasStyle a) => LGradient n -> a -> a |
| 323 |
lineLGradient g = lineTexture (LG g) |
| 324 |
|
| 325 |
|
| 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 |
|
| 332 |
|
| 333 |
|
| 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 |
|
| 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 |
|
| 376 |
|
| 377 |
_fillTexture :: (Typeable n, Floating n) => Lens' (Style V2 n) (Texture n) |
| 378 |
_fillTexture = _fillTextureR . committed |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
fillColor :: (InSpace V2 n a, Color c, Typeable n, Floating n, HasStyle a) => c -> a -> a |
| 385 |
fillColor = fillTexture . SC . SomeColor |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 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 |
|
| 395 |
|
| 396 |
fc :: (InSpace V2 n a, Floating n, Typeable n, HasStyle a) => Colour Double -> a -> a |
| 397 |
fc = fillColor |
| 398 |
|
| 399 |
|
| 400 |
|
| 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 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 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) |