Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function generateImage(picture, name){
- if( picture != null )
- {
- return '<img src="' + picture + '" alt="" />';
- }
- else
- {
- // http://jsfiddle.net/tHCsq/2/ - generate colors
- // http://jsfiddle.net/6PfsW/2/ - generate array
- // http://jsfiddle.net/LP8kr/ - shuffling
- var colors = ['#007f3f', '#3f7f00', '#b20094', '#b27600', '#997f00', '#990019', '#007f00', '#003bb2', '#7f3f00', '#7700b2', '#990066', '#007f7f', '#00007f', '#1d00b2', '#b21d00', '#7f7f00', '#7f003f', '#00b21d', '#009933', '#993200', '#320099', '#7f007f', '#00b276', '#001999', '#669900', '#3f007f', '#b2003b', '#7f0000', '#7f0099', '#199900', '#94b200', '#0094b2', '#003f7f', '#3bb200', '#006599', '#00997f'];
- return '<div class="fake-img"><div class="fake-img-bg" style="background: ' + colors[stringValue(name)] + '">' + nameInitials(name) + '</div></div>';
- }
- }
- function stringValue(string){
- var sum = 0;
- for( var i = 0; i < string.length; i++ )
- {
- var code = string.charCodeAt(i);
- sum ^= code * (i+1);
- }
- return parseInt(Math.round(sum % 36));
- }
- function nameInitials(name){
- var initials = '';
- name = '' + removeAccents(name);
- var matches = name.match(/\b(\w)/g);
- for( var i = 0; i < matches.length; i++ )
- {
- initials += matches[i];
- }
- if( initials.length > 2 )
- {
- return initials.substr(0, 2);
- }
- if( initials.length < 2 )
- {
- return name.substr(0, 2);
- }
- return initials;
- }
- function removeAccents(input){
- var accents = 'ÀÁÂÃÄÅàáâãäåÒÓÔÕÕÖØòóôõöøÈÉÊËèéêëðÇČçčÐĎďÌÍÎÏìíîïĽľÙÚÛÜùúûüÑŇñňŠšŤťŸÿýŽž ́', // need to hack accent
- accentsOut = 'AAAAAAaaaaaaOOOOOOOooooooEEEEeeeeeCCccDDdIIIIiiiiLlUUUUuuuuNNnnSsTtYyyZz ',
- output = '',
- index = -1;
- for( var i = 0; i < input.length; i++ )
- {
- index = accents.indexOf(input[i]);
- if( index != -1 )
- {
- output += typeof accentsOut[index] != 'undefined' ? accentsOut[index] : '';
- }
- else
- {
- output += typeof input[i] != 'undefined' ? input[i] : '';
- }
- }
- return output;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement