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
  • Loading branch information
Maskedman99 committed Mar 25, 2020
1 parent e43e3be commit adef1b3
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/utils/color.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ export const foregroundColorFromBackground = (color: ColorValue): 'black' | 'whi

export const colorHashFromString = (name: string): string => {
let hash = 0;
for (let i = name.length - 1; i >= 0; i--) {
hash = hash * 31 + name.codePointAt(i);
for (let i = 1; i < name.length; i++) {
hash = hash * 31 + name.charCodeAt(i);
hash %= 0x1000000;
}

let colorHash = hash.toString(16);
while (colorHash.length < 6) {
colorHash += '0';
}
colorHash = colorHash.padEnd(6, '0');
colorHash = colorHash.substring(3) + colorHash.substring(0, 3); // 3 Left Cyclic Shifts

return `#${colorHash}`;
};

0 comments on commit adef1b3

Please sign in to comment.