Skip to content

Commit

Permalink
reporter-webapp: Generate color for each license
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Steenbergen <thomas.steenbergen@here.com>
  • Loading branch information
tsteenbe committed Jun 10, 2021
1 parent 59ff6be commit 8c9e29b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
10 changes: 10 additions & 0 deletions reporter-web-app/src/models/WebAppLicense.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@
* License-Filename: LICENSE
*/

import { licenseToHslColor } from '../utils';

class WebAppLicense {
#_id;

#id;

#color;

constructor(obj) {
if (obj) {
if (Number.isInteger(obj._id)) {
Expand All @@ -31,6 +35,8 @@ class WebAppLicense {
if (obj.id) {
this.#id = obj.id;
}

this.#color = licenseToHslColor(this.#id);
}
}

Expand All @@ -41,6 +47,10 @@ class WebAppLicense {
get id() {
return this.#id;
}

get color() {
return this.#color;
}
}

export default WebAppLicense;
17 changes: 16 additions & 1 deletion reporter-web-app/src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,19 @@ const randomStringGenerator = (length = (Math.floor(Math.random() * 501) + 20))
return alpha(l, symbol(1, alpha(r, []))).join('');
};

export { randomStringGenerator };
// Utility function to generate color for given license string
const licenseToHslColor = (license) => {
let hash = 0;
let random = Math.floor(Math.random() * 20 + 5);

for (let i = 0; i < license.length; i++) {
hash = license.charCodeAt(i) + ((hash << 5) - hash);
hash |= 0;
}

return 'hsl(' + ((Math.abs(hash) % 320) + random) + ',' +
(25 + 70 * Math.random()) + '%,' +
(55 + 10 * Math.random()) + '%)';
}

export { randomStringGenerator, licenseToHslColor };

0 comments on commit 8c9e29b

Please sign in to comment.