/
gradients.styl
91 lines (77 loc) · 1.97 KB
/
gradients.styl
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
@import 'config'
/*
* Implicit color stop position.
*/
pos-in-stops(i, stops)
len = length(stops)
if len - 1 == i
100%
else if i
unit(i / len * 100, '%')
else
0
/*
* Normalize color stops:
*
* - (color pos) -> (pos color)
* - (color) -> (implied-pos color)
*
*/
normalize-stops(stops)
stops = clone(stops)
for stop, i in stops
if length(stop) == 1
color = stop[0]
stop[0] = pos-in-stops(i, stops)
stop[1] = color
else if typeof(stop[1]) == 'unit'
pos = stop[1]
stop[1] = stop[0]
stop[0] = pos
stops
/*
* Join color stops with the given translation function.
*/
join-stops(stops, translate)
str = ''
len = length(stops)
for stop, i in stops
str += ', ' if i
pos = stop[0]
color = stop[1]
str += translate(color, pos)
unquote(str)
/*
* Standard color stop.
*/
std-stop(color, pos)
'%s %s' % (color pos)
/*
* Create a linear gradient with the given start position
* and variable number of color stops.
*
* Examples:
*
* background: linear-gradient(top, red, green, blue)
* background: linear-gradient(bottom, red, green 50%, blue)
* background: linear-gradient(bottom, red, 50% green, blue)
* background: linear-gradient(bottom, red, 50% green, 90% white, blue)
*
*/
linear-gradient(start, stops...)
error('color stops required') unless length(stops)
unquote('linear-gradient(' + join(', ',arguments) + ')')
/*
* Create a linear gradient image with the given start position
* and variable number of color stops.
*/
linear-gradient-image(start, stops...)
error('node-canvas is required for linear-gradient-image()') unless has-canvas
stops = stops[0] if length(stops) == 1
error('gradient image size required') unless start[0] is a 'unit'
size = start[0]
start = start[1] or 'top'
grad = create-gradient-image(size, start)
stops = normalize-stops(stops)
add-color-stop(grad, stop[0], stop[1]) for stop in stops
'url(%s)' % gradient-data-uri(grad)