12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- // @flow
-
- import _ from 'lodash';
-
- const AVATAR_COLORS = [
- '232, 105, 156',
- '255, 198, 115',
- '128, 128, 255',
- '105, 232, 194',
- '234, 255, 128'
- ];
-
- const AVATAR_OPACITY = 0.4;
-
- /**
- * Generates the background color of an initials based avatar.
- *
- * @param {string?} initials - The initials of the avatar.
- * @param {Array<strig>} customAvatarBackgrounds - Custom avatar background values.
- * @returns {string}
- */
- export function getAvatarColor(initials: ?string, customAvatarBackgrounds: Array<string>) {
- const hasCustomAvatarBackgronds = customAvatarBackgrounds && customAvatarBackgrounds.length;
- const colorsBase = hasCustomAvatarBackgronds ? customAvatarBackgrounds : AVATAR_COLORS;
-
- let colorIndex = 0;
-
- if (initials) {
- let nameHash = 0;
-
- for (const s of initials) {
- nameHash += s.codePointAt(0);
- }
-
- colorIndex = nameHash % colorsBase.length;
- }
-
- return hasCustomAvatarBackgronds ? colorsBase[colorIndex] : `rgba(${colorsBase[colorIndex]}, ${AVATAR_OPACITY})`;
- }
-
- /**
- * Generates initials for a simple string.
- *
- * @param {string?} s - The string to generate initials for.
- * @returns {string?}
- */
- export function getInitials(s: ?string) {
- // We don't want to use the domain part of an email address, if it is one
- const initialsBasis = _.split(s, '@')[0];
- const words = _.words(initialsBasis);
- let initials = '';
-
- for (const w of words) {
- (initials.length < 2) && (initials += w.substr(0, 1).toUpperCase());
- }
-
- return initials;
- }
|