placeholder.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /**
  2. * ownCloud
  3. *
  4. * @author John Molakvoæ
  5. * @copyright 2016-2018 John Molakvoæ <skjnldsv@protonmail.com>
  6. * @author Morris Jobke
  7. * @copyright 2013 Morris Jobke <morris.jobke@gmail.com>
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  11. * License as published by the Free Software Foundation; either
  12. * version 3 of the License, or any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public
  20. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. /*
  24. * Adds a background color to the element called on and adds the first character
  25. * of the passed in string. This string is also the seed for the generation of
  26. * the background color.
  27. *
  28. * You have following HTML:
  29. *
  30. * <div id="albumart"></div>
  31. *
  32. * And call this from Javascript:
  33. *
  34. * $('#albumart').imageplaceholder('The Album Title');
  35. *
  36. * Which will result in:
  37. *
  38. * <div id="albumart" style="background-color: rgb(121, 90, 171); ... ">T</div>
  39. *
  40. * You may also call it like this, to have a different background, than the seed:
  41. *
  42. * $('#albumart').imageplaceholder('The Album Title', 'Album Title');
  43. *
  44. * Resulting in:
  45. *
  46. * <div id="albumart" style="background-color: rgb(121, 90, 171); ... ">A</div>
  47. *
  48. */
  49. /*
  50. * Alternatively, you can use the prototype function to convert your string to rgb colors:
  51. *
  52. * "a6741a86aded5611a8e46ce16f2ad646".toRgb()
  53. *
  54. * Will return the rgb parameters within the following object:
  55. *
  56. * Color {r: 208, g: 158, b: 109}
  57. *
  58. */
  59. (function ($) {
  60. String.prototype.toRgb = function() {
  61. var hash = this.toLowerCase().replace(/[^0-9a-f]+/g, '');
  62. // Already a md5 hash?
  63. if( !hash.match(/^[0-9a-f]{32}$/g) ) {
  64. hash = md5(hash);
  65. }
  66. function Color(r,g,b) {
  67. this.r = r;
  68. this.g = g;
  69. this.b = b;
  70. }
  71. function stepCalc(steps, ends) {
  72. var step = new Array(3);
  73. step[0] = (ends[1].r - ends[0].r) / steps;
  74. step[1] = (ends[1].g - ends[0].g) / steps;
  75. step[2] = (ends[1].b - ends[0].b) / steps;
  76. return step;
  77. }
  78. function mixPalette(steps, color1, color2) {
  79. var count = steps + 1;
  80. var palette = new Array();
  81. palette.push(color1);
  82. var step = stepCalc(steps, [color1, color2])
  83. for (i = 1; i < steps; i++) {
  84. var r = parseInt(color1.r + (step[0] * i));
  85. var g = parseInt(color1.g + (step[1] * i));
  86. var b = parseInt(color1.b + (step[2] * i));
  87. palette.push(new Color(r,g,b));
  88. }
  89. return palette;
  90. }
  91. var red = new Color(182, 70, 157);
  92. var yellow = new Color(221, 203, 85);
  93. var blue = new Color(0, 130, 201); // Nextcloud blue
  94. // Number of steps to go from a color to another
  95. // 3 colors * 6 will result in 18 generated colors
  96. var steps = 6;
  97. var palette1 = mixPalette(steps, red, yellow);
  98. var palette2 = mixPalette(steps, yellow, blue);
  99. var palette3 = mixPalette(steps, blue, red);
  100. var finalPalette = palette1.concat(palette2).concat(palette3);
  101. // Convert a string to an integer evenly
  102. function hashToInt(hash, maximum) {
  103. var finalInt = 0;
  104. var result = Array();
  105. // Splitting evenly the string
  106. for (var i in hash) {
  107. // chars in md5 goes up to f, hex:16
  108. result.push(parseInt(hash.charAt(i), 16) % 16);
  109. }
  110. // Adds up all results
  111. for (var j in result) {
  112. finalInt += result[j];
  113. }
  114. // chars in md5 goes up to f, hex:16
  115. // make sure we're always using int in our operation
  116. return parseInt(parseInt(finalInt) % maximum);
  117. }
  118. return finalPalette[hashToInt(hash, steps * 3 )];
  119. };
  120. $.fn.imageplaceholder = function(seed, text, size) {
  121. text = text || seed;
  122. // Compute the hash
  123. var rgb = seed.toRgb();
  124. this.css('background-color', 'rgb('+rgb.r+', '+rgb.g+', '+rgb.b+')');
  125. // Placeholders are square
  126. var height = this.height() || size || 32;
  127. this.height(height);
  128. this.width(height);
  129. // CSS rules
  130. this.css('color', '#fff');
  131. this.css('font-weight', 'normal');
  132. this.css('text-align', 'center');
  133. // calculate the height
  134. this.css('line-height', height + 'px');
  135. this.css('font-size', (height * 0.55) + 'px');
  136. if(seed !== null && seed.length) {
  137. this.html(text[0].toUpperCase());
  138. }
  139. };
  140. $.fn.clearimageplaceholder = function() {
  141. this.css('background-color', '');
  142. this.css('color', '');
  143. this.css('font-weight', '');
  144. this.css('text-align', '');
  145. this.css('line-height', '');
  146. this.css('font-size', '');
  147. this.html('');
  148. this.removeClass('icon-loading');
  149. };
  150. }(jQuery));