functions.html 7.1 KB

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