functions.html 8.1 KB

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