functions.html 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. ---
  2. title: Functions
  3. layout: documentation
  4. doc-tab: overview
  5. doc-subtab: functions
  6. ---
  7. {% include subnav-overview.html %}
  8. <section class="section">
  9. <div class="container">
  10. <h1 class="title">Functions</h1>
  11. <h2 class="subtitle">Utility functions to calculate colors and other values</h2>
  12. <hr>
  13. <div class="content">
  14. <p>Bulma uses 5 custom functions to help define the values and colors dynamically:</p>
  15. <ul>
  16. <li><code>powerNumber($number, $exp)</code>: calculates the value of a number exposed to another one. Returns a number.</li>
  17. <li><code>colorLuminance($color)</code>: defines if a color is dark or light. Return a decimal number between 0 and 1 where <= 0.5 is dark and > 0.5 is light.</li>
  18. <li><code>findColorInvert($color)</code>: returns either 70% transparent black or 100% opaque white depending on the luminance of the color.</li>
  19. </ul>
  20. </div>
  21. <hr>
  22. <h2 id="findColorInvert" class="title">The <code>findColorInvert()</code> function</h2>
  23. <div class="content">
  24. <p>The <code>findColorInvert($color)</code> function takes a <strong>color</strong> as an input, and outputs either transparent <strong>black</strong> <code>rgba(#000, 0.7)</code> or <strong>white</strong> <code>#fff</code>:</p>
  25. <ul>
  26. <li>if <code>colorLuminance($color) > 0.55</code>, it outputs <code>rgba(#000, 0.7)</code></li>
  27. <li>otherwise, it outputs <code>#fff</code></li>
  28. </ul>
  29. <p>Its purpose is to guarantee a <strong>readable</strong> shade for the <em>text</em> when the input color is used as the <em>background</em>.</p>
  30. <table class="table is-bordered">
  31. <thead>
  32. <tr>
  33. <th>color</th>
  34. <th>color luminance</th>
  35. <th>findColorInvert()</th>
  36. <th>result</th>
  37. </tr>
  38. </thead>
  39. <tbody>
  40. <tr>
  41. <td>
  42. <span class="color" style="background: #00d1b2;"></span>
  43. <code>#00d1b2</code>
  44. </td>
  45. <td>
  46. <code>0.52831</code>
  47. </td>
  48. <td>
  49. <span class="color" style="background: #fff;"></span>
  50. <code>#fff</code>
  51. </td>
  52. <td>
  53. <a class="button" style="background: #00d1b2; border-color: #00d1b2; color: #fff;">
  54. Button
  55. </a>
  56. </td>
  57. </tr>
  58. <tr>
  59. <td>
  60. <span class="color" style="background: #3273dc;"></span>
  61. <code>#3273dc</code>
  62. </td>
  63. <td>
  64. <code>0.23119</code>
  65. </td>
  66. <td>
  67. <span class="color" style="background: #fff;"></span>
  68. <code>#fff</code>
  69. </td>
  70. <td>
  71. <a class="button" style="background: #3273dc; border-color: #3273dc; color: #fff;">
  72. Button
  73. </a>
  74. </td>
  75. </tr>
  76. <tr>
  77. <td>
  78. <span class="color" style="background: #23d160;"></span>
  79. <code>#23d160</code>
  80. </td>
  81. <td>
  82. <code>0.51067</code>
  83. </td>
  84. <td>
  85. <span class="color" style="background: #fff;"></span>
  86. <code>#fff</code>
  87. </td>
  88. <td>
  89. <a class="button" style="background: #23d160; border-color: #23d160; color: #fff;">
  90. Button
  91. </a>
  92. </td>
  93. </tr>
  94. <tr>
  95. <td>
  96. <span class="color" style="background: #ffdd57;"></span>
  97. <code>#ffdd57</code>
  98. </td>
  99. <td>
  100. <code>0.76863</code>
  101. </td>
  102. <td>
  103. <span class="color" style="background: rgba(0, 0, 0, 0.7);"></span>
  104. <code>rgba(0, 0, 0, 0.7)</code>
  105. </td>
  106. <td>
  107. <a class="button" style="background: #ffdd57; border-color: #ffdd57; color: rgba(0, 0, 0, 0.7);">
  108. Button
  109. </a>
  110. </td>
  111. </tr>
  112. <tr>
  113. <td>
  114. <span class="color" style="background: #ff3860;"></span>
  115. <code>#ff3860</code>
  116. </td>
  117. <td>
  118. <code>0.27313</code>
  119. </td>
  120. <td>
  121. <span class="color" style="background: #fff;"></span>
  122. <code>#fff</code>
  123. </td>
  124. <td>
  125. <a class="button" style="background: #ff3860; border-color: #ff3860; color: #fff;">
  126. Button
  127. </a>
  128. </td>
  129. </tr>
  130. <tr>
  131. <td>
  132. <span class="color" style="background: #ffb3b3;"></span>
  133. <code>#ffb3b3</code>
  134. </td>
  135. <td>
  136. <code>0.61796</code>
  137. </td>
  138. <td>
  139. <span class="color" style="background: rgba(0,0,0,0.7);"></span>
  140. <code>rgba(0,0,0,0.7)</code>
  141. </td>
  142. <td>
  143. <a class="button" style="background: #ffb3b3; border-color: #ffb3b3; color: rgba(0,0,0,0.7);">
  144. Button
  145. </a>
  146. </td>
  147. </tr>
  148. <tr>
  149. <td>
  150. <span class="color" style="background: #ffbc6b;"></span>
  151. <code>#ffbc6b</code>
  152. </td>
  153. <td>
  154. <code>0.63053</code>
  155. </td>
  156. <td>
  157. <span class="color" style="background: rgba(0,0,0,0.7);"></span>
  158. <code>rgba(0,0,0,0.7)</code>
  159. </td>
  160. <td>
  161. <a class="button" style="background: #ffbc6b; border-color: #ffbc6b; color: rgba(0,0,0,0.7);">
  162. Button
  163. </a>
  164. </td>
  165. </tr>
  166. <tr>
  167. <td>
  168. <span class="color" style="background: hsl(294, 71%, 79%);"></span>
  169. <code>hsl(294, 71%, 79%)</code>
  170. </td>
  171. <td>
  172. <code>0.5529</code>
  173. </td>
  174. <td>
  175. <span class="color" style="background: rgba(0,0,0,0.7);"></span>
  176. <code>rgba(0,0,0,0.7)</code>
  177. </td>
  178. <td>
  179. <a class="button" style="background: hsl(294, 71%, 79%); border-color: hsl(294, 71%, 79%); color: rgba(0,0,0,0.7);">
  180. Button
  181. </a>
  182. </td>
  183. </tr>
  184. </tbody>
  185. </table>
  186. <p>
  187. For colors that have a luminance close to the <code>0.55</code> threshold, it can be useful to <strong>override</strong> the <code>findColorInvert()</code> function, and rather set the invert color <strong>manually.</strong>
  188. <br>
  189. For example, this shade of <span class="color" style="background: hsl(294, 71%, 79%); float: none; height: 16px; width: 16px; margin-right: 0; vertical-align: middle;"></span> purple has a color luminance of <code>0.5529</code>. It can be preferable to set a color invert of white instead of transparent black:
  190. </p>
  191. <table class="table is-bordered">
  192. <tbody>
  193. <tr>
  194. <th>
  195. with <code>findColorInvert()</code>
  196. </th>
  197. <td>
  198. <code>$purple-invert: findColorInvert($purple)</code>
  199. </td>
  200. <td>
  201. <span class="color" style="background: rgba(0,0,0,0.7);"></span>
  202. <code>rgba(0,0,0,0.7)</code>
  203. </td>
  204. <td>
  205. <a class="button" style="background: hsl(294, 71%, 79%); border-color: hsl(294, 71%, 79%); color: rgba(0,0,0,0.7);">
  206. Button
  207. </a>
  208. </td>
  209. </tr>
  210. <tr>
  211. <th>
  212. with manual setting
  213. </th>
  214. <td>
  215. <code>$purple-invert: #fff</code>
  216. </td>
  217. <td>
  218. <span class="color" style="background: #fff;"></span>
  219. <code>#fff</code>
  220. </td>
  221. <td>
  222. <a class="button" style="background: hsl(294, 71%, 79%); border-color: hsl(294, 71%, 79%); color: #fff;">
  223. Button
  224. </a>
  225. </td>
  226. </tr>
  227. </tbody>
  228. </table>
  229. </div>
  230. </div>
  231. </section>