Skip to content

Commit

Permalink
group-pm: fix colorHashFromString
Browse files Browse the repository at this point in the history
colorHashFromString() now considers all charecters in the string
it can also now return colors with no red component in it. Also fixed
the cases where the function returned #NaN for very long or
empty strings.

Fixes: #3985

group-pm: fix colorHashFromString

colorHashFromString() now considers all charecters in the string
it can also now return colors with no red component in it. Also fixed
the cases where the function returned #NaN for very long or
empty strings.

Fixes: #3985
  • Loading branch information
Maskedman99 committed Mar 25, 2020
1 parent 658003e commit 63bc582
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/utils/color.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ export const foregroundColorFromBackground = (color: ColorValue): 'black' | 'whi

export const colorHashFromString = (name: string): string => {
let hash = 0;
for (let i = 0; i < name.length; i++) {
hash = hash * 31 + name.charCodeAt(1);
for (let i = 1; i < name.length; i++) {
hash = hash * 31 + name.charCodeAt(i);
hash %= 0x1000000;
}
let colorHash = hash % 0xffffff;
if (colorHash < 0x100000) {
colorHash += 0x100000;
}
return `#${colorHash.toString(16)}`;

let colorHash = hash.toString(16);
colorHash = colorHash.padStart(6, '0');

return `#${colorHash}`;
};

0 comments on commit 63bc582

Please sign in to comment.