@nextcloud/vue
Version:
Nextcloud vue components
1 lines • 3.07 kB
Source Map (JSON)
{"version":3,"file":"usernameToColor.cjs","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":"kFA+BMA,EAAkB,SAASC,EAAU,CAE1C,IAAIC,EAAOD,EAAS,YAAa,EAG7BC,EAAK,MAAM,sBAAsB,IAAM,OAC1CA,EAAOC,EAAID,CAAI,GAGhBA,EAAOA,EAAK,QAAQ,aAAc,EAAE,EAEpC,MAAME,EAAQ,EACRC,EAAeC,EAAS,UAACF,CAAK,EAQpC,SAASG,EAAUL,EAAMM,EAAS,CACjC,IAAIC,EAAW,EACf,MAAMC,EAAS,CAAE,EAGjB,QAASC,EAAI,EAAGA,EAAIT,EAAK,OAAQS,IAEhCD,EAAO,KAAK,SAASR,EAAK,OAAOS,CAAC,EAAG,EAAE,EAAI,EAAE,EAI9C,UAAWC,KAAKF,EACfD,GAAYC,EAAOE,CAAC,EAKrB,OAAO,SAAS,SAASH,EAAU,EAAE,EAAID,EAAS,EAAE,CACpD,CACD,OAAOH,EAAaE,EAAUL,EAAME,EAAQ,CAAC,CAAC,CAC/C"}