| 1 |
{-# LANGUAGE DeriveDataTypeable #-} |
| 2 |
{-# LANGUAGE FlexibleContexts #-} |
| 3 |
{-# LANGUAGE GeneralizedNewtypeDeriving #-} |
| 4 |
{-# LANGUAGE MultiParamTypeClasses #-} |
| 5 |
{-# LANGUAGE TemplateHaskell #-} |
| 6 |
{-# LANGUAGE TypeFamilies #-} |
| 7 |
----------------------------------------------------------------------------- |
| 8 |
-- | |
| 9 |
-- Module : Diagrams.ThreeD.Attributes |
| 10 |
-- Copyright : (c) 2014 diagrams-lib team (see LICENSE) |
| 11 |
-- License : BSD-style (see LICENSE) |
| 12 |
-- Maintainer : [email protected] |
| 13 |
-- |
| 14 |
-- Diagrams may have /attributes/ which affect the way they are |
| 15 |
-- rendered. This module defines some common attributes relevant in |
| 16 |
-- 3D; particular backends may also define more backend-specific |
| 17 |
-- attributes. |
| 18 |
-- |
| 19 |
-- Every attribute type must have a /semigroup/ structure, that is, an |
| 20 |
-- associative binary operation for combining two attributes into one. |
| 21 |
-- Unless otherwise noted, all the attributes defined here use the |
| 22 |
-- 'Last' structure, that is, combining two attributes simply keeps |
| 23 |
-- the second one and throws away the first. This means that child |
| 24 |
-- attributes always override parent attributes. |
| 25 |
-- |
| 26 |
----------------------------------------------------------------------------- |
| 27 |
|
| 28 |
module Diagrams.ThreeD.Attributes where |
| 29 |
|
| 30 |
import Control.Lens |
| 31 |
import Data.Semigroup |
| 32 |
import Data.Typeable |
| 33 |
|
| 34 |
import Data.Colour |
| 35 |
|
| 36 |
import Diagrams.Core |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
newtype SurfaceColor = SurfaceColor (Last (Colour Double)) |
| 41 |
deriving (Typeable, Semigroup, Show) |
| 42 |
|
| 43 |
instance AttributeClass SurfaceColor |
| 44 |
|
| 45 |
_SurfaceColor :: Iso' SurfaceColor (Colour Double) |
| 46 |
_SurfaceColor = iso (\(SurfaceColor (Last c)) -> c) (SurfaceColor . Last) |
| 47 |
|
| 48 |
|
| 49 |
sc :: HasStyle d => Colour Double -> d -> d |
| 50 |
sc = applyAttr . review _SurfaceColor |
| 51 |
|
| 52 |
|
| 53 |
_sc :: Lens' (Style v n) (Maybe (Colour Double)) |
| 54 |
_sc = atAttr . mapping _SurfaceColor |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
newtype Diffuse = Diffuse (Last Double) |
| 62 |
deriving (Typeable, Semigroup, Show) |
| 63 |
|
| 64 |
instance AttributeClass Diffuse |
| 65 |
|
| 66 |
|
| 67 |
_Diffuse :: Iso' Diffuse Double |
| 68 |
_Diffuse = iso (\(Diffuse (Last d)) -> d) (Diffuse . Last) |
| 69 |
|
| 70 |
|
| 71 |
diffuse :: HasStyle d => Double -> d -> d |
| 72 |
diffuse = applyAttr . review _Diffuse |
| 73 |
|
| 74 |
|
| 75 |
_diffuse :: Lens' (Style v n) (Maybe Double) |
| 76 |
_diffuse = atAttr . mapping _Diffuse |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
newtype Ambient = Ambient (Last Double) |
| 85 |
deriving (Typeable, Semigroup, Show) |
| 86 |
|
| 87 |
instance AttributeClass Ambient |
| 88 |
|
| 89 |
_Ambient :: Iso' Ambient Double |
| 90 |
_Ambient = iso (\(Ambient (Last d)) -> d) (Ambient . Last) |
| 91 |
|
| 92 |
|
| 93 |
ambient :: HasStyle d => Double -> d -> d |
| 94 |
ambient = applyAttr . review _Ambient |
| 95 |
|
| 96 |
|
| 97 |
_ambient :: Lens' (Style v n) (Maybe Double) |
| 98 |
_ambient = atAttr . mapping _Ambient |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
data Specular = Specular |
| 108 |
{ _specularIntensity :: Double |
| 109 |
, _specularSize :: Double |
| 110 |
} deriving Show |
| 111 |
|
| 112 |
makeLenses ''Specular |
| 113 |
|
| 114 |
newtype Highlight = Highlight (Last Specular) |
| 115 |
deriving (Typeable, Semigroup, Show) |
| 116 |
|
| 117 |
instance AttributeClass Highlight |
| 118 |
|
| 119 |
_Highlight :: Iso' Highlight Specular |
| 120 |
_Highlight = iso (\(Highlight (Last s)) -> s) (Highlight . Last) |
| 121 |
|
| 122 |
|
| 123 |
highlight :: HasStyle d => Specular -> d -> d |
| 124 |
highlight = applyAttr . review _Highlight |
| 125 |
|
| 126 |
|
| 127 |
_highlight :: Lens' (Style v n) (Maybe Specular) |
| 128 |
_highlight = atAttr . mapping _Highlight |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
highlightIntensity :: Traversal' (Style v n) Double |
| 133 |
highlightIntensity = _highlight . _Just . specularSize |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
highlightSize :: Traversal' (Style v n) Double |
| 138 |
highlightSize = _highlight . _Just . specularSize |