index.styl 5.7 KB

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