@nextcloud/vue
Version:
Nextcloud vue components
1 lines • 3.08 kB
Source Map (JSON)
{"version":3,"file":"usernameToColor.mjs","sources":["../../src/functions/usernameToColor/usernameToColor.js"],"sourcesContent":["/**\n * @copyright Copyright (c) 2018 John Molakvoæ <skjnldsv@protonmail.com>\n *\n * @author John Molakvoæ <skjnldsv@protonmail.com>\n *\n * @license AGPL-3.0-or-later\n *\n * This program is free software: you can redistribute it and/or modify\n * it under the terms of the GNU Affero General Public License as\n * published by the Free Software Foundation, either version 3 of the\n * License, or (at your option) any later version.\n *\n * This program is distributed in the hope that it will be useful,\n * but WITHOUT ANY WARRANTY; without even the implied warranty of\n * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n * GNU Affero General Public License for more details.\n *\n * You should have received a copy of the GNU Affero General Public License\n * along with this program. If not, see <http://www.gnu.org/licenses/>.\n *\n */\nimport GenColors from '../../utils/GenColors.js'\n\nimport md5 from 'md5'\n\n/**\n * Originally taken from https://github.com/nextcloud/server/blob/master/core/js/placeholder.js\n *\n * @param {string} username Display name or user id to generate from\n * @return {object} the rgb colors as {r:255, g:255, b:255}\n */\nconst usernameToColor = function(username) {\n\t// Normalize hash\n\tlet hash = username.toLowerCase()\n\n\t// Already a md5 hash?\n\tif (hash.match(/^([0-9a-f]{4}-?){8}$/) === null) {\n\t\thash = md5(hash)\n\t}\n\n\thash = hash.replace(/[^0-9a-f]/g, '')\n\n\tconst steps = 6\n\tconst finalPalette = GenColors(steps)\n\n\t/**\n\t * Convert a string to an integer evenly\n\t *\n\t * @param {string} hash The hash to convert\n\t * @param {number} maximum Largest number allowed\n\t */\n\tfunction hashToInt(hash, maximum) {\n\t\tlet finalInt = 0\n\t\tconst result = []\n\n\t\t// Splitting evenly the string\n\t\tfor (let i = 0; i < hash.length; i++) {\n\t\t\t// chars in md5 goes up to f, hex:16\n\t\t\tresult.push(parseInt(hash.charAt(i), 16) % 16)\n\t\t}\n\n\t\t// Adds up all results\n\t\tfor (const j in result) {\n\t\t\tfinalInt += result[j]\n\t\t}\n\n\t\t// chars in md5 goes up to f, hex:16\n\t\t// make sure we're always using int in our operation\n\t\treturn parseInt(parseInt(finalInt, 10) % maximum, 10)\n\t}\n\treturn finalPalette[hashToInt(hash, steps * 3)]\n}\n\nexport default usernameToColor\n"],"names":["usernameToColor","username","hash","md5","steps","finalPalette","GenColors","hashToInt","maximum","finalInt","result","i","j"],"mappings":";;AA+BK,MAACA,IAAkB,SAASC,GAAU;AAE1C,MAAIC,IAAOD,EAAS,YAAa;AAG7BC,IAAK,MAAM,sBAAsB,MAAM,SAC1CA,IAAOC,EAAID,CAAI,IAGhBA,IAAOA,EAAK,QAAQ,cAAc,EAAE;AAEpC,QAAME,IAAQ,GACRC,IAAeC,EAAUF,CAAK;AAQpC,WAASG,EAAUL,GAAMM,GAAS;AACjC,QAAIC,IAAW;AACf,UAAMC,IAAS,CAAE;AAGjB,aAASC,IAAI,GAAGA,IAAIT,EAAK,QAAQS;AAEhCD,QAAO,KAAK,SAASR,EAAK,OAAOS,CAAC,GAAG,EAAE,IAAI,EAAE;AAI9C,eAAWC,KAAKF;AACfD,MAAAA,KAAYC,EAAOE,CAAC;AAKrB,WAAO,SAAS,SAASH,GAAU,EAAE,IAAID,GAAS,EAAE;AAAA,EACpD;AACD,SAAOH,EAAaE,EAAUL,GAAME,IAAQ,CAAC,CAAC;AAC/C;"}