Skip to content

Commit 3c9f7fc

Browse files
(feat) add user initials icon component (#86)
added default user icon Signed-off-by: Benjamin Strasser <bp.strasser@gmail.com>
1 parent a872f58 commit 3c9f7fc

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import UserDefaultIcon from './user-default-icon.svelte'
2+
3+
export { UserDefaultIcon }
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<script lang="ts">
2+
export const firstname: string = 'Nomen'
3+
export const lastname: string = 'Nescio'
4+
5+
function hashCode(str: string) {
6+
let hash = 0
7+
for (let i = 0, len = str.length; i < len; i++) {
8+
let chr = str.charCodeAt(i)
9+
hash = (hash << 3) - hash + chr
10+
hash |= 0
11+
}
12+
13+
return Math.abs(hash)
14+
}
15+
16+
function generateColor(firstname: string, lastname: string) {
17+
const hue = hashCode(firstname + lastname) % 360
18+
const saturation = 40
19+
const lightness = 55
20+
21+
return `hsl(${hue},${saturation}%,${lightness}%)`
22+
}
23+
24+
const backgroundColor = generateColor(firstname, lastname)
25+
const initials = `${firstname.charAt(0).toUpperCase()}${lastname.charAt(0).toUpperCase()}`
26+
</script>
27+
28+
<div
29+
class="text- flex h-10 w-10 items-center justify-center rounded-full text-xl"
30+
style="background-color: {backgroundColor};"
31+
>
32+
{initials}
33+
</div>

0 commit comments

Comments
 (0)