ui-avatars 1.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright 2026 Steve Lacey
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: ui-avatars
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Deterministic avatar URLs: Gravatar photos with a colorful initials fallback.
|
|
5
|
+
License: MIT
|
|
6
|
+
License-File: LICENSE.md
|
|
7
|
+
Keywords: avatar,gravatar,initials,placeholder,ui-avatars
|
|
8
|
+
Author: Steve Lacey
|
|
9
|
+
Author-email: steve@py.steve.ly
|
|
10
|
+
Requires-Python: >=3.10,<4.0
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
18
|
+
Requires-Dist: tailwind_colors (>=1.4.1,<2.0.0)
|
|
19
|
+
Project-URL: Homepage, https://github.com/stevelacey/ui-avatars
|
|
20
|
+
Project-URL: Repository, https://github.com/stevelacey/ui-avatars
|
|
21
|
+
Description-Content-Type: text/markdown
|
|
22
|
+
|
|
23
|
+
# ui-avatars
|
|
24
|
+
|
|
25
|
+
[](https://github.com/stevelacey/ui-avatars/actions)
|
|
26
|
+
[](https://pypi.org/project/ui-avatars/)
|
|
27
|
+
[](https://pypi.org/project/ui-avatars/)
|
|
28
|
+
[](LICENSE.md)
|
|
29
|
+
|
|
30
|
+
Deterministic avatar URLs that work great on both light and dark designs: a real
|
|
31
|
+
[Gravatar](https://gravatar.com) photo if there's an email, otherwise colored initials
|
|
32
|
+
from a hash via [ui-avatars.com](https://ui-avatars.com). Same input, same URL, always.
|
|
33
|
+
|
|
34
|
+
<table>
|
|
35
|
+
<tr>
|
|
36
|
+
<td align="center"><img src="https://ui-avatars.com/api/Ada%20Lovelace/128/f9731633/f97316/2/0.4/0/1/1/svg" width="72" height="72"></td>
|
|
37
|
+
<td align="center"><img src="https://www.gravatar.com/avatar/7487baad7b087cb03cc4ccf7d044c150?s=128&d=https%3A%2F%2Fui-avatars.com%2Fapi%2FSteve%2F128%2Frgba%28236%2C72%2C153%2C0.2%29%2Fec4899%2F2%2F0.4%2F0%2F1%2F1%2Fpng" width="72" height="72"></td>
|
|
38
|
+
<td align="center"><img src="https://ui-avatars.com/api/Alan%20Turing/128/84cc1633/84cc16/2/0.4/0/1/1/svg" width="72" height="72"></td>
|
|
39
|
+
<td align="center"><img src="https://ui-avatars.com/api/Linus%20Torvalds/128/06b6d433/06b6d4/2/0.4/0/1/1/svg" width="72" height="72"></td>
|
|
40
|
+
<td align="center"><img src="https://www.gravatar.com/avatar/629999fcb3f6a928abe5f65ed0ab09c2?s=128&d=https%3A%2F%2Fui-avatars.com%2Fapi%2FPaul%2520Irish%2F128%2Frgba%2859%2C130%2C246%2C0.2%29%2F3b82f6%2F2%2F0.4%2F0%2F1%2F1%2Fpng" width="72" height="72"></td>
|
|
41
|
+
<td align="center"><img src="https://ui-avatars.com/api/Margaret%20Hamilton/128/3b82f633/3b82f6/2/0.4/0/1/1/svg" width="72" height="72"></td>
|
|
42
|
+
<td align="center"><img src="https://www.gravatar.com/avatar/3869e93ce6b2a22e37cf13281d9e6f75?s=128&d=https%3A%2F%2Fui-avatars.com%2Fapi%2FLea%2520Verou%2F128%2Frgba%28139%2C92%2C246%2C0.2%29%2F8b5cf6%2F2%2F0.4%2F0%2F1%2F1%2Fpng" width="72" height="72"></td>
|
|
43
|
+
<td align="center"><img src="https://www.gravatar.com/avatar/047f2332cde3730f1ed661eebb0c5686?s=128&d=https%3A%2F%2Fui-avatars.com%2Fapi%2FGuido%2520van%2520Rossum%2F128%2Frgba%28139%2C92%2C246%2C0.2%29%2F8b5cf6%2F2%2F0.4%2F0%2F1%2F1%2Fpng" width="72" height="72"></td>
|
|
44
|
+
</tr>
|
|
45
|
+
<tr>
|
|
46
|
+
<td align="center"><img src="https://www.gravatar.com/avatar/b8605bc56f7d1249560eb40f7cc69001?s=128&d=https%3A%2F%2Fui-avatars.com%2Fapi%2FCassidy%2520Williams%2F128%2Frgba%2820%2C184%2C166%2C0.2%29%2F14b8a6%2F2%2F0.4%2F0%2F1%2F1%2Fpng" width="72" height="72"></td>
|
|
47
|
+
<td align="center"><img src="https://www.gravatar.com/avatar/8d924f842e67d03be5d0812563e8d672?s=256&d=https%3A%2F%2Fui-avatars.com%2Fapi%2FBig%2520Avatar%2F256%2Frgba%2834%2C197%2C94%2C0.2%29%2F22c55e%2F2%2F0.4%2F0%2F1%2F1%2Fpng" width="72" height="72"></td>
|
|
48
|
+
<td align="center"><img src="https://www.gravatar.com/avatar/de3fe73f6cd2adaac3a8a1fa04d4d80f?s=128&d=https%3A%2F%2Fui-avatars.com%2Fapi%2FChris%2520Coyier%2F128%2Frgba%28249%2C115%2C22%2C0.2%29%2Ff97316%2F2%2F0.4%2F0%2F1%2F1%2Fpng" width="72" height="72"></td>
|
|
49
|
+
<td align="center"><img src="https://ui-avatars.com/api/Brand%20Palette/128/1d4ed833/1d4ed8/2/0.4/0/1/1/svg" width="72" height="72"></td>
|
|
50
|
+
<td align="center"><img src="https://ui-avatars.com/api/Tim%20Berners-Lee/128/84cc1633/84cc16/2/0.4/0/1/1/svg" width="72" height="72"></td>
|
|
51
|
+
<td align="center"><img src="https://www.gravatar.com/avatar/21a241aa10ee457a5f6b72aca98a4860?s=128&d=https%3A%2F%2Fui-avatars.com%2Fapi%2FWade%2520Williams%2F128%2Frgba%2834%2C197%2C94%2C0.2%29%2F22c55e%2F2%2F0.4%2F0%2F1%2F1%2Fpng" width="72" height="72"></td>
|
|
52
|
+
<td align="center"><img src="https://ui-avatars.com/api/Katherine%20Johnson/128/10b98133/10b981/2/0.4/0/1/1/svg" width="72" height="72"></td>
|
|
53
|
+
<td align="center"><img src="https://www.gravatar.com/avatar/f1e3ab214a976a39cfd713bc93deb10f?s=128&d=https%3A%2F%2Fui-avatars.com%2Fapi%2FTJ%2520Holowaychuk%2F128%2Frgba%2814%2C165%2C233%2C0.2%29%2F0ea5e9%2F2%2F0.4%2F0%2F1%2F1%2Fpng" width="72" height="72"></td>
|
|
54
|
+
</tr>
|
|
55
|
+
</table>
|
|
56
|
+
|
|
57
|
+
## Install
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
pip install ui-avatars
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Usage
|
|
64
|
+
|
|
65
|
+
```python
|
|
66
|
+
from ui_avatars import avatar_url
|
|
67
|
+
|
|
68
|
+
avatar_url(name="Ada Lovelace", email="ada@example.com")
|
|
69
|
+
# Gravatar photo, falling back to generated initials
|
|
70
|
+
|
|
71
|
+
avatar_url(name="Ada Lovelace")
|
|
72
|
+
# straight ui-avatars.com URL, no Gravatar lookup
|
|
73
|
+
|
|
74
|
+
avatar_url(email="ada@example.com")
|
|
75
|
+
# initials from the first two letters of the email
|
|
76
|
+
|
|
77
|
+
avatar_url(name="Ada Lovelace", alpha=0.75, size=256, rounded=True)
|
|
78
|
+
# alpha, size, and ui-avatars.com rendering options as one-off overrides
|
|
79
|
+
|
|
80
|
+
avatar_url(name="Ada Lovelace", color="#dc2626")
|
|
81
|
+
# pick a specific color, skipping the palette
|
|
82
|
+
|
|
83
|
+
avatar_url(name="Ada Lovelace", alpha=1, color=("#dc2626", "#fff"))
|
|
84
|
+
# supply background and text color separately via a tuple
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## Configuration
|
|
88
|
+
|
|
89
|
+
Reconfigure the shared `avatars` instance once, at startup, to change the palette for
|
|
90
|
+
every call:
|
|
91
|
+
|
|
92
|
+
```python
|
|
93
|
+
from ui_avatars import avatars
|
|
94
|
+
|
|
95
|
+
avatars.configure(colors=["#1d4ed8", "#b91c1c"], size=256, rounded=True)
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Or build your own instance instead:
|
|
99
|
+
|
|
100
|
+
```python
|
|
101
|
+
from ui_avatars import Avatars
|
|
102
|
+
|
|
103
|
+
avatars = Avatars(colors=["#1d4ed8", "#b91c1c"], size=256)
|
|
104
|
+
avatars.build(name="Ada Lovelace", email="ada@example.com")
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
The default palette is [`tailwind_colors`](https://pypi.org/project/tailwind_colors/)'s
|
|
108
|
+
`RAINBOW_500`. You can swap the whole palette like so:
|
|
109
|
+
|
|
110
|
+
```python
|
|
111
|
+
from tailwind_colors import TCH
|
|
112
|
+
|
|
113
|
+
avatars.configure(colors=TCH.RAINBOW_300)
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
For an independent background/text color instead of one tinted from the other, use a
|
|
117
|
+
`(background, text)` tuple as a `colors` entry — `alpha` still applies to whichever
|
|
118
|
+
background results:
|
|
119
|
+
|
|
120
|
+
```python
|
|
121
|
+
avatars.configure(colors=[("#fee2e2", "#ef4444"), ("#ffedd5", "#f97316")])
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
## Development
|
|
125
|
+
|
|
126
|
+
```bash
|
|
127
|
+
poetry install
|
|
128
|
+
poetry run pytest
|
|
129
|
+
poetry run ruff check .
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
## License
|
|
133
|
+
|
|
134
|
+
MIT
|
|
135
|
+
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
# ui-avatars
|
|
2
|
+
|
|
3
|
+
[](https://github.com/stevelacey/ui-avatars/actions)
|
|
4
|
+
[](https://pypi.org/project/ui-avatars/)
|
|
5
|
+
[](https://pypi.org/project/ui-avatars/)
|
|
6
|
+
[](LICENSE.md)
|
|
7
|
+
|
|
8
|
+
Deterministic avatar URLs that work great on both light and dark designs: a real
|
|
9
|
+
[Gravatar](https://gravatar.com) photo if there's an email, otherwise colored initials
|
|
10
|
+
from a hash via [ui-avatars.com](https://ui-avatars.com). Same input, same URL, always.
|
|
11
|
+
|
|
12
|
+
<table>
|
|
13
|
+
<tr>
|
|
14
|
+
<td align="center"><img src="https://ui-avatars.com/api/Ada%20Lovelace/128/f9731633/f97316/2/0.4/0/1/1/svg" width="72" height="72"></td>
|
|
15
|
+
<td align="center"><img src="https://www.gravatar.com/avatar/7487baad7b087cb03cc4ccf7d044c150?s=128&d=https%3A%2F%2Fui-avatars.com%2Fapi%2FSteve%2F128%2Frgba%28236%2C72%2C153%2C0.2%29%2Fec4899%2F2%2F0.4%2F0%2F1%2F1%2Fpng" width="72" height="72"></td>
|
|
16
|
+
<td align="center"><img src="https://ui-avatars.com/api/Alan%20Turing/128/84cc1633/84cc16/2/0.4/0/1/1/svg" width="72" height="72"></td>
|
|
17
|
+
<td align="center"><img src="https://ui-avatars.com/api/Linus%20Torvalds/128/06b6d433/06b6d4/2/0.4/0/1/1/svg" width="72" height="72"></td>
|
|
18
|
+
<td align="center"><img src="https://www.gravatar.com/avatar/629999fcb3f6a928abe5f65ed0ab09c2?s=128&d=https%3A%2F%2Fui-avatars.com%2Fapi%2FPaul%2520Irish%2F128%2Frgba%2859%2C130%2C246%2C0.2%29%2F3b82f6%2F2%2F0.4%2F0%2F1%2F1%2Fpng" width="72" height="72"></td>
|
|
19
|
+
<td align="center"><img src="https://ui-avatars.com/api/Margaret%20Hamilton/128/3b82f633/3b82f6/2/0.4/0/1/1/svg" width="72" height="72"></td>
|
|
20
|
+
<td align="center"><img src="https://www.gravatar.com/avatar/3869e93ce6b2a22e37cf13281d9e6f75?s=128&d=https%3A%2F%2Fui-avatars.com%2Fapi%2FLea%2520Verou%2F128%2Frgba%28139%2C92%2C246%2C0.2%29%2F8b5cf6%2F2%2F0.4%2F0%2F1%2F1%2Fpng" width="72" height="72"></td>
|
|
21
|
+
<td align="center"><img src="https://www.gravatar.com/avatar/047f2332cde3730f1ed661eebb0c5686?s=128&d=https%3A%2F%2Fui-avatars.com%2Fapi%2FGuido%2520van%2520Rossum%2F128%2Frgba%28139%2C92%2C246%2C0.2%29%2F8b5cf6%2F2%2F0.4%2F0%2F1%2F1%2Fpng" width="72" height="72"></td>
|
|
22
|
+
</tr>
|
|
23
|
+
<tr>
|
|
24
|
+
<td align="center"><img src="https://www.gravatar.com/avatar/b8605bc56f7d1249560eb40f7cc69001?s=128&d=https%3A%2F%2Fui-avatars.com%2Fapi%2FCassidy%2520Williams%2F128%2Frgba%2820%2C184%2C166%2C0.2%29%2F14b8a6%2F2%2F0.4%2F0%2F1%2F1%2Fpng" width="72" height="72"></td>
|
|
25
|
+
<td align="center"><img src="https://www.gravatar.com/avatar/8d924f842e67d03be5d0812563e8d672?s=256&d=https%3A%2F%2Fui-avatars.com%2Fapi%2FBig%2520Avatar%2F256%2Frgba%2834%2C197%2C94%2C0.2%29%2F22c55e%2F2%2F0.4%2F0%2F1%2F1%2Fpng" width="72" height="72"></td>
|
|
26
|
+
<td align="center"><img src="https://www.gravatar.com/avatar/de3fe73f6cd2adaac3a8a1fa04d4d80f?s=128&d=https%3A%2F%2Fui-avatars.com%2Fapi%2FChris%2520Coyier%2F128%2Frgba%28249%2C115%2C22%2C0.2%29%2Ff97316%2F2%2F0.4%2F0%2F1%2F1%2Fpng" width="72" height="72"></td>
|
|
27
|
+
<td align="center"><img src="https://ui-avatars.com/api/Brand%20Palette/128/1d4ed833/1d4ed8/2/0.4/0/1/1/svg" width="72" height="72"></td>
|
|
28
|
+
<td align="center"><img src="https://ui-avatars.com/api/Tim%20Berners-Lee/128/84cc1633/84cc16/2/0.4/0/1/1/svg" width="72" height="72"></td>
|
|
29
|
+
<td align="center"><img src="https://www.gravatar.com/avatar/21a241aa10ee457a5f6b72aca98a4860?s=128&d=https%3A%2F%2Fui-avatars.com%2Fapi%2FWade%2520Williams%2F128%2Frgba%2834%2C197%2C94%2C0.2%29%2F22c55e%2F2%2F0.4%2F0%2F1%2F1%2Fpng" width="72" height="72"></td>
|
|
30
|
+
<td align="center"><img src="https://ui-avatars.com/api/Katherine%20Johnson/128/10b98133/10b981/2/0.4/0/1/1/svg" width="72" height="72"></td>
|
|
31
|
+
<td align="center"><img src="https://www.gravatar.com/avatar/f1e3ab214a976a39cfd713bc93deb10f?s=128&d=https%3A%2F%2Fui-avatars.com%2Fapi%2FTJ%2520Holowaychuk%2F128%2Frgba%2814%2C165%2C233%2C0.2%29%2F0ea5e9%2F2%2F0.4%2F0%2F1%2F1%2Fpng" width="72" height="72"></td>
|
|
32
|
+
</tr>
|
|
33
|
+
</table>
|
|
34
|
+
|
|
35
|
+
## Install
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
pip install ui-avatars
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Usage
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
from ui_avatars import avatar_url
|
|
45
|
+
|
|
46
|
+
avatar_url(name="Ada Lovelace", email="ada@example.com")
|
|
47
|
+
# Gravatar photo, falling back to generated initials
|
|
48
|
+
|
|
49
|
+
avatar_url(name="Ada Lovelace")
|
|
50
|
+
# straight ui-avatars.com URL, no Gravatar lookup
|
|
51
|
+
|
|
52
|
+
avatar_url(email="ada@example.com")
|
|
53
|
+
# initials from the first two letters of the email
|
|
54
|
+
|
|
55
|
+
avatar_url(name="Ada Lovelace", alpha=0.75, size=256, rounded=True)
|
|
56
|
+
# alpha, size, and ui-avatars.com rendering options as one-off overrides
|
|
57
|
+
|
|
58
|
+
avatar_url(name="Ada Lovelace", color="#dc2626")
|
|
59
|
+
# pick a specific color, skipping the palette
|
|
60
|
+
|
|
61
|
+
avatar_url(name="Ada Lovelace", alpha=1, color=("#dc2626", "#fff"))
|
|
62
|
+
# supply background and text color separately via a tuple
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Configuration
|
|
66
|
+
|
|
67
|
+
Reconfigure the shared `avatars` instance once, at startup, to change the palette for
|
|
68
|
+
every call:
|
|
69
|
+
|
|
70
|
+
```python
|
|
71
|
+
from ui_avatars import avatars
|
|
72
|
+
|
|
73
|
+
avatars.configure(colors=["#1d4ed8", "#b91c1c"], size=256, rounded=True)
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
Or build your own instance instead:
|
|
77
|
+
|
|
78
|
+
```python
|
|
79
|
+
from ui_avatars import Avatars
|
|
80
|
+
|
|
81
|
+
avatars = Avatars(colors=["#1d4ed8", "#b91c1c"], size=256)
|
|
82
|
+
avatars.build(name="Ada Lovelace", email="ada@example.com")
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
The default palette is [`tailwind_colors`](https://pypi.org/project/tailwind_colors/)'s
|
|
86
|
+
`RAINBOW_500`. You can swap the whole palette like so:
|
|
87
|
+
|
|
88
|
+
```python
|
|
89
|
+
from tailwind_colors import TCH
|
|
90
|
+
|
|
91
|
+
avatars.configure(colors=TCH.RAINBOW_300)
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
For an independent background/text color instead of one tinted from the other, use a
|
|
95
|
+
`(background, text)` tuple as a `colors` entry — `alpha` still applies to whichever
|
|
96
|
+
background results:
|
|
97
|
+
|
|
98
|
+
```python
|
|
99
|
+
avatars.configure(colors=[("#fee2e2", "#ef4444"), ("#ffedd5", "#f97316")])
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
## Development
|
|
103
|
+
|
|
104
|
+
```bash
|
|
105
|
+
poetry install
|
|
106
|
+
poetry run pytest
|
|
107
|
+
poetry run ruff check .
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
## License
|
|
111
|
+
|
|
112
|
+
MIT
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
[tool.poetry]
|
|
2
|
+
name = "ui-avatars"
|
|
3
|
+
version = "1.0.0"
|
|
4
|
+
description = "Deterministic avatar URLs: Gravatar photos with a colorful initials fallback."
|
|
5
|
+
authors = ["Steve Lacey <steve@py.steve.ly>"]
|
|
6
|
+
license = "MIT"
|
|
7
|
+
readme = "README.md"
|
|
8
|
+
homepage = "https://github.com/stevelacey/ui-avatars"
|
|
9
|
+
repository = "https://github.com/stevelacey/ui-avatars"
|
|
10
|
+
keywords = ["avatar", "gravatar", "initials", "placeholder", "ui-avatars"]
|
|
11
|
+
packages = [{ include = "ui_avatars", from = "src" }]
|
|
12
|
+
|
|
13
|
+
[tool.poetry.dependencies]
|
|
14
|
+
python = ">=3.10,<4.0"
|
|
15
|
+
tailwind_colors = "^1.4.1"
|
|
16
|
+
|
|
17
|
+
[tool.poetry.group.lint.dependencies]
|
|
18
|
+
ruff = "*"
|
|
19
|
+
|
|
20
|
+
[tool.poetry.group.test.dependencies]
|
|
21
|
+
pytest = "^9.0"
|
|
22
|
+
pytest-cov = "^5.0"
|
|
23
|
+
pytest-randomly = "^4.1.0"
|
|
24
|
+
|
|
25
|
+
[tool.pytest.ini_options]
|
|
26
|
+
addopts = [
|
|
27
|
+
"--cov=ui_avatars",
|
|
28
|
+
"--cov-report=term-missing",
|
|
29
|
+
"--strict-markers",
|
|
30
|
+
]
|
|
31
|
+
python_files = ["tests.py", "*_tests.py"]
|
|
32
|
+
testpaths = ["tests"]
|
|
33
|
+
|
|
34
|
+
[tool.coverage.run]
|
|
35
|
+
source = ["src"]
|
|
36
|
+
|
|
37
|
+
[tool.coverage.report]
|
|
38
|
+
fail_under = 100
|
|
39
|
+
show_missing = true
|
|
40
|
+
skip_covered = true
|
|
41
|
+
|
|
42
|
+
[tool.ruff.lint]
|
|
43
|
+
select = ["B", "C4", "E", "F", "I", "N", "S", "UP", "W"]
|
|
44
|
+
ignore = ["E501"]
|
|
45
|
+
|
|
46
|
+
[tool.ruff.lint.per-file-ignores]
|
|
47
|
+
"tests/*" = ["S101"]
|
|
48
|
+
|
|
49
|
+
[tool.ruff.lint.isort]
|
|
50
|
+
known-first-party = ["ui_avatars"]
|
|
51
|
+
|
|
52
|
+
[build-system]
|
|
53
|
+
requires = ["poetry-core>=2.0"]
|
|
54
|
+
build-backend = "poetry.core.masonry.api"
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import hashlib
|
|
2
|
+
from urllib.parse import quote
|
|
3
|
+
|
|
4
|
+
from tailwind_colors import TCH
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class Avatars:
|
|
8
|
+
COLORS = list(TCH.RAINBOW_500)
|
|
9
|
+
|
|
10
|
+
def __init__(
|
|
11
|
+
self,
|
|
12
|
+
*,
|
|
13
|
+
alpha: float = 0.2,
|
|
14
|
+
bold: bool = True,
|
|
15
|
+
colors: list[str | tuple[str, str]] = COLORS,
|
|
16
|
+
font_size: float = 0.4,
|
|
17
|
+
length: int = 2,
|
|
18
|
+
rounded: bool = False,
|
|
19
|
+
size: int = 128,
|
|
20
|
+
uppercase: bool = True,
|
|
21
|
+
) -> None:
|
|
22
|
+
self.alpha = alpha
|
|
23
|
+
self.bold = bold
|
|
24
|
+
self.colors = list(colors)
|
|
25
|
+
self.font_size = font_size
|
|
26
|
+
self.length = length
|
|
27
|
+
self.rounded = rounded
|
|
28
|
+
self.size = size
|
|
29
|
+
self.uppercase = uppercase
|
|
30
|
+
|
|
31
|
+
def build(
|
|
32
|
+
self,
|
|
33
|
+
*,
|
|
34
|
+
name: str | None = None,
|
|
35
|
+
email: str | None = None,
|
|
36
|
+
alpha: float | None = None,
|
|
37
|
+
bold: bool | None = None,
|
|
38
|
+
color: str | tuple[str, str] | None = None,
|
|
39
|
+
font_size: float | None = None,
|
|
40
|
+
length: int | None = None,
|
|
41
|
+
rounded: bool | None = None,
|
|
42
|
+
size: int | None = None,
|
|
43
|
+
uppercase: bool | None = None,
|
|
44
|
+
) -> str:
|
|
45
|
+
if not name and not email:
|
|
46
|
+
raise ValueError("requires at least one of name or email")
|
|
47
|
+
|
|
48
|
+
alpha = self.alpha if alpha is None else alpha
|
|
49
|
+
bold = self.bold if bold is None else bold
|
|
50
|
+
font_size = self.font_size if font_size is None else font_size
|
|
51
|
+
length = self.length if length is None else length
|
|
52
|
+
rounded = self.rounded if rounded is None else rounded
|
|
53
|
+
size = self.size if size is None else size
|
|
54
|
+
uppercase = self.uppercase if uppercase is None else uppercase
|
|
55
|
+
|
|
56
|
+
if not name:
|
|
57
|
+
local_part = email.strip().split("@", 1)[0]
|
|
58
|
+
name = "".join(c for c in local_part if c.isalpha())[:2] or local_part[:2]
|
|
59
|
+
|
|
60
|
+
digest = hashlib.md5(
|
|
61
|
+
(email or name).strip().lower().encode(), usedforsecurity=False
|
|
62
|
+
).hexdigest()
|
|
63
|
+
|
|
64
|
+
if color is None:
|
|
65
|
+
color = self.colors[int(digest[:8], 16) % len(self.colors)]
|
|
66
|
+
|
|
67
|
+
background, font_color = color if isinstance(color, tuple) else (color, color)
|
|
68
|
+
background, font_color = background.lstrip("#"), font_color.lstrip("#")
|
|
69
|
+
if len(background) == 3:
|
|
70
|
+
background = "".join(c * 2 for c in background)
|
|
71
|
+
if len(font_color) == 3:
|
|
72
|
+
font_color = "".join(c * 2 for c in font_color)
|
|
73
|
+
alpha_255 = min(255, max(0, round(alpha * 255)))
|
|
74
|
+
background_color = f"{background}{alpha_255:02x}"
|
|
75
|
+
image_format = "png" if email else "svg"
|
|
76
|
+
|
|
77
|
+
if image_format == "png":
|
|
78
|
+
r, g, b = (int(background[i : i + 2], 16) for i in (0, 2, 4))
|
|
79
|
+
background_color = f"rgba({r},{g},{b},{alpha})"
|
|
80
|
+
|
|
81
|
+
url = (
|
|
82
|
+
f"https://ui-avatars.com/api/{quote(name)}/{size}/{background_color}/{font_color}/"
|
|
83
|
+
f"{length}/{font_size}/{int(rounded)}/{int(bold)}/{int(uppercase)}/{image_format}"
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
if email:
|
|
87
|
+
url = f"https://www.gravatar.com/avatar/{digest}?s={size}&d={quote(url, safe='')}"
|
|
88
|
+
|
|
89
|
+
return url
|
|
90
|
+
|
|
91
|
+
def configure(
|
|
92
|
+
self,
|
|
93
|
+
*,
|
|
94
|
+
alpha: float | None = None,
|
|
95
|
+
bold: bool | None = None,
|
|
96
|
+
colors: list[str | tuple[str, str]] | None = None,
|
|
97
|
+
font_size: float | None = None,
|
|
98
|
+
length: int | None = None,
|
|
99
|
+
rounded: bool | None = None,
|
|
100
|
+
size: int | None = None,
|
|
101
|
+
uppercase: bool | None = None,
|
|
102
|
+
) -> "Avatars":
|
|
103
|
+
if alpha is not None:
|
|
104
|
+
self.alpha = alpha
|
|
105
|
+
if bold is not None:
|
|
106
|
+
self.bold = bold
|
|
107
|
+
if colors is not None:
|
|
108
|
+
self.colors = list(colors)
|
|
109
|
+
if font_size is not None:
|
|
110
|
+
self.font_size = font_size
|
|
111
|
+
if length is not None:
|
|
112
|
+
self.length = length
|
|
113
|
+
if rounded is not None:
|
|
114
|
+
self.rounded = rounded
|
|
115
|
+
if size is not None:
|
|
116
|
+
self.size = size
|
|
117
|
+
if uppercase is not None:
|
|
118
|
+
self.uppercase = uppercase
|
|
119
|
+
return self
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
avatars = Avatars()
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
from ui_avatars.generator import avatars
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def avatar_url(
|
|
5
|
+
*,
|
|
6
|
+
name: str | None = None,
|
|
7
|
+
email: str | None = None,
|
|
8
|
+
alpha: float | None = None,
|
|
9
|
+
bold: bool | None = None,
|
|
10
|
+
color: str | tuple[str, str] | None = None,
|
|
11
|
+
font_size: float | None = None,
|
|
12
|
+
length: int | None = None,
|
|
13
|
+
rounded: bool | None = None,
|
|
14
|
+
size: int | None = None,
|
|
15
|
+
uppercase: bool | None = None,
|
|
16
|
+
) -> str:
|
|
17
|
+
return avatars.build(
|
|
18
|
+
name=name,
|
|
19
|
+
email=email,
|
|
20
|
+
alpha=alpha,
|
|
21
|
+
bold=bold,
|
|
22
|
+
color=color,
|
|
23
|
+
font_size=font_size,
|
|
24
|
+
length=length,
|
|
25
|
+
rounded=rounded,
|
|
26
|
+
size=size,
|
|
27
|
+
uppercase=uppercase,
|
|
28
|
+
)
|