Skip to content

Commit

Permalink
bergeys proposal
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffreyrosenbluth committed Nov 13, 2013
1 parent 977ad18 commit 019990d
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 131 deletions.
83 changes: 0 additions & 83 deletions src/Diagrams/Attributes.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,6 @@ module Diagrams.Attributes (

Color(..), SomeColor(..)

-- ** Line color
, LineColor, getLineColor, lineColor, lineColorA, lc, lcA

-- ** Fill color
, FillColor, getFillColor, recommendFillColor, fillColor, fc, fcA

-- ** Opacity
, Opacity, getOpacity, opacity

Expand Down Expand Up @@ -95,77 +89,6 @@ class Color c where
data SomeColor = forall c. Color c => SomeColor c
deriving Typeable

-- | The color with which lines (strokes) are drawn. Note that child
-- colors always override parent colors; that is, @'lineColor' c1
-- . 'lineColor' c2 $ d@ is equivalent to @'lineColor' c2 $ d@.
-- More precisely, the semigroup structure on line color attributes
-- is that of 'Last'.
newtype LineColor = LineColor (Last SomeColor)
deriving (Typeable, Semigroup)
instance AttributeClass LineColor

instance Default LineColor where
def = LineColor (Last (SomeColor (black :: Colour Double)))

getLineColor :: LineColor -> SomeColor
getLineColor (LineColor (Last c)) = c

-- | Set the line (stroke) color. This function is polymorphic in the
-- color type (so it can be used with either 'Colour' or
-- 'AlphaColour'), but this can sometimes create problems for type
-- inference, so the 'lc' and 'lcA' variants are provided with more
-- concrete types.
lineColor :: (Color c, HasStyle a) => c -> a -> a
lineColor = applyAttr . LineColor . Last . SomeColor

-- | Apply a 'lineColor' attribute.
lineColorA :: HasStyle a => LineColor -> a -> a
lineColorA = applyAttr

-- | A synonym for 'lineColor', specialized to @'Colour' Double@
-- (i.e. opaque colors).
lc :: HasStyle a => Colour Double -> a -> a
lc = lineColor

-- | A synonym for 'lineColor', specialized to @'AlphaColour' Double@
-- (i.e. colors with transparency).
lcA :: HasStyle a => AlphaColour Double -> a -> a
lcA = lineColor

-- | The color with which shapes are filled. Note that child
-- colors always override parent colors; that is, @'fillColor' c1
-- . 'fillColor' c2 $ d@ is equivalent to @'lineColor' c2 $ d@.
-- More precisely, the semigroup structure on fill color attributes
-- is that of 'Last'.
newtype FillColor = FillColor (Recommend (Last SomeColor))
deriving (Typeable, Semigroup)
instance AttributeClass FillColor

-- | Set the fill color. This function is polymorphic in the color
-- type (so it can be used with either 'Colour' or 'AlphaColour'),
-- but this can sometimes create problems for type inference, so the
-- 'fc' and 'fcA' variants are provided with more concrete types.
fillColor :: (Color c, HasStyle a) => c -> a -> a
fillColor = applyAttr . FillColor . Commit . Last . SomeColor

-- | Set a \"recommended\" fill color, to be used only if no explicit
-- calls to 'fillColor' (or 'fc', or 'fcA') are used.
recommendFillColor :: (Color c, HasStyle a) => c -> a -> a
recommendFillColor = applyAttr . FillColor . Recommend . Last . SomeColor

getFillColor :: FillColor -> SomeColor
getFillColor (FillColor c) = getLast . getRecommend $ c

-- | A synonym for 'fillColor', specialized to @'Colour' Double@
-- (i.e. opaque colors).
fc :: HasStyle a => Colour Double -> a -> a
fc = fillColor

-- | A synonym for 'fillColor', specialized to @'AlphaColour' Double@
-- (i.e. colors with transparency).
fcA :: HasStyle a => AlphaColour Double -> a -> a
fcA = fillColor

This comment has been minimized.

Copy link
@jeffreyrosenbluth

jeffreyrosenbluth Nov 13, 2013

Author Member

Moved to TwoD.Attributes.hs

instance (Floating a, Real a) => Color (Colour a) where
toAlphaColour = opaque . colourConvert

Expand All @@ -175,12 +98,6 @@ instance (Floating a, Real a) => Color (AlphaColour a) where
instance Color SomeColor where
toAlphaColour (SomeColor c) = toAlphaColour c

instance Color LineColor where
toAlphaColour (LineColor (Last c)) = toAlphaColour c

instance Color FillColor where
toAlphaColour (FillColor c) = toAlphaColour . getLast . getRecommend $ c

-- | Convert to an RGB space while preserving the alpha channel.
toRGBAUsingSpace :: Color c => RGBSpace Double -> c
-> (Double, Double, Double, Double)
Expand Down
6 changes: 3 additions & 3 deletions src/Diagrams/TwoD.hs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ module Diagrams.TwoD
, sized, sizedAs

-- * Textures
, Texture(..), SpreadMethod(..), GradientStop, mkStops, getFillTexture
, Texture(..), SpreadMethod(..), GradientStop(..), mkStops, getFillTexture
, fillTexture, getLineTexture, lineTexture, idTransform

, LGradient(..), lGradStops, lGradTrans, lGradStart, lGradEnd
Expand All @@ -237,8 +237,8 @@ module Diagrams.TwoD
, rGradSpreadMethod, defaultRG, _RG, mkRadialGradient

-- ** Colors
, fillColorT, fcT, fcAT, recommendFillColorT
, lineColorT, lcT, lcAT
, fillColor, fc, fcA, recommendFillColor, getFillColor
, lineColor, lc, lcA, lineColorA, getLineColor

This comment has been minimized.

Copy link
@jeffreyrosenbluth

jeffreyrosenbluth Nov 13, 2013

Author Member

No change for the user


-- * Visual aids for understanding the internal model
, showOrigin
Expand Down
21 changes: 11 additions & 10 deletions src/Diagrams/TwoD/Adjust.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,19 @@ module Diagrams.TwoD.Adjust

import Diagrams.Core

import Diagrams.Attributes (lineWidthA, lineColorA, lineCap
, lineJoin, lineMiterLimitA
)
import Diagrams.Util ((#))
import Diagrams.Attributes (lineWidthA, lineCap
, lineJoin, lineMiterLimitA
)
import Diagrams.TwoD.Attributes (lineColorA)
import Diagrams.Util ((#))

import Diagrams.TwoD.Types (R2, p2)
import Diagrams.TwoD.Size ( size2D, center2D, SizeSpec2D(..)
, requiredScaleT, requiredScale
)
import Diagrams.TwoD.Text (fontSizeA)
import Diagrams.TwoD.Types (R2, p2)
import Diagrams.TwoD.Size ( size2D, center2D, SizeSpec2D(..)
, requiredScaleT, requiredScale
)
import Diagrams.TwoD.Text (fontSizeA)

import Data.AffineSpace ((.-.))
import Data.AffineSpace ((.-.))
import Data.Semigroup

import Data.Default.Class
Expand Down
3 changes: 2 additions & 1 deletion src/Diagrams/TwoD/Arrow.hs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ import Diagrams.Core

import Data.Colour hiding (atop)
import Diagrams.Attributes
import Diagrams.TwoD.Attributes
import Diagrams.Parametric
import Diagrams.Path
import Diagrams.Solve (quadForm)
Expand Down Expand Up @@ -201,7 +202,7 @@ xWidth p = a + b
b = fromMaybe 0 (magnitude <$> traceV origin unit_X p)

-- | Get the line color from the shaft to use as the fill color for the joint.
colorJoint :: Style v -> Style v
colorJoint :: Style R2 -> Style R2
colorJoint sStyle =
let c = fmap getLineColor . getAttr $ sStyle in
case c of
Expand Down
127 changes: 99 additions & 28 deletions src/Diagrams/TwoD/Attributes.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

-----------------------------------------------------------------------------
-- |
-- Module : Diagrams.Attributes
-- Module : Diagrams.TwoD.Attributes
-- Copyright : (c) 2013 diagrams-lib team (see LICENSE)
-- License : BSD-style (see LICENSE)
-- Maintainer : diagrams-discuss@googlegroups.com
Expand All @@ -22,7 +22,8 @@
module Diagrams.TwoD.Attributes (
-- * Gradients
Texture(..), _SC, _LG, _RG, defaultLG, defaultRG, mkStops, idTransform
, GradientStop, SpreadMethod(..), lineLGradient
, GradientStop(..), SpreadMethod(..), lineLGradient, lineRGradient
, stopColor, stopFraction

-- ** Linear Gradients
, LGradient(..), lGradStops, lGradTrans, lGradStart, lGradEnd
Expand All @@ -36,13 +37,13 @@ module Diagrams.TwoD.Attributes (
, LineTexture(..), getLineTexture, lineTexture

-- * Line color
, lineColorT, lcT, lcAT
, LineColor, lineColor, getLineColor, lc, lcA, lineColorA

-- * Fill texture
, FillTexture(..), getFillTexture, fillTexture

-- * Fill color
, fillColorT, fcT, fcAT, recommendFillColorT
, FillColor, fillColor, getFillColor, fc, fcA, recommendFillColor

) where

Expand All @@ -60,7 +61,12 @@ import Data.Monoid.Recommend
import Data.Semigroup

-- | A stop is (color, proportion, opacity)
type GradientStop = (SomeColor, Double, Double)
--type GradientStop = (SomeColor, Double, Double)
data GradientStop = GradientStop
{ _stopColor :: SomeColor
, _stopFraction :: Double}

makeLenses ''GradientStop

This comment has been minimized.

Copy link
@jeffreyrosenbluth

jeffreyrosenbluth Nov 13, 2013

Author Member

Change GradientStop to a type not an alias.
And removed redundant opacity field.


data SpreadMethod = GradPad | GradReflect | GradRepeat

Expand Down Expand Up @@ -113,16 +119,16 @@ defaultRG = RG (RGradient
, _rGradSpreadMethod = GradPad
})

mkStops :: Color c => [(c, Double, Double)] -> [GradientStop]
mkStops s = map (\(x, y, z) -> (SomeColor x, y, z)) s
mkStops :: [(Colour Double, Double, Double)] -> [GradientStop]
mkStops s = map (\(x, y, z) -> GradientStop (SomeColor (withOpacity x z)) y) s

mkLinearGradient :: [GradientStop] -> P2 -> P2 -> SpreadMethod -> Texture
mkLinearGradient stops start end spreadMethod
= LG (LGradient stops start end (scaling 1) spreadMethod)

mkRadialGradient :: [GradientStop] -> Double -> P2 -> P2 -> SpreadMethod -> Texture
mkRadialGradient stops radius center focus spreadMethod
= RG (RGradient stops radius center focus (scaling 1) spreadMethod)
mkRadialGradient stops r center focus spreadMethod
= RG (RGradient stops r center focus (scaling 1) spreadMethod)

newtype LineTexture = LineTexture (Last Texture)
deriving (Typeable, Semigroup)
Expand All @@ -147,14 +153,48 @@ getLineTexture (LineTexture (Last t)) = t
lineTexture :: (HasStyle a, V a ~ R2) => Texture-> a -> a
lineTexture = applyTAttr . LineTexture . Last

lineColorT :: (Color c, HasStyle a, V a ~ R2) => c -> a -> a
lineColorT c = lineTexture (SC (SomeColor c))

lcT :: (HasStyle a, V a ~ R2) => Colour Double -> a -> a
lcT = lineColorT

lcAT :: (HasStyle a, V a ~ R2) => AlphaColour Double -> a -> a
lcAT = lineColorT
-- | The color with which lines (strokes) are drawn. Note that child
-- colors always override parent colors; that is, @'lineColor' c1
-- . 'lineColor' c2 $ d@ is equivalent to @'lineColor' c2 $ d@.
-- More precisely, the semigroup structure on line color attributes
-- is that of 'Last'.
newtype LineColor = LineColor (Last SomeColor)
deriving (Typeable, Semigroup)
instance AttributeClass LineColor

instance Default LineColor where
def = LineColor (Last (SomeColor (black :: Colour Double)))

instance Color LineColor where
toAlphaColour (LineColor (Last c)) = toAlphaColour c

getLineColor :: LineColor -> SomeColor
getLineColor (LineColor (Last c)) = c

-- | Set the line (stroke) color. This function is polymorphic in the
-- color type (so it can be used with either 'Colour' or
-- 'AlphaColour'), but this can sometimes create problems for type
-- inference, so the 'lc' and 'lcA' variants are provided with more
-- concrete types.
lineColor :: (Color c, HasStyle a, V a ~ R2) => c -> a -> a
lineColor c = (lTx c) . (lCl c)
where
lTx x = lineTexture (SC (SomeColor x))
lCl = applyAttr . LineColor . Last . SomeColor

This comment has been minimized.

Copy link
@jeffreyrosenbluth

jeffreyrosenbluth Nov 13, 2013

Author Member

Add both attributes to the style. backends should implement only one!


-- | Apply a 'lineColor' attribute.
lineColorA :: HasStyle a => LineColor -> a -> a
lineColorA = applyAttr

-- | A synonym for 'lineColor', specialized to @'Colour' Double@
-- (i.e. opaque colors).
lc :: (HasStyle a, V a ~ R2) => Colour Double -> a -> a
lc = lineColor

-- | A synonym for 'lineColor', specialized to @'AlphaColour' Double@
-- (i.e. colors with transparency).
lcA :: (HasStyle a, V a ~ R2) => AlphaColour Double -> a -> a
lcA = lineColor

lineLGradient :: (HasStyle a, V a ~ R2) => LGradient -> a -> a
lineLGradient g = lineTexture (LG g)
Expand Down Expand Up @@ -184,14 +224,45 @@ getFillTexture (FillTexture tx) = getLast . getRecommend $ tx
fillTexture :: (HasStyle a, V a ~ R2) => Texture -> a -> a
fillTexture = applyTAttr . FillTexture . Commit . Last

fillColorT :: (Color c, HasStyle a, V a ~ R2) => c -> a -> a
fillColorT c = fillTexture (SC (SomeColor c))

recommendFillColorT :: (Color c, HasStyle a, V a ~ R2) => c -> a -> a
recommendFillColorT = applyTAttr . FillTexture . Recommend . Last . SC . SomeColor

fcT :: (HasStyle a, V a ~ R2) => Colour Double -> a -> a
fcT = fillColorT

fcAT :: (HasStyle a, V a ~ R2) => AlphaColour Double -> a -> a
fcAT = fillColorT
-- | The color with which shapes are filled. Note that child
-- colors always override parent colors; that is, @'fillColor' c1
-- . 'fillColor' c2 $ d@ is equivalent to @'lineColor' c2 $ d@.
-- More precisely, the semigroup structure on fill color attributes
-- is that of 'Last'.
newtype FillColor = FillColor (Recommend (Last SomeColor))
deriving (Typeable, Semigroup)
instance AttributeClass FillColor

instance Color FillColor where
toAlphaColour (FillColor c) = toAlphaColour . getLast . getRecommend $ c

-- | Set the fill color. This function is polymorphic in the color
-- type (so it can be used with either 'Colour' or 'AlphaColour'),
-- but this can sometimes create problems for type inference, so the
-- 'fc' and 'fcA' variants are provided with more concrete types.
fillColor :: (Color c, HasStyle a, V a ~ R2) => c -> a -> a
fillColor c = (fTx c) . (fCl c)
where
fTx x = fillTexture (SC (SomeColor x))
fCl = applyAttr . FillColor . Commit . Last . SomeColor

This comment has been minimized.

Copy link
@jeffreyrosenbluth

jeffreyrosenbluth Nov 13, 2013

Author Member

As with lineColor add both attributes to the style. backends should implement only one!

This comment has been minimized.

Copy link
@byorgey

byorgey Nov 13, 2013

Member

How about adding some Haddock comments explaining that backends should implement only one?

This comment has been minimized.

Copy link
@jeffreyrosenbluth

jeffreyrosenbluth via email Nov 13, 2013

Author Member

-- | Set a \"recommended\" fill color, to be used only if no explicit
-- calls to 'fillColor' (or 'fc', or 'fcA') are used.
recommendFillColor :: (Color c, HasStyle a, V a ~ R2) => c -> a -> a
recommendFillColor c = (fT c) . (fC c)
where
fT = applyTAttr . FillTexture . Recommend . Last . SC . SomeColor
fC = applyAttr . FillColor . Recommend . Last . SomeColor

getFillColor :: FillColor -> SomeColor
getFillColor (FillColor c) = getLast . getRecommend $ c

-- | A synonym for 'fillColor', specialized to @'Colour' Double@
-- (i.e. opaque colors).
fc :: (HasStyle a, V a ~ R2) => Colour Double -> a -> a
fc = fillColor

-- | A synonym for 'fillColor', specialized to @'AlphaColour' Double@
-- (i.e. colors with transparency).
fcA :: (HasStyle a, V a ~ R2) => AlphaColour Double -> a -> a
fcA = fillColor
11 changes: 6 additions & 5 deletions src/Diagrams/TwoD/Combinators.hs
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,21 @@ import Data.VectorSpace

import Diagrams.Core

import Diagrams.Attributes (fc, lw)
import Diagrams.Attributes (lw)
import Diagrams.TwoD.Attributes (fc)
import Diagrams.BoundingBox
import Diagrams.Combinators
import Diagrams.Coordinates
import Diagrams.Path
import Diagrams.Segment
import Diagrams.TrailLike
import Diagrams.TwoD.Align
import Diagrams.TwoD.Path ()
import Diagrams.TwoD.Path ()
import Diagrams.TwoD.Shapes
import Diagrams.TwoD.Transform (scaleX, scaleY)
import Diagrams.TwoD.Transform (scaleX, scaleY)
import Diagrams.TwoD.Types
import Diagrams.TwoD.Vector (fromDirection, unitX, unitY)
import Diagrams.Util (( # ))
import Diagrams.TwoD.Vector (fromDirection, unitX, unitY)
import Diagrams.Util (( # ))


infixl 6 ===
Expand Down
1 change: 1 addition & 0 deletions src/Diagrams/TwoD/Model.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import Control.Lens (makeLenses, (^.))

import Diagrams.Core
import Diagrams.Attributes
import Diagrams.TwoD.Attributes
import Diagrams.Path
import Diagrams.TwoD.Ellipse
import Diagrams.TwoD.Path
Expand Down
Loading

1 comment on commit 019990d

@jeffreyrosenbluth
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested this branch with Cairo (buy building the website) - all seems to work fine.

  • Without gradients ofcourse.

Please sign in to comment.