1 | called-from = ()
|
---|
2 |
|
---|
3 | vendors = moz webkit o ms official
|
---|
4 |
|
---|
5 | // stringify the given arg
|
---|
6 |
|
---|
7 | -string(arg)
|
---|
8 | type(arg) + ' ' + arg
|
---|
9 |
|
---|
10 | // require a color
|
---|
11 |
|
---|
12 | require-color(color)
|
---|
13 | unless color is a 'color'
|
---|
14 | error('RGB or HSL value expected, got a ' + -string(color))
|
---|
15 |
|
---|
16 | // require a unit
|
---|
17 |
|
---|
18 | require-unit(n)
|
---|
19 | unless n is a 'unit'
|
---|
20 | error('unit expected, got a ' + -string(n))
|
---|
21 |
|
---|
22 | // require a string
|
---|
23 |
|
---|
24 | require-string(str)
|
---|
25 | unless str is a 'string' or str is a 'ident'
|
---|
26 | error('string expected, got a ' + -string(str))
|
---|
27 |
|
---|
28 | // Math functions
|
---|
29 |
|
---|
30 | abs(n) { math(n, 'abs') }
|
---|
31 | min(a, b) { a < b ? a : b }
|
---|
32 | max(a, b) { a > b ? a : b }
|
---|
33 |
|
---|
34 | // Trigonometrics
|
---|
35 | PI = -math-prop('PI')
|
---|
36 |
|
---|
37 | radians-to-degrees(angle)
|
---|
38 | angle * (180 / PI)
|
---|
39 |
|
---|
40 | degrees-to-radians(angle)
|
---|
41 | angle * (PI / 180)
|
---|
42 |
|
---|
43 | sin(n)
|
---|
44 | n = unit(n) == 'deg' ? degrees-to-radians(unit(n, '')) : unit(n, '')
|
---|
45 | round(math(n, 'sin'), 9)
|
---|
46 |
|
---|
47 | cos(n)
|
---|
48 | n = unit(n) == 'deg' ? degrees-to-radians(unit(n, '')) : unit(n, '')
|
---|
49 | round(math(n, 'cos'), 9)
|
---|
50 |
|
---|
51 | // Rounding Math functions
|
---|
52 |
|
---|
53 | ceil(n, precision = 0)
|
---|
54 | multiplier = 10 ** precision
|
---|
55 | math(n * multiplier, 'ceil') / multiplier
|
---|
56 |
|
---|
57 | floor(n, precision = 0)
|
---|
58 | multiplier = 10 ** precision
|
---|
59 | math(n * multiplier, 'floor') / multiplier
|
---|
60 |
|
---|
61 | round(n, precision = 0)
|
---|
62 | multiplier = 10 ** precision
|
---|
63 | math(n * multiplier, 'round') / multiplier
|
---|
64 |
|
---|
65 | // return the sum of the given numbers
|
---|
66 |
|
---|
67 | sum(nums)
|
---|
68 | sum = 0
|
---|
69 | sum += n for n in nums
|
---|
70 |
|
---|
71 | // return the average of the given numbers
|
---|
72 |
|
---|
73 | avg(nums)
|
---|
74 | sum(nums) / length(nums)
|
---|
75 |
|
---|
76 | // return a unitless number, or pass through
|
---|
77 |
|
---|
78 | remove-unit(n)
|
---|
79 | if typeof(n) is "unit"
|
---|
80 | unit(n, "")
|
---|
81 | else
|
---|
82 | n
|
---|
83 |
|
---|
84 | // convert a percent to a decimal, or pass through
|
---|
85 |
|
---|
86 | percent-to-decimal(n)
|
---|
87 | if unit(n) is "%"
|
---|
88 | remove-unit(n) / 100
|
---|
89 | else
|
---|
90 | n
|
---|
91 |
|
---|
92 | // check if n is an odd number
|
---|
93 |
|
---|
94 | odd(n)
|
---|
95 | 1 == n % 2
|
---|
96 |
|
---|
97 | // check if n is an even number
|
---|
98 |
|
---|
99 | even(n)
|
---|
100 | 0 == n % 2
|
---|
101 |
|
---|
102 | // check if color is light
|
---|
103 |
|
---|
104 | light(color)
|
---|
105 | lightness(color) >= 50%
|
---|
106 |
|
---|
107 | // check if color is dark
|
---|
108 |
|
---|
109 | dark(color)
|
---|
110 | lightness(color) < 50%
|
---|
111 |
|
---|
112 | // desaturate color by amount
|
---|
113 |
|
---|
114 | desaturate(color, amount)
|
---|
115 | adjust(color, 'saturation', - amount)
|
---|
116 |
|
---|
117 | // saturate color by amount
|
---|
118 |
|
---|
119 | saturate(color = '', amount = 100%)
|
---|
120 | if color is a 'color'
|
---|
121 | adjust(color, 'saturation', amount)
|
---|
122 | else
|
---|
123 | unquote( "saturate(" + color + ")" )
|
---|
124 |
|
---|
125 | // darken by the given amount
|
---|
126 |
|
---|
127 | darken(color, amount)
|
---|
128 | adjust(color, 'lightness', - amount)
|
---|
129 |
|
---|
130 | // lighten by the given amount
|
---|
131 |
|
---|
132 | lighten(color, amount)
|
---|
133 | adjust(color, 'lightness', amount)
|
---|
134 |
|
---|
135 | // decrease opacity by amount
|
---|
136 |
|
---|
137 | fade-out(color, amount)
|
---|
138 | color - rgba(black, percent-to-decimal(amount))
|
---|
139 |
|
---|
140 | // increase opacity by amount
|
---|
141 |
|
---|
142 | fade-in(color, amount)
|
---|
143 | color + rgba(black, percent-to-decimal(amount))
|
---|
144 |
|
---|
145 | // spin hue by a given amount
|
---|
146 |
|
---|
147 | spin(color, amount)
|
---|
148 | color + unit(amount, deg)
|
---|
149 |
|
---|
150 | // mix two colors by a given amount
|
---|
151 |
|
---|
152 | mix(color1, color2, weight = 50%)
|
---|
153 | unless weight in 0..100
|
---|
154 | error("Weight must be between 0% and 100%")
|
---|
155 |
|
---|
156 | if length(color1) == 2
|
---|
157 | weight = color1[0]
|
---|
158 | color1 = color1[1]
|
---|
159 |
|
---|
160 | else if length(color2) == 2
|
---|
161 | weight = 100 - color2[0]
|
---|
162 | color2 = color2[1]
|
---|
163 |
|
---|
164 | require-color(color1)
|
---|
165 | require-color(color2)
|
---|
166 |
|
---|
167 | p = unit(weight / 100, '')
|
---|
168 | w = p * 2 - 1
|
---|
169 |
|
---|
170 | a = alpha(color1) - alpha(color2)
|
---|
171 |
|
---|
172 | w1 = (((w * a == -1) ? w : (w + a) / (1 + w * a)) + 1) / 2
|
---|
173 | w2 = 1 - w1
|
---|
174 |
|
---|
175 | channels = (red(color1) red(color2)) (green(color1) green(color2)) (blue(color1) blue(color2))
|
---|
176 | rgb = ()
|
---|
177 |
|
---|
178 | for pair in channels
|
---|
179 | push(rgb, floor(pair[0] * w1 + pair[1] * w2))
|
---|
180 |
|
---|
181 | a1 = alpha(color1) * p
|
---|
182 | a2 = alpha(color2) * (1 - p)
|
---|
183 | alpha = a1 + a2
|
---|
184 |
|
---|
185 | rgba(rgb[0], rgb[1], rgb[2], alpha)
|
---|
186 |
|
---|
187 | // invert colors, leave alpha intact
|
---|
188 |
|
---|
189 | invert(color = '')
|
---|
190 | if color is a 'color'
|
---|
191 | rgba(#fff - color, alpha(color))
|
---|
192 | else
|
---|
193 | unquote( "invert(" + color + ")" )
|
---|
194 |
|
---|
195 | // give complement of the given color
|
---|
196 |
|
---|
197 | complement( color )
|
---|
198 | spin( color, 180 )
|
---|
199 |
|
---|
200 | // give grayscale of the given color
|
---|
201 |
|
---|
202 | grayscale( color = '' )
|
---|
203 | if color is a 'color'
|
---|
204 | desaturate( color, 100% )
|
---|
205 | else
|
---|
206 | unquote( "grayscale(" + color + ")" )
|
---|
207 |
|
---|
208 | // mix the given color with white
|
---|
209 |
|
---|
210 | tint( color, percent )
|
---|
211 | mix( white, color, percent )
|
---|
212 |
|
---|
213 | // mix the given color with black
|
---|
214 |
|
---|
215 | shade( color, percent )
|
---|
216 | mix( black, color, percent )
|
---|
217 |
|
---|
218 | // return the last value in the given expr
|
---|
219 |
|
---|
220 | last(expr)
|
---|
221 | expr[length(expr) - 1]
|
---|
222 |
|
---|
223 | // return keys in the given pairs or object
|
---|
224 |
|
---|
225 | keys(pairs)
|
---|
226 | ret = ()
|
---|
227 | if type(pairs) == 'object'
|
---|
228 | for key in pairs
|
---|
229 | push(ret, key)
|
---|
230 | else
|
---|
231 | for pair in pairs
|
---|
232 | push(ret, pair[0]);
|
---|
233 | ret
|
---|
234 |
|
---|
235 | // return values in the given pairs or object
|
---|
236 |
|
---|
237 | values(pairs)
|
---|
238 | ret = ()
|
---|
239 | if type(pairs) == 'object'
|
---|
240 | for key, val in pairs
|
---|
241 | push(ret, val)
|
---|
242 | else
|
---|
243 | for pair in pairs
|
---|
244 | push(ret, pair[1]);
|
---|
245 | ret
|
---|
246 |
|
---|
247 | // join values with the given delimiter
|
---|
248 |
|
---|
249 | join(delim, vals...)
|
---|
250 | buf = ''
|
---|
251 | vals = vals[0] if length(vals) == 1
|
---|
252 | for val, i in vals
|
---|
253 | buf += i ? delim + val : val
|
---|
254 |
|
---|
255 | // add a CSS rule to the containing block
|
---|
256 |
|
---|
257 | // - This definition allows add-property to be used as a mixin
|
---|
258 | // - It has the same effect as interpolation but allows users
|
---|
259 | // to opt for a functional style
|
---|
260 |
|
---|
261 | add-property-function = add-property
|
---|
262 | add-property(name, expr)
|
---|
263 | if mixin
|
---|
264 | {name} expr
|
---|
265 | else
|
---|
266 | add-property-function(name, expr)
|
---|
267 |
|
---|
268 | prefix-classes(prefix)
|
---|
269 | -prefix-classes(prefix, block)
|
---|
270 |
|
---|
271 | // Caching mixin, use inside your functions to enable caching by extending.
|
---|
272 |
|
---|
273 | $stylus_mixin_cache = {}
|
---|
274 | cache()
|
---|
275 | $key = (current-media() or 'no-media') + '__' + called-from[0] + '__' + arguments
|
---|
276 | if $key in $stylus_mixin_cache
|
---|
277 | @extend {"$cache_placeholder_for_" + $stylus_mixin_cache[$key]}
|
---|
278 | else if 'cache' in called-from
|
---|
279 | {block}
|
---|
280 | else
|
---|
281 | $id = length($stylus_mixin_cache)
|
---|
282 |
|
---|
283 | &,
|
---|
284 | /$cache_placeholder_for_{$id}
|
---|
285 | $stylus_mixin_cache[$key] = $id
|
---|
286 | {block}
|
---|
287 |
|
---|
288 | // Percentage function to convert a number, e.g. ".45", into a percentage, e.g. "45%"
|
---|
289 |
|
---|
290 | percentage(num)
|
---|
291 | return unit(num * 100, '%')
|
---|
292 |
|
---|
293 | // Returns the position of a `value` within a `list`
|
---|
294 |
|
---|
295 | index(list, value)
|
---|
296 | for val, i in list
|
---|
297 | return i if val == value
|
---|