Skip to content

Commit

Permalink
generate text avatars
Browse files Browse the repository at this point in the history
  • Loading branch information
adbenitez committed Sep 1, 2023
1 parent 7476db1 commit 1a641ad
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 4 deletions.
1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@ionic/react-router": "^7.0.0",
"@types/react-router": "^5.1.20",
"@types/react-router-dom": "^5.3.3",
"consistent-color-generation": "^0.4.0",
"fuse.js": "^6.6.2",
"ionicons": "^7.1.2",
"preact": "^10.17.0",
Expand Down
18 changes: 18 additions & 0 deletions frontend/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion frontend/public/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.2.1
2.3.0
5 changes: 2 additions & 3 deletions frontend/src/components/BotItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from "@ionic/react";
import { languageOutline, openOutline } from "ionicons/icons";

import TextAvatar from "./TextAvatar";
import { Bot, recently } from "../store";
import { getText as _ } from "../i18n";
import "./BotItem.css";
Expand Down Expand Up @@ -52,9 +53,7 @@ export default function BotItem({
href={bot.url}
>
<IonChip>
<IonAvatar>
<img src="icon.png" />
</IonAvatar>
<TextAvatar text={bot.addr} />
<IonLabel>{bot.addr} </IonLabel>
<IonIcon icon={openOutline} />
</IonChip>
Expand Down
21 changes: 21 additions & 0 deletions frontend/src/components/TextAvatar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { IonAvatar } from "@ionic/react";
// @ts-ignore
import getRGB from "consistent-color-generation";

export default function TextAvatar({ text }: { text: string }) {
const backgroundColor = getRGB(text).toString();
const textColor = "#ffffff";
const fontFamily = "Arial";
const fontSize = 50;
const svg = btoa(
`<svg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' viewBox='0 0 100 100' width='100' height='100'><rect width='100' height='100' x='0' y='0' fill='${backgroundColor}'></rect><text x='50%' y='50%' alignment-baseline='central' text-anchor='middle' font-family='${fontFamily}' font-size='${fontSize}' fill='${textColor}' dominant-baseline='middle'>${text.charAt(
0,
)}</text></svg>`,
);
const url = `data:image/svg+xml;base64,${svg}`;
return (
<IonAvatar>
<img src={url} />
</IonAvatar>
);
}

0 comments on commit 1a641ad

Please sign in to comment.