Skip to content

Commit

Permalink
avatar crafter color getter
Browse files Browse the repository at this point in the history
  • Loading branch information
dmstern committed Aug 31, 2018
1 parent 6fb6d62 commit 34f26bb
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 35 deletions.
6 changes: 4 additions & 2 deletions server/mock/fractal-menu-enhancer.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@
{
"name": "Morgenstern, Daniel (init)",
"email": "daniel.morgenstern@init.de"
}
},
"daniel <mail@daniel.com>"
],
"main": "src/main.js",
"dist": {
Expand Down Expand Up @@ -230,6 +231,7 @@
{
"name": "Morgenstern, Daniel (init)",
"email": "daniel.morgenstern@init.de"
}
},
"daniel <mail@daniel.com>"
]
}
76 changes: 43 additions & 33 deletions src/model/Crafter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,39 +19,6 @@ export default class Crafter implements SearchComparable {
}
}

public get color(): string {
const initials = this.initials;
const key = this.email
? this.email.replace(/ /g, '')
: this.name
? this.name.replace(/ /g, '')
: '';
if (key && Crafter.colors[key]) {
return Crafter.colors[key];
}

if (initials) {
let colorKeyNumber =
(initials.charCodeAt(0) + initials.charCodeAt(0)) % (Object.keys(colors).length - 2);
Crafter.colors.forEach((oldKey) => {
if (oldKey === colorKeyNumber) {
colorKeyNumber++;
if (colorKeyNumber >= Object.keys(colors).length - 2) {
colorKeyNumber -= 2;
}
}
});
const colorKey = Object.keys(colors)[colorKeyNumber];
const color = colorKey
.replace(
/(?:^|\.?)([A-Z])/g, (x, y) => '-' + y.toLowerCase(),
).replace(/^-/, '');
Crafter.colors.set(key, colorKeyNumber);
return color;
}
return 'accent';
}

private static colors: Map<string, number> = new Map<string, number>();

public readonly name?: string;
Expand All @@ -60,6 +27,13 @@ export default class Crafter implements SearchComparable {

private backgroundColor?: string;

public get color() {
if (this.backgroundColor) {
return this.backgroundColor;
}
return this.backgroundColor = this.generateColor();
}

constructor(author?: IAuthor | string) {
if (author) {
if (typeof author === 'string') {
Expand Down Expand Up @@ -98,4 +72,40 @@ export default class Crafter implements SearchComparable {
&& this.email === other.email
&& this.url === other.url;
}

private generateColor(): string {
const initials = this.initials;
const key = this.email
? this.email.replace(/ /g, '')
: this.name
? this.name.replace(/ /g, '')
: '';
if (key && Crafter.colors[key]) {
return Crafter.colors[key];
}

if (initials) {
let colorKeyNumber =
(initials.charCodeAt(0) + initials.charCodeAt(0)) % (Object.keys(colors).length - 2);
Crafter.colors.forEach((oldKey) => {
if (oldKey === colorKeyNumber) {
colorKeyNumber++;
if (colorKeyNumber >= Object.keys(colors).length - 2) {
colorKeyNumber -= 2;
}
}
});
const colorKey = Object.keys(colors)[colorKeyNumber];
const color = colorKey
.replace(
/(?:^|\.?)([A-Z])/g, (x, y) => '-' + y.toLowerCase(),
).replace(/^-/, '');
Crafter.colors.set(key, colorKeyNumber);
this.backgroundColor = color;
} else {
this.backgroundColor = 'accent';
}
return this.backgroundColor;
}

}

0 comments on commit 34f26bb

Please sign in to comment.