Blame view

node_modules/stylus/lib/functions/index.styl 5.71 KB
ce4c83ff   wxy   初始提交
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
  called-from = ()
  
  vendors = moz webkit o ms official
  
  // stringify the given arg
  
  -string(arg)
    type(arg) + ' ' + arg
  
  // require a color
  
  require-color(color)
    unless color is a 'color'
      error('RGB or HSL value expected, got a ' + -string(color))
  
  // require a unit
  
  require-unit(n)
    unless n is a 'unit'
      error('unit expected, got a ' + -string(n))
  
  // require a string
  
  require-string(str)
    unless str is a 'string' or str is a 'ident'
      error('string expected, got a ' + -string(str))
  
  // Math functions
  
  abs(n) { math(n, 'abs') }
  min(a, b) { a < b ? a : b }
  max(a, b) { a > b ? a : b }
  
  // Trigonometrics
  PI = -math-prop('PI')
  
  radians-to-degrees(angle)
    angle * (180 / PI)
  
  degrees-to-radians(angle)
    unit(angle * (PI / 180),'')
  
  sin(n)
    n = degrees-to-radians(n) if unit(n) == 'deg'
    round(math(n, 'sin'), 9)
  
  cos(n)
    n = degrees-to-radians(n) if unit(n) == 'deg'
    round(math(n, 'cos'), 9)
  
  // Rounding Math functions
  
  ceil(n, precision = 0)
    multiplier = 10 ** precision
    math(n * multiplier, 'ceil') / multiplier
  
  floor(n, precision = 0)
    multiplier = 10 ** precision
    math(n * multiplier, 'floor') / multiplier
  
  round(n, precision = 0)
    multiplier = 10 ** precision
    math(n * multiplier, 'round') / multiplier
  
  // return the sum of the given numbers
  
  sum(nums)
    sum = 0
    sum += n for n in nums
  
  // return the average of the given numbers
  
  avg(nums)
    sum(nums) / length(nums)
  
  // return a unitless number, or pass through
  
  remove-unit(n)
    if typeof(n) is "unit"
      unit(n, "")
    else
      n
  
  // convert a percent to a decimal, or pass through
  
  percent-to-decimal(n)
    if unit(n) is "%"
      remove-unit(n) / 100
    else
      n
  
  // check if n is an odd number
  
  odd(n)
    1 == n % 2
  
  // check if n is an even number
  
  even(n)
    0 == n % 2
  
  // check if color is light
  
  light(color)
    lightness(color) >= 50%
  
  // check if color is dark
  
  dark(color)
    lightness(color) < 50%
  
  // desaturate color by amount
  
  desaturate(color, amount)
    adjust(color, 'saturation', - amount)
  
  // saturate color by amount
  
  saturate(color = '', amount = 100%)
    if color is a 'color'
      adjust(color, 'saturation', amount)
    else
      unquote( "saturate(" + color + ")" )
  
  // darken by the given amount
  
  darken(color, amount)
    adjust(color, 'lightness', - amount)
  
  // lighten by the given amount
  
  lighten(color, amount)
    adjust(color, 'lightness', amount)
  
  // decrease opacity by amount
  
  fade-out(color, amount)
    color - rgba(black, percent-to-decimal(amount))
  
  // increase opacity by amount
  
  fade-in(color, amount)
    color + rgba(black, percent-to-decimal(amount))
  
  // spin hue by a given amount
  
  spin(color, amount)
    color + unit(amount, deg)
  
  // mix two colors by a given amount
  
  mix(color1, color2, weight = 50%)
    unless weight in 0..100
      error("Weight must be between 0% and 100%")
  
    if length(color1) == 2
      weight = color1[0]
      color1 = color1[1]
  
    else if length(color2) == 2
      weight = 100 - color2[0]
      color2 = color2[1]
  
    require-color(color1)
    require-color(color2)
  
    p = unit(weight / 100, '')
    w = p * 2 - 1
  
    a = alpha(color1) - alpha(color2)
  
    w1 = (((w * a == -1) ? w : (w + a) / (1 + w * a)) + 1) / 2
    w2 = 1 - w1
  
    channels = (red(color1) red(color2)) (green(color1) green(color2)) (blue(color1) blue(color2))
    rgb = ()
  
    for pair in channels
      push(rgb, floor(pair[0] * w1 + pair[1] * w2))
  
    a1 = alpha(color1) * p
    a2 = alpha(color2) * (1 - p)
    alpha = a1 + a2
  
    rgba(rgb[0], rgb[1], rgb[2], alpha)
  
  // invert colors, leave alpha intact
  
  invert(color = '')
    if color is a 'color'
      rgba(#fff - color, alpha(color))
    else
      unquote( "invert(" + color + ")" )
  
  // give complement of the given color
  
  complement( color )
    spin( color, 180 )
  
  // give grayscale of the given color
  
  grayscale( color = '' )
    if color is a 'color'
      desaturate( color, 100% )
    else
      unquote( "grayscale(" + color + ")" )
  
  // mix the given color with white
  
  tint( color, percent )
    mix( white, color, percent )
  
  // mix the given color with black
  
  shade( color, percent )
    mix( black, color, percent )
  
  // return the last value in the given expr
  
  last(expr)
    expr[length(expr) - 1]
  
  // return keys in the given pairs or object
  
  keys(pairs)
    ret = ()
    if type(pairs) == 'object'
      for key in pairs
        push(ret, key)
    else
      for pair in pairs
        push(ret, pair[0]);
    ret
  
  // return values in the given pairs or object
  
  values(pairs)
    ret = ()
    if type(pairs) == 'object'
      for key, val in pairs
        push(ret, val)
    else
      for pair in pairs
        push(ret, pair[1]);
    ret
  
  // join values with the given delimiter
  
  join(delim, vals...)
    buf = ''
    vals = vals[0] if length(vals) == 1
    for val, i in vals
      buf += i ? delim + val : val
  
  // add a CSS rule to the containing block
  
  // - This definition allows add-property to be used as a mixin
  // - It has the same effect as interpolation but allows users
  //   to opt for a functional style
  
  add-property-function = add-property
  add-property(name, expr)
    if mixin
      {name} expr
    else
      add-property-function(name, expr)
  
  prefix-classes(prefix)
    -prefix-classes(prefix, block)
  
  // Caching mixin, use inside your functions to enable caching by extending.
  
  $stylus_mixin_cache = {}
  cache()
    $key = (current-media() or 'no-media') + '__' + called-from[0] + '__' + arguments
    if $key in $stylus_mixin_cache
      @extend {"$cache_placeholder_for_" + $stylus_mixin_cache[$key]}
    else if 'cache' in called-from
      {block}
    else
      $id = length($stylus_mixin_cache)
  
      &,
      /$cache_placeholder_for_{$id}
        $stylus_mixin_cache[$key] = $id
        {block}
  
  // Percentage function to convert a number, e.g. ".45", into a percentage, e.g. "45%"
  
  percentage(num)
    return unit(num * 100, '%')
  
  // Returns the position of a `value` within a `list`
  
  index(list, value)
    for val, i in list
      return i if val == value