/
Gradient.purs
167 lines (126 loc) · 4.33 KB
/
Gradient.purs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
module CSS.Gradient
(
-- * Color ramp type.
Ramp
-- * Linear gradients.
, linearGradient
, hGradient
, vGradient
-- * Radial gradients.
, Radial
, circle, ellipse
, circular, elliptical
, Extend
, closestSide, closestCorner, farthestSide, farthestCorner
, radialGradient
-- * Repeating gradients.
, repeatingLinearGradient
, hRepeatingGradient
, vRepeatingGradient
, repeatingRadialGradient
)
where
import Prelude
import Data.Generic (class Generic)
import Data.Tuple (Tuple(..))
import CSS.Background (class Loc, BackgroundImage, Direction, sideTop, straight, sideLeft)
import CSS.Color (Color)
import CSS.Common (class Other, browsers, other)
import CSS.Property (class Val, Value(..), value)
import CSS.Size (Size, Abs, Rel, pct)
import CSS.String (fromString)
type Ramp = Array (Tuple Color (Size Rel))
-------------------------------------------------------------------------------
linearGradient :: Direction -> Ramp -> BackgroundImage
linearGradient d xs = other $ Value $
let
lg =
fromString "linear-gradient("
<> value d
<> fromString ","
<> ramp xs
<> fromString ")"
in
case lg of
Value v -> browsers <> v
hGradient :: Color -> Color -> BackgroundImage
hGradient = shortcut (linearGradient (straight sideLeft))
vGradient :: Color -> Color -> BackgroundImage
vGradient = shortcut (linearGradient (straight sideTop ))
-------------------------------------------------------------------------------
repeatingLinearGradient :: Direction -> Ramp -> BackgroundImage
repeatingLinearGradient d xs = other $ Value $
let
rlg =
fromString "repeating-linear-gradient("
<> value d
<> fromString ","
<> ramp xs
<> fromString ")"
in
case rlg of
Value v -> browsers <> v
hRepeatingGradient :: Color -> Color -> BackgroundImage
hRepeatingGradient = shortcut (repeatingLinearGradient (straight sideLeft))
vRepeatingGradient :: Color -> Color -> BackgroundImage
vRepeatingGradient = shortcut (repeatingLinearGradient (straight sideTop ))
-------------------------------------------------------------------------------
newtype Radial = Radial Value
derive instance eqRadial :: Eq Radial
derive instance ordRadial :: Ord Radial
derive instance genericRadial :: Generic Radial
instance valRadial :: Val Radial where
value (Radial v) = v
instance otherRadial :: Other Radial where
other = Radial
circle :: Extend -> Radial
circle ext = Radial (fromString "circle " <> value ext)
ellipse :: Extend -> Radial
ellipse ext = Radial (fromString "ellipse " <> value ext)
circular :: Size Abs -> Radial
circular radius = Radial (value (Tuple radius radius))
elliptical :: forall a. Size a -> Size a -> Radial
elliptical radx rady = Radial (value (Tuple radx rady))
newtype Extend = Extend Value
derive instance eqExtend :: Eq Extend
derive instance ordExtend :: Ord Extend
derive instance genericExtend :: Generic Extend
instance valExtend :: Val Extend where
value (Extend v) = v
instance otherExtend :: Other Extend where
other = Extend
closestSide :: Extend
closestSide = Extend $ fromString "closest-side"
closestCorner :: Extend
closestCorner = Extend $ fromString "closest-corner"
farthestSide :: Extend
farthestSide = Extend $ fromString "farthest-side"
farthestCorner :: Extend
farthestCorner = Extend $ fromString "farthest-corner"
-------------------------------------------------------------------------------
radialGradient :: forall l. Loc l => l -> Radial -> Ramp -> BackgroundImage
radialGradient d r xs = other $ Value $
let
rg =
fromString "radial-gradient("
<> value [value d, value r, ramp xs]
<> fromString ")"
in
case rg of
Value v -> browsers <> v
repeatingRadialGradient
:: forall l. Loc l => l -> Radial -> Ramp -> BackgroundImage
repeatingRadialGradient d r xs = other $ Value $
let
rrg =
fromString "repeating-radial-gradient("
<> value [value d, value r, ramp xs]
<> fromString ")"
in
case rrg of
Value v -> browsers <> v
-------------------------------------------------------------------------------
ramp :: Ramp -> Value
ramp xs = value (map (\(Tuple a b) -> value (Tuple (value a) (value b))) xs)
shortcut :: (Ramp -> BackgroundImage) -> Color -> Color -> BackgroundImage
shortcut g f t = g [(Tuple f (pct 0.0)), (Tuple t (pct 100.0))]