@suifei/hash-avatar 0.0.0-stage → 1.0.0

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.
package/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2026 suifei
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
202
+
package/README.md CHANGED
@@ -1,3 +1,155 @@
1
- # Temporary Holding Version
1
+ # hash-avatar
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ [![npm](https://img.shields.io/npm/v/@suifei/hash-avatar?color=2f6feb&label=npm)](https://www.npmjs.com/package/@suifei/hash-avatar)
4
+ [![CI](https://github.com/suifei/hash-avatar/actions/workflows/ci.yml/badge.svg)](https://github.com/suifei/hash-avatar/actions/workflows/ci.yml)
5
+ [![license](https://img.shields.io/badge/license-Apache--2.0-blue)](LICENSE)
6
+
7
+ English | [简体中文](README.zh-CN.md)
8
+
9
+ Deterministic SVG avatars from a name or email, via SHA-256. Zero dependencies, synchronous, runs in browsers and Node. About 0.1 ms and 6 KB per avatar.
10
+
11
+ <p>
12
+ <img src="https://hash-avatar.suifei.workers.dev/v1/alice.svg?gender=female&size=96" width="96" alt="">
13
+ <img src="https://hash-avatar.suifei.workers.dev/v1/bob.svg?gender=male&age=middle&race=black&size=96" width="96" alt="">
14
+ <img src="https://hash-avatar.suifei.workers.dev/v1/carol.svg?gender=female&age=old&race=asian&size=96" width="96" alt="">
15
+ <img src="https://hash-avatar.suifei.workers.dev/v1/dave.svg?gender=male&age=teen&race=brown&size=96" width="96" alt="">
16
+ <img src="https://hash-avatar.suifei.workers.dev/v1/eve.svg?age=baby&size=96" width="96" alt="">
17
+ <img src="https://hash-avatar.suifei.workers.dev/v1/octocat.svg?gender=male&race=white&size=96" width="96" alt="">
18
+ </p>
19
+
20
+ **Live demo:** https://suifei.github.io/hash-avatar/
21
+
22
+ There are three ways to use it:
23
+
24
+ | | Use when | Needs |
25
+ |---|---|---|
26
+ | [Image URL](#1-image-url-no-code) | READMEs, Markdown, any `<img>` | nothing |
27
+ | [CDN script](#2-browser-via-cdn) | rendering in a web page | one `<script>` tag |
28
+ | [npm package](#3-npm) | Node, bundlers, server-side rendering | `npm i @suifei/hash-avatar` |
29
+
30
+ ## 1. Image URL (no code)
31
+
32
+ Works anywhere an image URL works, the same way shields.io badges do:
33
+
34
+ ```html
35
+ <img src="https://hash-avatar.suifei.workers.dev/v1/octocat.svg?gender=male&size=128" width="128" alt="octocat">
36
+ ```
37
+
38
+ ```markdown
39
+ ![avatar](https://hash-avatar.suifei.workers.dev/v1/octocat.svg?size=128)
40
+ ```
41
+
42
+ | URL | Behaviour |
43
+ |---|---|
44
+ | `/v1/<name>.svg` | Pinned to contract v1. **Recommended.** Cached for a year. |
45
+ | `/<name>.svg` | Follows the latest contract version. Cached for a day. |
46
+ | `/avatar.svg?name=<name>` | Query-string form, for names that are awkward in a path. |
47
+
48
+ Query parameters: `gender`, `age`, `race`, `size` (16–1024) and `fp=0` (hide the fingerprint ring). They take the same values as the [options](#options) below. URL-encode the name (`encodeURIComponent`). Names are limited to 256 characters.
49
+
50
+ > Avoid putting real email addresses in public image URLs: they end up in server logs and `Referer` headers. See [Privacy](#privacy).
51
+
52
+ The service is a small Cloudflare Worker ([`worker/index.js`](worker/index.js)). To self-host it, run `npx wrangler deploy`.
53
+
54
+ ## 2. Browser via CDN
55
+
56
+ ```html
57
+ <script src="https://cdn.jsdelivr.net/npm/@suifei/hash-avatar@1/src/hash-avatar.js"></script>
58
+ <div id="me"></div>
59
+ <script>
60
+ document.getElementById('me').innerHTML =
61
+ HashAvatar.svg('alice@example.com', { gender: 'female', age: 'youth', size: 96 });
62
+ </script>
63
+ ```
64
+
65
+ unpkg works too: `https://unpkg.com/@suifei/hash-avatar@1/src/hash-avatar.js`. The script defines `window.HashAvatar`.
66
+
67
+ ## 3. npm
68
+
69
+ ```sh
70
+ npm i @suifei/hash-avatar
71
+ ```
72
+
73
+ ```js
74
+ const HashAvatar = require('@suifei/hash-avatar');
75
+
76
+ const svg = HashAvatar.svg('alice@example.com', { gender: 'female', age: 'youth', race: 'auto', size: 128 });
77
+ // => '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256" ...>'
78
+
79
+ // Use as a data URL:
80
+ const src = 'data:image/svg+xml;charset=utf-8,' + encodeURIComponent(svg);
81
+ ```
82
+
83
+ ## API
84
+
85
+ ### `svg(input, options?) → string`
86
+
87
+ Returns a complete SVG document as a string.
88
+
89
+ #### Options
90
+
91
+ | Option | Values | Default |
92
+ |---|---|---|
93
+ | `gender` | `male` · `female` · `neutral` | `neutral` |
94
+ | `age` | `baby` · `teen` · `youth` · `middle` · `old` | `youth` |
95
+ | `race` | `asian` · `white` · `black` · `brown` · `auto` | `auto` |
96
+ | `size` | width/height in px (the viewBox is always 256) | `256` |
97
+ | `salt` | any string, mixed into the hash | `''` |
98
+ | `fingerprint` | draw the 128-bit fingerprint ring | `true` |
99
+
100
+ Invalid values fall back to the defaults.
101
+
102
+ ### `hash(input, salt?) → { input, bytes, hex }`
103
+
104
+ The normalized input, the 32-byte SHA-256 digest (`Uint8Array`) and its hex string.
105
+
106
+ ### `normalize(input) → string`
107
+
108
+ NFC, trim, then collapse runs of whitespace to one space. Only email-like input (`x@y`) is lowercased, so `Alice@Example.com` and `alice@example.com` give the same avatar, but `Alice` and `alice` do not.
109
+
110
+ ### `sha256(bytes) → Uint8Array`
111
+
112
+ The built-in synchronous SHA-256.
113
+
114
+ ### `GENDERS`, `AGES`, `RACES`, `version`
115
+
116
+ The accepted option values and the contract version (`'v1'`).
117
+
118
+ ## Why avatars are unique
119
+
120
+ Facial features, hairstyles and accessories only have a finite number of combinations, so they cannot make avatars unique on their own. Each avatar therefore has an outer ring of **128 ticks**, one per bit of the first 128 bits of the hash. Ticks are drawn most significant bit first, clockwise from 12 o'clock. A long tick means 1 and a faint dot means 0.
121
+
122
+ If the first 128 bits differ, the rings differ. Two different inputs share a ring only when SHA-256 truncated to 128 bits collides, which is **less likely than a UUIDv4 collision** (122 random bits).
123
+
124
+ Gender, age and race are **not** hashed. They are rendering parameters, so the same identity keeps the same ring and recognisable traits when they change.
125
+
126
+ ## Known limitations
127
+
128
+ - **Small sizes.** Below about 96 px the ring ticks cannot be told apart by eye. Uniqueness then becomes probabilistic and rests on the combination of features.
129
+ - **Privacy.** The ring is the hash in plain sight. For emails, anyone can read those 128 bits and run a dictionary lookup. When you display avatars publicly, render them on your server with a secret `salt`. The public image service cannot do this, because a salt in a URL is not secret, so pass it a username or opaque ID rather than an email.
130
+ - **What race changes.** Race only changes the range of skin, hair and eye colours and the weighting of hair textures. **Facial geometry is never changed.** This is deliberate, to avoid drawing stereotypes.
131
+
132
+ ## Versioning contract
133
+
134
+ Once an avatar has been shown to someone, the mapping from input to picture is a public contract. Under contract v1:
135
+
136
+ 1. The hash input is `'hash-avatar/v1\0' + salt + '\0' + normalize(input)`.
137
+ 2. Bytes 0–15 are drawn as the fingerprint ring. Bytes 16–31 drive the main features through fixed slots. Secondary features reuse bytes 0–15.
138
+ 3. Gender, age and race are not part of the hash.
139
+
140
+ Any change to **slot assignment, the contents or order of any list** (picking uses modulo, so even appending an item breaks existing avatars), **palettes or geometry** changes existing avatars. Such a change requires a **v2** release with a new hash tag and a CHANGELOG entry. Golden snapshot tests (`test/golden.json`) enforce this rule.
141
+
142
+ The npm major version follows the contract version: `1.x` is always contract v1.
143
+
144
+ ## Development
145
+
146
+ ```sh
147
+ npm test # node:test, runs in a few seconds
148
+ npm run build:demo # inlines the library into a single-file dist/index.html
149
+ npm run sheet # renders every hair / hat / beard / clothing / glasses variant (needs rsvg-convert)
150
+ npm run worker:dev # run the image service locally
151
+ ```
152
+
153
+ ## License
154
+
155
+ [Apache-2.0](LICENSE)
@@ -0,0 +1,154 @@
1
+ # hash-avatar
2
+
3
+ [![npm](https://img.shields.io/npm/v/@suifei/hash-avatar?color=2f6feb&label=npm)](https://www.npmjs.com/package/@suifei/hash-avatar)
4
+ [![CI](https://github.com/suifei/hash-avatar/actions/workflows/ci.yml/badge.svg)](https://github.com/suifei/hash-avatar/actions/workflows/ci.yml)
5
+ [![license](https://img.shields.io/badge/license-Apache--2.0-blue)](LICENSE)
6
+
7
+ [English](README.md) | 简体中文
8
+
9
+ 把名称或 email 经 SHA-256 映射成确定性的 SVG 头像。零依赖、同步执行,浏览器和 Node 都能用。每个头像约 0.1ms、约 6KB。
10
+
11
+ <p>
12
+ <img src="https://hash-avatar.suifei.workers.dev/v1/alice.svg?gender=female&size=96" width="96" alt="">
13
+ <img src="https://hash-avatar.suifei.workers.dev/v1/bob.svg?gender=male&age=middle&race=black&size=96" width="96" alt="">
14
+ <img src="https://hash-avatar.suifei.workers.dev/v1/carol.svg?gender=female&age=old&race=asian&size=96" width="96" alt="">
15
+ <img src="https://hash-avatar.suifei.workers.dev/v1/dave.svg?gender=male&age=teen&race=brown&size=96" width="96" alt="">
16
+ <img src="https://hash-avatar.suifei.workers.dev/v1/eve.svg?age=baby&size=96" width="96" alt="">
17
+ <img src="https://hash-avatar.suifei.workers.dev/v1/octocat.svg?gender=male&race=white&size=96" width="96" alt="">
18
+ </p>
19
+
20
+ **在线演示:** https://suifei.github.io/hash-avatar/
21
+
22
+ 有三种用法:
23
+
24
+ | | 适用场景 | 需要 |
25
+ |---|---|---|
26
+ | [图片 URL](#1-图片-url无需代码) | README、Markdown、任意 `<img>` | 什么都不用 |
27
+ | [CDN 脚本](#2-浏览器cdn) | 在网页里渲染 | 一个 `<script>` 标签 |
28
+ | [npm 包](#3-npm) | Node、打包工具、服务端渲染 | `npm i @suifei/hash-avatar` |
29
+
30
+ ## 1. 图片 URL(无需代码)
31
+
32
+ 用法和 shields.io 徽章一样,凡是能填图片 URL 的地方都能用:
33
+
34
+ ```html
35
+ <img src="https://hash-avatar.suifei.workers.dev/v1/octocat.svg?gender=male&size=128" width="128" alt="octocat">
36
+ ```
37
+
38
+ ```markdown
39
+ ![avatar](https://hash-avatar.suifei.workers.dev/v1/octocat.svg?size=128)
40
+ ```
41
+
42
+ | URL | 行为 |
43
+ |---|---|
44
+ | `/v1/<name>.svg` | 锁定契约 v1,**推荐**。缓存一年 |
45
+ | `/<name>.svg` | 跟随最新的契约版本,缓存一天 |
46
+ | `/avatar.svg?name=<name>` | 查询参数形式,适合不方便放进路径的名称 |
47
+
48
+ 查询参数有 `gender`、`age`、`race`、`size`(16–1024)和 `fp=0`(隐藏外圈指纹),取值与下面的[选项](#选项)相同。名称需要 URL 编码(`encodeURIComponent`),长度上限 256 个字符。
49
+
50
+ > 不要把真实 email 放进公开的图片 URL,它会出现在服务器日志和 `Referer` 头里。详见[隐私](#已知限制)。
51
+
52
+ 这个服务是一个很小的 Cloudflare Worker([`worker/index.js`](worker/index.js))。如果要自建,运行 `npx wrangler deploy`。
53
+
54
+ ## 2. 浏览器(CDN)
55
+
56
+ ```html
57
+ <script src="https://cdn.jsdelivr.net/npm/@suifei/hash-avatar@1/src/hash-avatar.js"></script>
58
+ <div id="me"></div>
59
+ <script>
60
+ document.getElementById('me').innerHTML =
61
+ HashAvatar.svg('alice@example.com', { gender: 'female', age: 'youth', size: 96 });
62
+ </script>
63
+ ```
64
+
65
+ 也可以用 unpkg:`https://unpkg.com/@suifei/hash-avatar@1/src/hash-avatar.js`。脚本会定义全局的 `window.HashAvatar`。
66
+
67
+ ## 3. npm
68
+
69
+ ```sh
70
+ npm i @suifei/hash-avatar
71
+ ```
72
+
73
+ ```js
74
+ const HashAvatar = require('@suifei/hash-avatar');
75
+
76
+ const svg = HashAvatar.svg('alice@example.com', { gender: 'female', age: 'youth', race: 'auto', size: 128 });
77
+
78
+ // 作为 data URL 使用:
79
+ const src = 'data:image/svg+xml;charset=utf-8,' + encodeURIComponent(svg);
80
+ ```
81
+
82
+ ## API
83
+
84
+ ### `svg(input, options?) → string`
85
+
86
+ 返回完整的 SVG 文档字符串。
87
+
88
+ #### 选项
89
+
90
+ | 选项 | 取值 | 默认 |
91
+ |---|---|---|
92
+ | `gender` | `male` · `female` · `neutral` | `neutral` |
93
+ | `age` | `baby` · `teen` · `youth` · `middle` · `old` | `youth` |
94
+ | `race` | `asian` · `white` · `black` · `brown` · `auto`(不指定) | `auto` |
95
+ | `size` | 宽高像素(viewBox 固定为 256) | `256` |
96
+ | `salt` | 任意字符串,会混入哈希 | `''` |
97
+ | `fingerprint` | 是否绘制 128 位外圈指纹 | `true` |
98
+
99
+ 传入非法值时使用默认值。
100
+
101
+ ### `hash(input, salt?) → { input, bytes, hex }`
102
+
103
+ 返回规范化后的输入、32 字节 SHA-256 摘要(`Uint8Array`)和它的十六进制字符串。
104
+
105
+ ### `normalize(input) → string`
106
+
107
+ 依次做 NFC、去掉首尾空白、把连续空白合并成一个空格。只有形如 email(`x@y`)的输入才转小写。所以 `Alice@Example.com` 和 `alice@example.com` 得到同一个头像,`Alice` 和 `alice` 则不同。
108
+
109
+ ### `sha256(bytes) → Uint8Array`
110
+
111
+ 内置的同步 SHA-256。
112
+
113
+ ### `GENDERS`、`AGES`、`RACES`、`version`
114
+
115
+ 各选项的合法取值,以及契约版本号(`'v1'`)。
116
+
117
+ ## 唯一性原理
118
+
119
+ 五官、发型、配饰等特征的组合是有限的,单靠它们做不到唯一。所以每个头像的外圈有 **128 个刻度**,逐位画出哈希的前 128 位:高位在前,从 12 点钟方向起顺时针,长刻度表示 1,暗点表示 0。
120
+
121
+ 哈希前 128 位不同,外圈就一定不同。两个不同输入得到相同外圈,需要 SHA-256 截断到 128 位后发生碰撞,这个概率**低于 UUIDv4 的碰撞概率**(122 位随机)。
122
+
123
+ 性别、年龄、人种**不参与哈希**,只是渲染参数。所以同一身份切换这些维度后,外圈和特征依然可以辨认。
124
+
125
+ ## 已知限制
126
+
127
+ - **小尺寸**:头像小于约 96px 时,外圈刻度肉眼分辨不清。此时唯一性退化为概率性的,靠特征组合来区分。
128
+ - **隐私**:外圈相当于哈希明文。对 email 而言,别人可以读出这 128 位后做字典反查。公开展示时,应在服务端使用保密的 `salt` 渲染。公共图片服务做不到这一点,因为写在 URL 里的 salt 不是秘密,所以应该传用户名或不透明 ID,而不是 email。
129
+ - **人种的作用范围**:人种只影响肤色、发色、瞳色的取值范围和发型质地的权重,**不改变五官几何**。这是刻意的设计,避免画成刻板印象。
130
+
131
+ ## 版本契约
132
+
133
+ 头像一旦对外展示,输入到图像的映射就是公开契约。v1 契约规定:
134
+
135
+ 1. 哈希输入为 `'hash-avatar/v1\0' + salt + '\0' + normalize(input)`。
136
+ 2. 字节 0–15 画成外圈指纹,字节 16–31 通过固定槽位驱动主特征,次要特征复用字节 0–15。
137
+ 3. 性别、年龄、人种不参与哈希。
138
+
139
+ 改动以下任何一项都会改变已有头像:**槽位分配**、**任何列表的内容或顺序**(`pick` 用的是取模,往列表里追加一项也算)、**调色板**、**几何参数**。这类改动必须发布 **v2**,换用新的哈希标签,并写进 CHANGELOG。golden 快照测试(`test/golden.json`)负责守住这一条。
140
+
141
+ npm 主版本号与契约版本对应:`1.x` 永远是契约 v1。
142
+
143
+ ## 开发
144
+
145
+ ```sh
146
+ npm test # node:test,几秒内跑完
147
+ npm run build:demo # 把库内联进单文件 dist/index.html
148
+ npm run sheet # 渲染全部发型、帽子、胡须、衣物、眼镜(需要 rsvg-convert)
149
+ npm run worker:dev # 在本地运行图片服务
150
+ ```
151
+
152
+ ## 许可证
153
+
154
+ [Apache-2.0](LICENSE)
package/package.json CHANGED
@@ -1,6 +1,46 @@
1
1
  {
2
2
  "name": "@suifei/hash-avatar",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.0.0",
4
+ "description": "Deterministic SVG avatars from a name or email via SHA-256. Zero dependencies, synchronous, browser and Node.",
5
+ "main": "src/hash-avatar.js",
6
+ "unpkg": "src/hash-avatar.js",
7
+ "jsdelivr": "src/hash-avatar.js",
8
+ "files": [
9
+ "src",
10
+ "README.md",
11
+ "README.zh-CN.md",
12
+ "LICENSE"
13
+ ],
14
+ "scripts": {
15
+ "test": "node --test",
16
+ "build:demo": "node scripts/build-demo.js",
17
+ "sheet": "node tools/sheet.js",
18
+ "worker:dev": "npx wrangler dev",
19
+ "worker:deploy": "npx wrangler deploy"
20
+ },
21
+ "keywords": [
22
+ "avatar",
23
+ "identicon",
24
+ "svg",
25
+ "sha256",
26
+ "deterministic",
27
+ "generator"
28
+ ],
29
+ "author": "suifei",
30
+ "license": "Apache-2.0",
31
+ "repository": {
32
+ "type": "git",
33
+ "url": "git+https://github.com/suifei/hash-avatar.git"
34
+ },
35
+ "homepage": "https://github.com/suifei/hash-avatar#readme",
36
+ "bugs": {
37
+ "url": "https://github.com/suifei/hash-avatar/issues"
38
+ },
39
+ "engines": {
40
+ "node": ">=18"
41
+ },
42
+ "publishConfig": {
43
+ "access": "public",
44
+ "registry": "https://registry.npmjs.org/"
45
+ }
46
+ }
@@ -0,0 +1,679 @@
1
+ /*! hash-avatar v1 — deterministic SVG avatar from SHA-256. No dependencies. */
2
+ (function (root) {
3
+ 'use strict';
4
+
5
+ // ---------- SHA-256 (sync) ----------
6
+ const K = new Int32Array(64), IV = new Int32Array(8);
7
+ (function () {
8
+ let n = 2, c = 0;
9
+ const prime = x => { for (let i = 2; i * i <= x; i++) if (x % i === 0) return false; return true; };
10
+ while (c < 64) {
11
+ if (prime(n)) {
12
+ if (c < 8) IV[c] = (Math.sqrt(n) % 1) * 4294967296 | 0;
13
+ K[c] = (Math.cbrt(n) % 1) * 4294967296 | 0;
14
+ c++;
15
+ }
16
+ n++;
17
+ }
18
+ })();
19
+
20
+ function sha256(msg) {
21
+ const len = msg.length, total = ((len + 9 + 63) >> 6) << 6;
22
+ const buf = new Uint8Array(total); buf.set(msg); buf[len] = 0x80;
23
+ const dv = new DataView(buf.buffer);
24
+ dv.setUint32(total - 4, (len * 8) >>> 0);
25
+ dv.setUint32(total - 8, Math.floor(len / 0x20000000));
26
+ const h = Int32Array.from(IV), w = new Int32Array(64);
27
+ for (let o = 0; o < total; o += 64) {
28
+ for (let i = 0; i < 16; i++) w[i] = dv.getInt32(o + i * 4);
29
+ for (let i = 16; i < 64; i++) {
30
+ const a = w[i - 15], b = w[i - 2];
31
+ const s0 = (a >>> 7 | a << 25) ^ (a >>> 18 | a << 14) ^ (a >>> 3);
32
+ const s1 = (b >>> 17 | b << 15) ^ (b >>> 19 | b << 13) ^ (b >>> 10);
33
+ w[i] = w[i - 16] + s0 + w[i - 7] + s1 | 0;
34
+ }
35
+ let A = h[0], B = h[1], C = h[2], D = h[3], E = h[4], F = h[5], G = h[6], H = h[7];
36
+ for (let i = 0; i < 64; i++) {
37
+ const S1 = (E >>> 6 | E << 26) ^ (E >>> 11 | E << 21) ^ (E >>> 25 | E << 7);
38
+ const t1 = H + S1 + (E & F ^ ~E & G) + K[i] + w[i] | 0;
39
+ const S0 = (A >>> 2 | A << 30) ^ (A >>> 13 | A << 19) ^ (A >>> 22 | A << 10);
40
+ const t2 = S0 + (A & B ^ A & C ^ B & C) | 0;
41
+ H = G; G = F; F = E; E = D + t1 | 0; D = C; C = B; B = A; A = t1 + t2 | 0;
42
+ }
43
+ h[0] += A; h[1] += B; h[2] += C; h[3] += D; h[4] += E; h[5] += F; h[6] += G; h[7] += H;
44
+ }
45
+ const out = new Uint8Array(32), odv = new DataView(out.buffer);
46
+ for (let i = 0; i < 8; i++) odv.setInt32(i * 4, h[i]);
47
+ return out;
48
+ }
49
+
50
+ // ---------- identity -> hash ----------
51
+ const enc = new TextEncoder();
52
+ function normalize(input) {
53
+ let s = String(input == null ? '' : input).normalize('NFC').trim().replace(/\s+/g, ' ');
54
+ if (/^[^\s@]+@[^\s@]+$/.test(s)) s = s.toLowerCase();
55
+ return s;
56
+ }
57
+ function hash(input, salt) {
58
+ const norm = normalize(input);
59
+ const bytes = sha256(enc.encode('hash-avatar/v1\u0000' + (salt || '') + '\u0000' + norm));
60
+ let hex = '';
61
+ for (let i = 0; i < 32; i++) hex += (bytes[i] < 16 ? '0' : '') + bytes[i].toString(16);
62
+ return { input: norm, bytes, hex };
63
+ }
64
+
65
+ // ---------- drawing helpers ----------
66
+ const r1 = n => Math.round(n * 10) / 10;
67
+ function d(s, ...v) { let o = s[0]; for (let i = 0; i < v.length; i++) o += (typeof v[i] === 'number' ? r1(v[i]) : v[i]) + s[i + 1]; return o; }
68
+ const P = (path, fill, extra) => `<path d="${path}" fill="${fill}"${extra || ''}/>`;
69
+ const S = (path, stroke, w, extra) => `<path d="${path}" fill="none" stroke="${stroke}" stroke-width="${w}" stroke-linecap="round" stroke-linejoin="round"${extra || ''}/>`;
70
+ const C = (x, y, r, fill, extra) => `<circle cx="${r1(x)}" cy="${r1(y)}" r="${r1(r)}" fill="${fill}"${extra || ''}/>`;
71
+ const E = (x, y, rx, ry, fill, extra) => `<ellipse cx="${r1(x)}" cy="${r1(y)}" rx="${r1(rx)}" ry="${r1(ry)}" fill="${fill}"${extra || ''}/>`;
72
+ const hsl = (h, s, l) => `hsl(${Math.round(h)},${s}%,${l}%)`;
73
+ function shade(hex, k) {
74
+ const n = parseInt(hex.slice(1), 16);
75
+ const c = [n >> 16, n >> 8 & 255, n & 255].map(v => Math.max(0, Math.min(255, Math.round(v * k))));
76
+ return '#' + c.map(v => v.toString(16).padStart(2, '0')).join('');
77
+ }
78
+ function mix(a, b, t) {
79
+ const x = parseInt(a.slice(1), 16), y = parseInt(b.slice(1), 16);
80
+ const c = [16, 8, 0].map(s => Math.round(((x >> s) & 255) * (1 - t) + ((y >> s) & 255) * t));
81
+ return '#' + c.map(v => v.toString(16).padStart(2, '0')).join('');
82
+ }
83
+
84
+ // ---------- palettes / geometry ----------
85
+ const GENDERS = ['male', 'female', 'neutral'];
86
+ const AGES = ['baby', 'teen', 'youth', 'middle', 'old'];
87
+ const RACES = ['asian', 'white', 'black', 'brown', 'auto'];
88
+ const SKIN_BY = {
89
+ asian: ['#F6DBC0', '#EDCBA6', '#E0B88E'],
90
+ white: ['#FBE4D5', '#F5D2BA', '#E9BE9E'],
91
+ brown: ['#C99066', '#B07A50', '#97643E'],
92
+ black: ['#7C4F33', '#603C26', '#47291A'],
93
+ };
94
+ SKIN_BY.auto = [].concat(SKIN_BY.asian, SKIN_BY.white, SKIN_BY.brown, SKIN_BY.black);
95
+ const HAIR_BY = {
96
+ asian: ['#1E1D1C', '#2B2A28', '#3A2A22', '#26303F', '#4A2F23', '#6E4430'],
97
+ white: ['#2B2A28', '#4A2F23', '#6E4430', '#9C5A33', '#C98A3F', '#DDBB78', '#B5452A', '#E8D5A8'],
98
+ black: ['#1E1D1C', '#2B2A28', '#3A2A22', '#4A2F23'],
99
+ brown: ['#1E1D1C', '#2B2A28', '#3A2A22', '#4A2F23', '#6E4430'],
100
+ };
101
+ HAIR_BY.auto = ['#2B2A28', '#4A2F23', '#6E4430', '#9C5A33', '#C98A3F', '#DDBB78', '#B5452A', '#26303F'];
102
+ const EYE_BY = {
103
+ asian: ['#3B2A20', '#2A1D16', '#5A3B22'],
104
+ white: ['#2F5D8A', '#3E7A55', '#6E6A5E', '#5A3B22', '#7A5230', '#4F7FA8'],
105
+ black: ['#2A1D16', '#3B2A20', '#5A3B22'],
106
+ brown: ['#2A1D16', '#3B2A20', '#5A3B22', '#7A5230'],
107
+ };
108
+ EYE_BY.auto = ['#3B2A20', '#5A3B22', '#2F5D8A', '#3E7A55', '#6E6A5E', '#7A5230'];
109
+ const HAIR_OLD = ['#8E8E8A', '#B4B4AF', '#D8D8D2', '#EEEEE8', '#6F6E6A'];
110
+ const GLASS = ['#2B2A28', '#7A4B2E', '#B8913A'];
111
+ const INK = '#2B2A28';
112
+ const GEO = {
113
+ baby: { cy: 118, rx: 50, ry: 47, bw: 56 },
114
+ teen: { cy: 106, rx: 41, ry: 49, bw: 66 },
115
+ youth: { cy: 104, rx: 42, ry: 51, bw: 74 },
116
+ middle: { cy: 104, rx: 44, ry: 51, bw: 78 },
117
+ old: { cy: 106, rx: 44, ry: 50, bw: 76 },
118
+ };
119
+ const STYLES = {
120
+ male: {
121
+ baby: ['tuft', 'fuzz', 'tuft2', 'babycurls'],
122
+ young: ['crop', 'swept', 'spiky', 'buzz', 'curly', 'afro', 'quiff', 'sidepart', 'manbun', 'mohawk', 'shaggy', 'locs', 'cornrows', 'shoulder'],
123
+ middle: ['crop', 'swept', 'buzz', 'curly', 'receding', 'sidepart', 'quiff', 'shaggy', 'locs', 'shoulder', 'afro'],
124
+ old: ['bald', 'receding', 'crop', 'buzz', 'perm', 'sidepart', 'combover'],
125
+ },
126
+ female: {
127
+ baby: ['tuft', 'fuzz', 'tuft2', 'babycurls', 'sprout'],
128
+ young: ['bob', 'long', 'ponytail', 'bun', 'wavy', 'afro', 'pixie', 'twinbraids', 'pigtails', 'puffs', 'bangslong', 'curtain', 'locs'],
129
+ middle: ['bob', 'long', 'bun', 'wavy', 'ponytail', 'perm', 'pixie', 'curtain', 'locs', 'shoulder', 'afro'],
130
+ old: ['bun', 'perm', 'bob', 'swept', 'pixie', 'shoulder'],
131
+ },
132
+ neutral: {
133
+ baby: ['tuft', 'fuzz', 'tuft2', 'babycurls'],
134
+ young: ['bob', 'crop', 'swept', 'curly', 'buzz', 'ponytail', 'pixie', 'shaggy', 'sidepart', 'locs', 'shoulder', 'curtain', 'afro'],
135
+ middle: ['bob', 'crop', 'swept', 'curly', 'buzz', 'pixie', 'shaggy', 'shoulder', 'locs'],
136
+ old: ['perm', 'crop', 'bob', 'buzz', 'pixie'],
137
+ },
138
+ };
139
+ // Race only shifts weights toward common textures; every style stays reachable for every race.
140
+ const STYLE_BONUS = {
141
+ black: [['afro', 'curly', 'locs', 'cornrows', 'buzz', 'puffs', 'twinbraids', 'perm', 'babycurls'], 2],
142
+ asian: [['crop', 'sidepart', 'bob', 'long', 'bangslong', 'swept', 'buzz', 'ponytail', 'pixie', 'shaggy', 'fuzz'], 1],
143
+ white: [['swept', 'wavy', 'sidepart', 'long', 'curtain', 'shaggy', 'bob', 'tuft'], 1],
144
+ brown: [['wavy', 'curly', 'long', 'sidepart', 'swept', 'ponytail', 'perm', 'tuft2'], 1],
145
+ auto: [[], 0],
146
+ };
147
+ const TALL = { spiky: 1, mohawk: 1, quiff: 1, bun: 1, manbun: 1, puffs: 1, afro: 1, sprout: 1 };
148
+ const COVERING = { beanie: 1, cap: 1, bucket: 1, beret: 1, flatcap: 1, sunhat: 1, knit: 1, bonnet: 1 };
149
+
150
+ // ---------- hair ----------
151
+ // Arc over the top of the head: from left temple to right temple (center = (cx, y0)).
152
+ const capArc = (g, e, eb) => d`M${g.cx - g.rx - e} ${g.y0} A${g.rx + e} ${g.ry + eb} 0 0 1 ${g.cx + g.rx + e} ${g.y0}`;
153
+ // Cap closed by a single curve whose lowest point sits at top + h*ry.
154
+ const capQ = (g, e, eb, h) => capArc(g, e, eb) + d` Q${g.cx} ${2 * (g.top + h * g.ry) - g.y0} ${g.cx - g.rx - e} ${g.y0} Z`;
155
+ const cropPath = g => capArc(g, 2, 6) + d` L${g.cx + g.rx + 1} ${g.y0 + 6} Q${g.cx + g.rx * 0.55} ${g.top + g.ry * 0.2} ${g.cx + g.rx * 0.05} ${g.top + g.ry * 0.3} Q${g.cx - g.rx * 0.5} ${g.top + g.ry * 0.38} ${g.cx - g.rx - 1} ${g.y0 + 6} Z`;
156
+ const centerPart = (g, hc) => P(capArc(g, 2, 5) + d` L${g.cx + g.rx + 1} ${g.y0 + 4} Q${g.cx + g.rx * 0.5} ${g.top + g.ry * 0.4} ${g.cx} ${g.top + g.ry * 0.28} Q${g.cx - g.rx * 0.5} ${g.top + g.ry * 0.4} ${g.cx - g.rx - 1} ${g.y0 + 4} Z`, hc);
157
+ function fringeCurls(g, hc, r, h) {
158
+ let s = '';
159
+ for (let i = 0; i < 5; i++) s += C(g.cx - g.rx * 0.62 + i * g.rx * 0.31, g.top + g.ry * h + (i % 2) * 2, r, hc);
160
+ return s;
161
+ }
162
+ function rimCurls(g, hc, r, e) {
163
+ let s = '';
164
+ for (let i = 0; i < 9; i++) {
165
+ const t = Math.PI - Math.PI * (0.04 + 0.92 * i / 8);
166
+ s += C(g.cx + (g.rx + e) * Math.cos(t), g.y0 - (g.ry + e) * Math.sin(t), r + (i % 2) * 1.5, hc);
167
+ }
168
+ return s;
169
+ }
170
+ function longFront(g, hc) {
171
+ const { cx, cy, rx, ry, top } = g;
172
+ return P(capArc(g, 4, 9) + d` L${cx + rx + 4} ${cy + ry * 0.35} Q${cx + rx - 3} ${top + ry * 0.4} ${cx - rx * 0.1} ${top + ry * 0.24} Q${cx - rx + 3} ${top + ry * 0.35} ${cx - rx - 4} ${cy + ry * 0.35} Z`, hc);
173
+ }
174
+ function curtainFront(g, hc) {
175
+ const { cx, cy, rx, ry, top } = g, L = cx - rx, R = cx + rx;
176
+ return P(capArc(g, 4, 9) + d` L${R + 4} ${cy + ry * 0.35} Q${R - 2} ${top + ry * 0.45} ${cx + 2} ${top + ry * 0.26} L${cx - 2} ${top + ry * 0.26} Q${L + 2} ${top + ry * 0.45} ${L - 4} ${cy + ry * 0.35} Z`, hc);
177
+ }
178
+ function sideStrands(g, hc) {
179
+ const { cx, cy, rx, sh } = g, L = cx - rx, R = cx + rx;
180
+ return P(d`M${L - 6} ${cy} Q${L - 11} ${sh} ${L - 6} ${sh + 18} Q${L + 2} ${sh + 21} ${L + 9} ${sh + 10} Q${L + 2} ${sh - 10} ${L + 4} ${cy} Z`, hc) +
181
+ P(d`M${R + 6} ${cy} Q${R + 11} ${sh} ${R + 6} ${sh + 18} Q${R - 2} ${sh + 21} ${R - 9} ${sh + 10} Q${R - 2} ${sh - 10} ${R - 4} ${cy} Z`, hc);
182
+ }
183
+ function longBack(g, hc, wavy) {
184
+ const { cx, cy, rx, ry, y0 } = g, bot = cy + ry * 1.3, Lx = cx - rx - 7, Rx = cx + rx + 7;
185
+ let s = d`M${Lx} ${y0} A${rx + 7} ${ry + 10} 0 0 1 ${Rx} ${y0}`;
186
+ if (!wavy) return P(s + d` L${Rx + 2} ${bot} Q${cx} ${bot + 10} ${Lx - 2} ${bot} Z`, hc);
187
+ const n = 4, seg = (bot - y0) / n;
188
+ for (let i = 0; i < n; i++) s += d` Q${Rx + (i % 2 ? -5 : 9)} ${y0 + seg * (i + 0.5)} ${Rx} ${y0 + seg * (i + 1)}`;
189
+ s += d` Q${cx} ${bot + 10} ${Lx} ${bot}`;
190
+ for (let i = n - 1; i >= 0; i--) s += d` Q${Lx - (i % 2 ? -5 : 9)} ${y0 + seg * (i + 0.5)} ${Lx} ${y0 + seg * i}`;
191
+ return P(s + 'Z', hc);
192
+ }
193
+ function bobBack(g, hc, depth) {
194
+ const { cx, cy, rx, ry, y0 } = g, e = 5, chin = cy + ry * depth, L = cx - rx - e, R = cx + rx + e;
195
+ return P(d`M${L} ${y0} A${rx + e} ${ry + 8} 0 0 1 ${R} ${y0} L${R} ${chin} Q${R} ${chin + 8} ${R - 10} ${chin + 6} L${L + 10} ${chin + 6} Q${L} ${chin + 8} ${L} ${chin} Z`, hc);
196
+ }
197
+ function tufts(g, hc) {
198
+ const { cx, cy, rx, ry, y0 } = g, L = cx - rx, R = cx + rx;
199
+ return P(d`M${L - 2} ${y0 + 1} Q${L - 6} ${cy + ry * 0.12} ${L + 3} ${cy + ry * 0.1} Q${L + 2} ${cy - ry * 0.05} ${L + 7} ${y0 - 3} Z`, hc) +
200
+ P(d`M${R + 2} ${y0 + 1} Q${R + 6} ${cy + ry * 0.12} ${R - 3} ${cy + ry * 0.1} Q${R - 2} ${cy - ry * 0.05} ${R - 7} ${y0 - 3} Z`, hc);
201
+ }
202
+ const lockTexture = (path, hd) => `<path d="${path}" fill="none" stroke="${hd}" stroke-width="7" stroke-dasharray="1.4 5" stroke-opacity="0.45"/>`;
203
+
204
+ const HAIR_FN = {
205
+ crop: (g, hc) => ({ front: P(cropPath(g), hc) }),
206
+ swept: (g, hc) => ({ front: P(capArc(g, 2, 6) + d` L${g.cx + g.rx + 1} ${g.y0 + 4} Q${g.cx + g.rx * 0.4} ${g.top + g.ry * 0.55} ${g.cx - g.rx * 0.15} ${g.top + g.ry * 0.36} Q${g.cx - g.rx * 0.6} ${g.top + g.ry * 0.24} ${g.cx - g.rx - 1} ${g.y0 + 2} Z`, hc) }),
207
+ sidepart: (g, hc, acc, hd) => {
208
+ const { cx, rx, ry, y0, top } = g;
209
+ return { front: P(capArc(g, 2, 6) + d` L${cx + rx + 1} ${y0 + 5} Q${cx + rx - 4} ${top + ry * 0.26} ${cx - rx * 0.3} ${top + ry * 0.22} Q${cx - rx + 4} ${top + ry * 0.3} ${cx - rx - 1} ${y0 + 5} Z`, hc) + S(d`M${cx - rx * 0.36} ${top + ry * 0.2} Q${cx - rx * 0.32} ${top + 2} ${cx - rx * 0.18} ${top - 4}`, hd, 1.5) };
210
+ },
211
+ quiff: (g, hc) => {
212
+ const { cx, rx, ry, y0, top } = g, L = cx - rx, R = cx + rx;
213
+ return { front: P(capArc(g, 2, 6) + d` L${R + 1} ${y0 + 5} Q${R - 6} ${top + ry * 0.25} ${cx} ${top + ry * 0.28} Q${L + 6} ${top + ry * 0.25} ${L - 1} ${y0 + 5} Z`, hc) +
214
+ P(d`M${L + 2} ${top + ry * 0.3} Q${L - 2} ${top - 12} ${cx} ${top - 18} Q${R + 4} ${top - 16} ${R + 2} ${top + ry * 0.25} Q${cx + rx * 0.3} ${top + ry * 0.05} ${L + 2} ${top + ry * 0.3} Z`, hc) +
215
+ S(d`M${cx - rx * 0.45} ${top - 5} Q${cx} ${top - 14} ${cx + rx * 0.5} ${top - 8}`, '#FFFFFF', 1.5, ' stroke-opacity="0.3"') };
216
+ },
217
+ spiky: (g, hc) => {
218
+ const { cx, rx, ry, y0, top } = g, N = 12;
219
+ let s = '';
220
+ for (let i = 0; i <= N; i++) {
221
+ const t = Math.PI - i * Math.PI / N, e = (i % 2 && i > 1 && i < N - 1) ? 13 : 3;
222
+ s += (i ? ' L' : 'M') + d`${cx + (rx + e) * Math.cos(t)} ${y0 - (ry + e) * Math.sin(t)}`;
223
+ }
224
+ s += d` L${cx + rx + 1} ${y0 + 6} Q${cx + rx * 0.55} ${top + ry * 0.2} ${cx + rx * 0.05} ${top + ry * 0.3} Q${cx - rx * 0.5} ${top + ry * 0.38} ${cx - rx - 1} ${y0 + 6} Z`;
225
+ return { front: P(s, hc) };
226
+ },
227
+ mohawk: (g, hc) => {
228
+ const { cx, ry, top } = g;
229
+ return { front: P(capQ(g, 1, 2, 0.3), hc, ' opacity="0.45"') + P(d`M${cx - 9} ${top + ry * 0.28} L${cx - 10} ${top - 2} L${cx - 7} ${top - 14} L${cx - 3} ${top - 5} L${cx + 1} ${top - 19} L${cx + 4} ${top - 6} L${cx + 8} ${top - 14} L${cx + 10} ${top - 1} L${cx + 9} ${top + ry * 0.28} Z`, hc) };
230
+ },
231
+ buzz: (g, hc) => ({ front: P(capQ(g, 1, 2, 0.26), hc, ' opacity="0.9"') }),
232
+ curly: (g, hc) => ({ front: P(capQ(g, 2, 5, 0.3), hc) + rimCurls(g, hc, 8, 1) + fringeCurls(g, hc, 7.5, 0.28) }),
233
+ afro: (g, hc) => {
234
+ const { cx, cy, rx, ry } = g, ecy = cy - ry * 0.22, ea = rx * 1.48, eb = ry * 1.06;
235
+ let back = E(cx, ecy, ea, eb, hc);
236
+ for (let i = 0; i < 16; i++) {
237
+ const t = Math.PI * 2 * i / 16;
238
+ if (Math.sin(t) > 0.35) continue;
239
+ back += C(cx + ea * Math.cos(t), ecy + eb * Math.sin(t), 9, hc);
240
+ }
241
+ return { back, front: P(capQ(g, 2, 5, 0.3), hc) + fringeCurls(g, hc, 7.5, 0.28) };
242
+ },
243
+ perm: (g, hc) => {
244
+ const { cx, cy, rx, ry } = g, ecy = cy - ry * 0.35, ea = rx * 1.18, eb = ry * 0.8;
245
+ let back = E(cx, ecy, ea, eb, hc);
246
+ for (let i = 0; i < 16; i++) {
247
+ const t = Math.PI * 2 * i / 16;
248
+ if (Math.sin(t) > 0.2) continue;
249
+ back += C(cx + ea * Math.cos(t), ecy + eb * Math.sin(t), 7.5, hc);
250
+ }
251
+ return { back, front: P(capQ(g, 2, 5, 0.3), hc) + fringeCurls(g, hc, 6.5, 0.26) };
252
+ },
253
+ locs: (g, hc, acc, hd) => {
254
+ const { cx, cy, rx, ry, y0, top, sh } = g, L = cx - rx, R = cx + rx;
255
+ let strands = '';
256
+ for (let i = 0; i < 10; i++) {
257
+ const x = L - 5 + i * (2 * rx + 10) / 9;
258
+ strands += d`M${x} ${y0}L${x + (i < 5 ? -2 : 2)} ${cy + ry * (1.15 + (i * 7 % 3) * 0.08)}`;
259
+ }
260
+ const back = P(capArc(g, 6, 9) + d` L${R + 6} ${cy} L${L - 6} ${cy} Z`, hc) + S(strands, hc, 7.5) + lockTexture(strands, hd);
261
+ const mids = d`M${L - 3} ${cy}L${L - 6} ${sh + 16}M${L + 4} ${cy + ry * 0.3}L${L + 2} ${sh + 12}M${R + 3} ${cy}L${R + 6} ${sh + 16}M${R - 4} ${cy + ry * 0.3}L${R - 2} ${sh + 12}`;
262
+ const fr = d`M${L + 3} ${top + ry * 0.4}L${L - 1} ${cy + ry * 0.5}M${R - 3} ${top + ry * 0.4}L${R + 1} ${cy + ry * 0.5}`;
263
+ return {
264
+ back,
265
+ mid: S(mids, hc, 7) + lockTexture(mids, hd),
266
+ front: P(capArc(g, 3, 7) + d` L${R + 2} ${y0 + 8} Q${cx + rx * 0.5} ${top + ry * 0.28} ${cx} ${top + ry * 0.3} Q${cx - rx * 0.5} ${top + ry * 0.28} ${L - 2} ${y0 + 8} Z`, hc) + S(fr, hc, 7) + lockTexture(fr, hd),
267
+ };
268
+ },
269
+ cornrows: (g, hc, acc, hd) => {
270
+ const { cx, rx, ry, top } = g;
271
+ let rows = '';
272
+ for (let i = -3; i <= 3; i++) rows += d`M${cx + i * rx * 0.2} ${top + ry * 0.22}Q${cx + i * rx * 0.24} ${top + ry * 0.05} ${cx + i * rx * 0.27} ${top - 2}`;
273
+ return { front: P(capQ(g, 1, 3, 0.24), hc) + S(rows, hd, 1.6, ' stroke-dasharray="2.2 2"') };
274
+ },
275
+ shaggy: (g, hc) => {
276
+ const { cx, cy, rx, ry, y0, top } = g, L = cx - rx, R = cx + rx;
277
+ const back = P(d`M${L - 6} ${y0} A${rx + 6} ${ry + 9} 0 0 1 ${R + 6} ${y0} L${R + 7} ${cy + ry * 0.2} L${R + 1} ${cy + ry * 0.12} L${R + 4} ${cy + ry * 0.34} L${L - 4} ${cy + ry * 0.34} L${L - 1} ${cy + ry * 0.12} L${L - 7} ${cy + ry * 0.2} Z`, hc);
278
+ let f = capArc(g, 4, 8) + d` L${R + 4} ${cy + ry * 0.05}`;
279
+ for (let i = 0; i <= 6; i++) f += d` L${R - 2 - i * (2 * rx - 4) / 6} ${top + ry * (i % 2 ? 0.48 : 0.32)}`;
280
+ return { back, front: P(f + d` L${L - 4} ${cy + ry * 0.05} Z`, hc) };
281
+ },
282
+ shoulder: (g, hc) => ({ back: bobBack(g, hc, 0.85), front: curtainFront(g, hc) }),
283
+ bob: (g, hc) => {
284
+ const { cx, cy, rx, ry, top } = g;
285
+ const front = P(capArc(g, 4, 8) + d` L${cx + rx + 4} ${cy + ry * 0.3} Q${cx + rx + 2} ${top + ry * 0.5} ${cx + rx * 0.55} ${top + ry * 0.38} Q${cx} ${top + ry * 0.46} ${cx - rx * 0.55} ${top + ry * 0.38} Q${cx - rx - 2} ${top + ry * 0.5} ${cx - rx - 4} ${cy + ry * 0.3} Z`, hc);
286
+ return { back: bobBack(g, hc, 0.55), front };
287
+ },
288
+ pixie: (g, hc) => {
289
+ const { cx, rx, ry, y0, top } = g;
290
+ return { front: P(capArc(g, 2, 5) + d` L${cx + rx + 1} ${y0 + 2} Q${cx + rx - 2} ${top + ry * 0.3} ${cx + rx * 0.2} ${top + ry * 0.26} L${cx - rx * 0.35} ${top + ry * 0.52} Q${cx - rx * 0.62} ${top + ry * 0.4} ${cx - rx - 1} ${y0 + 8} Z`, hc) };
291
+ },
292
+ long: (g, hc) => ({ back: longBack(g, hc, false), mid: sideStrands(g, hc), front: longFront(g, hc) }),
293
+ wavy: (g, hc) => ({ back: longBack(g, hc, true), front: longFront(g, hc) }),
294
+ curtain: (g, hc) => ({ back: longBack(g, hc, false), mid: sideStrands(g, hc), front: curtainFront(g, hc) }),
295
+ bangslong: (g, hc) => {
296
+ const { cx, cy, rx, ry, top } = g, L = cx - rx, R = cx + rx;
297
+ return { back: longBack(g, hc, false), mid: sideStrands(g, hc), front: P(capArc(g, 4, 9) + d` L${R + 4} ${cy + ry * 0.4} Q${R} ${cy} ${R - 1} ${top + ry * 0.5} L${L + 1} ${top + ry * 0.5} Q${L} ${cy} ${L - 4} ${cy + ry * 0.4} Z`, hc) };
298
+ },
299
+ ponytail: (g, hc, acc) => {
300
+ const { cx, cy, rx, ry, top } = g;
301
+ const back = P(d`M${cx + rx * 0.5} ${top + ry * 0.15} Q${cx + rx + 26} ${cy - ry * 0.35} ${cx + rx + 16} ${cy + ry * 0.55} Q${cx + rx + 8} ${cy + ry * 0.1} ${cx + rx - 4} ${cy - ry * 0.3} Z`, hc) + C(cx + rx + 3, top + ry * 0.32, 4, acc);
302
+ return { back, front: HAIR_FN.swept(g, hc).front };
303
+ },
304
+ bun: (g, hc) => ({ back: C(g.cx, g.top - g.ry * 0.08, g.rx * 0.42, hc), front: centerPart(g, hc) }),
305
+ manbun: (g, hc, acc, hd) => ({ back: C(g.cx, g.top - 8, 10, hc), front: P(capQ(g, 2, 4, 0.2), hc) + S(d`M${g.cx - g.rx * 0.4} ${g.top + g.ry * 0.18} Q${g.cx} ${g.top - 4} ${g.cx + g.rx * 0.4} ${g.top + g.ry * 0.18}`, hd, 1.2, ' stroke-opacity="0.6"') }),
306
+ twinbraids: (g, hc, acc, hd) => {
307
+ const { cx, cy, rx, ry } = g;
308
+ let mid = '';
309
+ for (const sgn of [-1, 1]) {
310
+ let y = cy + ry * 0.35, x = cx + sgn * (rx + 1);
311
+ for (let k = 0; k < 7; k++) { mid += E(x, y, 5.5, 6.5, hc, ` stroke="${hd}" stroke-width="1"`); y += 9; x += sgn * 0.8; }
312
+ mid += C(x, y - 3, 3.5, acc);
313
+ }
314
+ return { back: P(capArc(g, 4, 7) + d` L${cx + rx + 4} ${cy} L${cx - rx - 4} ${cy} Z`, hc), mid, front: centerPart(g, hc) };
315
+ },
316
+ pigtails: (g, hc, acc) => {
317
+ const { cx, cy, rx, ry } = g, yy = cy - ry * 0.2;
318
+ let back = '';
319
+ for (const sgn of [-1, 1]) {
320
+ const x = cx + sgn * (rx + 9);
321
+ back += E(x, yy, 9, 17, hc, ` transform="rotate(${sgn * -20} ${r1(x)} ${r1(yy)})"`);
322
+ back += C(cx + sgn * (rx + 2), cy - ry * 0.34, 3.5, acc);
323
+ }
324
+ return { back, front: centerPart(g, hc) };
325
+ },
326
+ puffs: (g, hc, acc, hd) => {
327
+ const { cx, rx, ry, top } = g;
328
+ let back = '';
329
+ for (const sgn of [-1, 1]) {
330
+ const x = cx + sgn * rx * 0.78, y = top + 4;
331
+ back += C(x, y, 16, hc);
332
+ for (let i = 0; i < 7; i++) { const t = Math.PI * (1 + i / 6); back += C(x + 16 * Math.cos(t), y + 16 * Math.sin(t), 6, hc); }
333
+ }
334
+ return { back, front: P(capQ(g, 2, 4, 0.24), hc) + S(d`M${cx} ${top + ry * 0.2} L${cx} ${top - 3}`, hd, 1.4) };
335
+ },
336
+ receding: (g, hc) => {
337
+ const { cx, rx, ry, y0, top } = g;
338
+ return { front: P(capArc(g, 1, 3) + d` Q${cx + rx - 6} ${top + ry * 0.3} ${cx + rx * 0.3} ${top + ry * 0.12} Q${cx} ${top + ry * 0.3} ${cx - rx * 0.3} ${top + ry * 0.12} Q${cx - rx + 6} ${top + ry * 0.3} ${cx - rx - 1} ${y0} Z`, hc, ' opacity="0.9"') + tufts(g, hc) };
339
+ },
340
+ combover: (g, hc) => {
341
+ const { cx, rx, ry, top } = g;
342
+ return { front: tufts(g, hc) + P(capQ(g, 1, 2, 0.22), hc, ' opacity="0.5"') + S(d`M${cx - rx + 4} ${top + ry * 0.3} Q${cx} ${top - 6} ${cx + rx - 2} ${top + ry * 0.12}M${cx - rx + 6} ${top + ry * 0.4} Q${cx} ${top} ${cx + rx - 4} ${top + ry * 0.22}`, hc, 1.6) };
343
+ },
344
+ bald: (g, hc) => {
345
+ const { cx, rx, ry, top } = g, x = cx - rx * 0.35, y = top + ry * 0.22;
346
+ return { front: tufts(g, hc) + E(x, y, 8, 4, '#FFFFFF', ` opacity="0.3" transform="rotate(-25 ${r1(x)} ${r1(y)})"`) };
347
+ },
348
+ fuzz: (g, hc) => ({ front: P(capQ(g, 0, 1, 0.22), hc, ' opacity="0.45"') }),
349
+ tuft: (g, hc) => ({ front: P(capQ(g, 0, 1, 0.18), hc, ' opacity="0.3"') + S(d`M${g.cx} ${g.top + 4} Q${g.cx - 4} ${g.top - 10} ${g.cx + 5} ${g.top - 12} Q${g.cx + 12} ${g.top - 9} ${g.cx + 7} ${g.top - 3}`, hc, 3) }),
350
+ tuft2: (g, hc) => ({ front: P(capQ(g, 0, 1, 0.18), hc, ' opacity="0.3"') + S(d`M${g.cx - 6} ${g.top + 4} Q${g.cx - 12} ${g.top - 8} ${g.cx - 4} ${g.top - 10} Q${g.cx + 1} ${g.top - 7} ${g.cx - 3} ${g.top - 3}`, hc, 2.6) + S(d`M${g.cx + 4} ${g.top + 3} Q${g.cx + 2} ${g.top - 11} ${g.cx + 11} ${g.top - 10} Q${g.cx + 16} ${g.top - 5} ${g.cx + 10} ${g.top - 2}`, hc, 2.6) }),
351
+ babycurls: (g, hc) => {
352
+ let s = P(capQ(g, 0, 1, 0.2), hc, ' opacity="0.4"');
353
+ for (let i = 0; i < 5; i++) s += C(g.cx - 14 + i * 7, g.top + 1 - (i % 2) * 3, 4.2, hc);
354
+ return { front: s };
355
+ },
356
+ sprout: (g, hc, acc) => ({ front: P(capQ(g, 0, 1, 0.2), hc, ' opacity="0.4"') + P(d`M${g.cx - 4} ${g.top + 2} Q${g.cx - 9} ${g.top - 14} ${g.cx} ${g.top - 17} Q${g.cx + 9} ${g.top - 14} ${g.cx + 4} ${g.top + 2} Z`, hc) + C(g.cx, g.top + 1, 2.8, acc) }),
357
+ };
358
+
359
+ // ---------- headwear ----------
360
+ function headwear(kind, g, hue) {
361
+ const { cx, cy, rx, ry, top, y0 } = g, L = cx - rx, R = cx + rx;
362
+ const h1 = hsl(hue, 55, 52), h2 = hsl(hue, 50, 38), h3 = hsl(hue, 60, 82);
363
+ switch (kind) {
364
+ case 'beanie': case 'knit': {
365
+ let ribs = '';
366
+ for (let i = 1; i < 12; i++) ribs += d`M${L - 5 + i * (2 * rx + 10) / 12} ${top + ry * 0.31}l0 ${ry * 0.15}`;
367
+ return P(d`M${L - 4} ${top + ry * 0.42} C${L - 4} ${top - 24} ${R + 4} ${top - 24} ${R + 4} ${top + ry * 0.42} Z`, h1) +
368
+ `<rect x="${r1(L - 5)}" y="${r1(top + ry * 0.29)}" width="${r1(2 * rx + 10)}" height="${r1(ry * 0.19)}" rx="4" fill="${h2}"/>` +
369
+ S(ribs, h1, 1.2, ' stroke-opacity="0.5"') + C(cx, top - 14, kind === 'knit' ? 8 : 6.5, h3);
370
+ }
371
+ case 'cap':
372
+ return P(d`M${L - 3} ${top + ry * 0.38} C${L - 3} ${top - 22} ${R + 3} ${top - 22} ${R + 3} ${top + ry * 0.38} Z`, h1) +
373
+ S(d`M${cx} ${top - 11} L${cx} ${top + ry * 0.3}`, h2, 1.2) + C(cx, top - 11, 2.6, h2) +
374
+ P(d`M${L - 2} ${top + ry * 0.36} Q${cx} ${top + ry * 0.26} ${R + 2} ${top + ry * 0.36} Q${cx} ${top + ry * 0.66} ${L - 2} ${top + ry * 0.36} Z`, h2);
375
+ case 'bucket':
376
+ return P(d`M${L + 1} ${top + ry * 0.32} C${L + 1} ${top - 20} ${R - 1} ${top - 20} ${R - 1} ${top + ry * 0.32} Z`, h1) +
377
+ P(d`M${L - 14} ${top + ry * 0.52} L${L - 1} ${top + ry * 0.28} L${R + 1} ${top + ry * 0.28} L${R + 14} ${top + ry * 0.52} Q${cx} ${top + ry * 0.64} ${L - 14} ${top + ry * 0.52} Z`, h2);
378
+ case 'beret':
379
+ return E(cx + 5, top - 1, rx + 9, 12, h2, ` transform="rotate(-8 ${r1(cx + 5)} ${r1(top - 1)})"`) + C(cx + 8, top - 13, 3, h2);
380
+ case 'flatcap': {
381
+ const t1 = hsl(hue, 18, 42), t2 = hsl(hue, 18, 30);
382
+ return P(d`M${L - 3} ${top + ry * 0.42} Q${L - 3} ${top - 11} ${cx} ${top - 13} Q${R + 7} ${top - 11} ${R + 6} ${top + ry * 0.38} Z`, t1) +
383
+ P(d`M${L - 1} ${top + ry * 0.38} Q${cx} ${top + ry * 0.28} ${R + 3} ${top + ry * 0.36} Q${cx + 4} ${top + ry * 0.56} ${L - 1} ${top + ry * 0.38} Z`, t2);
384
+ }
385
+ case 'sunhat': {
386
+ const by = top + ry * 0.32;
387
+ return E(cx, by, rx + 30, 10, h3) +
388
+ P(d`M${L + 2} ${by + 2} C${L + 2} ${top - 22} ${R - 2} ${top - 22} ${R - 2} ${by + 2} Z`, h3) +
389
+ `<rect x="${r1(L + 2)}" y="${r1(by - 8)}" width="${r1(2 * rx - 4)}" height="6" fill="${h1}"/>` +
390
+ P(d`M${cx - rx - 30} ${by} A${rx + 30} 10 0 0 0 ${cx + rx + 30} ${by} Z`, h3);
391
+ }
392
+ case 'bonnet': {
393
+ const b1 = hsl(hue, 60, 84), b2 = hsl(hue, 45, 66);
394
+ return P(d`M${L - 7} ${cy + ry * 0.3} C${L - 9} ${top - 36} ${R + 9} ${top - 36} ${R + 7} ${cy + ry * 0.3} L${R - 1} ${cy + ry * 0.25} C${R} ${top + ry * 0.1} ${L} ${top + ry * 0.1} ${L + 1} ${cy + ry * 0.25} Z`, b1) +
395
+ S(d`M${L + 1} ${cy + ry * 0.25} C${L} ${top + ry * 0.1} ${R} ${top + ry * 0.1} ${R - 1} ${cy + ry * 0.25}`, b2, 2.5) +
396
+ S(d`M${L - 4} ${cy + ry * 0.35} L${cx - 5} ${cy + ry * 1.05}M${R + 4} ${cy + ry * 0.35} L${cx + 5} ${cy + ry * 1.05}`, b2, 2.5) + C(cx, cy + ry * 1.06, 3.5, b2);
397
+ }
398
+ case 'headband':
399
+ return S(d`M${L} ${y0 + 2} A${rx} ${ry + 3} 0 0 1 ${R} ${y0 + 2}`, h1, 5);
400
+ case 'flower': {
401
+ const fx = cx - rx * 0.62, fy = top + ry * 0.28;
402
+ let s = '';
403
+ for (let i = 0; i < 5; i++) { const t = i / 5 * 2 * Math.PI - Math.PI / 2; s += C(fx + 5 * Math.cos(t), fy + 5 * Math.sin(t), 3.6, h3); }
404
+ return s + C(fx, fy, 2.6, '#F2C14E');
405
+ }
406
+ }
407
+ return '';
408
+ }
409
+
410
+ // ---------- render ----------
411
+ function svg(input, opts) {
412
+ opts = opts || {};
413
+ const gender = GENDERS.indexOf(opts.gender) >= 0 ? opts.gender : 'neutral';
414
+ const age = AGES.indexOf(opts.age) >= 0 ? opts.age : 'youth';
415
+ const race = RACES.indexOf(opts.race) >= 0 ? opts.race : 'auto';
416
+ const size = opts.size || 256;
417
+ const fp = opts.fingerprint !== false;
418
+ const force = opts._force || {};
419
+ const H = hash(input, opts.salt), b = H.bytes;
420
+ // bytes 0..15 are drawn as the fingerprint ring; 16..31 drive the main features (fixed slots).
421
+ // Secondary features reuse bytes 0..15: uniqueness comes from the ring, so reuse costs nothing.
422
+ const F = i => b[16 + i], X = i => b[i];
423
+ const pick = (arr, v) => arr[v % arr.length];
424
+
425
+ const baby = age === 'baby', old = age === 'old', middle = age === 'middle', teen = age === 'teen';
426
+ const male = gender === 'male', female = gender === 'female';
427
+ const band = baby ? 'baby' : old ? 'old' : middle ? 'middle' : 'young';
428
+
429
+ // features
430
+ const bgHue = F(0) / 256 * 360;
431
+ const skin = pick(SKIN_BY[race], F(1));
432
+ let hair = old ? pick(HAIR_OLD, F(2)) : pick(HAIR_BY[race], F(2));
433
+ const misc = F(14), deco = F(15);
434
+ if (middle && (misc >> 6) === 3) hair = mix(hair, '#B4B4AF', 0.45);
435
+ const base = STYLES[gender][band], bonus = STYLE_BONUS[race];
436
+ let styles = base.slice();
437
+ for (let k = 0; k < bonus[1]; k++) for (const s of bonus[0]) if (base.indexOf(s) >= 0) styles.push(s);
438
+ const style = force.style || pick(styles, F(3) | X(4) << 8);
439
+ const eyeStyle = pick(baby ? ['dot', 'oval', 'oval'] : ['dot', 'oval', 'happy', 'sleepy', 'dot', 'oval'], F(4));
440
+ const eyeColor = pick(EYE_BY[race], F(5));
441
+ const browStyle = pick(['straight', 'arched', 'angled', 'thick', 'raised'], F(6));
442
+ const noseStyle = baby ? 'button' : pick(['hook', 'button', 'nostrils'], F(7));
443
+ const mouthStyle = pick(baby ? ['smile', 'o', 'pacifier', 'grin'] : ['smile', 'grin', 'o', 'smirk', 'flat', 'smile'], F(8));
444
+ const glassList = baby ? ['none'] : old ? ['none', 'round', 'rect', 'half', 'round', 'rect'] : middle ? ['none', 'none', 'none', 'round', 'rect', 'half', 'rect', 'sun'] : ['none', 'none', 'none', 'none', 'round', 'rect', 'half', 'sun'];
445
+ const glasses = force.glasses || pick(glassList, F(9)), glassColor = pick(GLASS, F(9) >> 4);
446
+ const beardList = (!male || baby || teen) ? ['none']
447
+ : old ? ['none', 'none', 'mustache', 'beard', 'longbeard', 'goatee', 'handlebar', 'longbeard']
448
+ : middle ? ['none', 'none', 'stubble', 'mustache', 'beard', 'goatee', 'chinstrap', 'beard', 'handlebar', 'sideburns']
449
+ : ['none', 'none', 'none', 'stubble', 'mustache', 'beard', 'goatee', 'chinstrap', 'soulpatch', 'sideburns', 'handlebar'];
450
+ const beard = force.beard || pick(beardList, F(10));
451
+ let acc = pick(baby ? ['none'] : male ? ['none', 'none', 'headphones', 'none'] : ['none', 'none', 'headphones', 'earrings'], F(11));
452
+ const clothList = baby ? ['onesie', 'stripes', 'overalls'] : ['tee', 'hoodie', 'collar', 'cardigan', 'suit', 'turtleneck', 'stripes', 'overalls'];
453
+ const cloth = force.cloth || pick(clothList, F(12));
454
+ const clothHue = F(13) / 256 * 360;
455
+ const freckles = !baby && (misc >> 4 & 1), blush = baby || (misc >> 5 & 1);
456
+ const none = n => Array(n).fill('none');
457
+ const hatList = baby ? ['none', 'none', 'knit', female ? 'bonnet' : 'knit']
458
+ : old ? none(7).concat(['beanie', 'cap', male ? 'flatcap' : female ? 'sunhat' : 'beret'])
459
+ : middle ? none(9).concat(['beanie', 'cap', 'beret', male ? 'flatcap' : 'headband'])
460
+ : none(12).concat(['beanie', 'cap', 'bucket', 'beret', 'headband', male ? 'beanie' : 'flower']);
461
+ let hat = force.hat || pick(hatList, X(0));
462
+ if (COVERING[hat] && TALL[style]) hat = 'none';
463
+ if (COVERING[hat] && acc === 'headphones') acc = 'none';
464
+ const hatHue = X(1) / 256 * 360;
465
+
466
+ // geometry
467
+ const G0 = GEO[age], cx = 128, cy = G0.cy;
468
+ const rx = G0.rx + (misc & 3) - 1, ry = G0.ry + (misc >> 2 & 3) - 1;
469
+ const top = cy - ry, L = cx - rx, R = cx + rx, sh = cy + ry + (baby ? 16 : 20);
470
+ const g = { cx, cy, rx, ry, top, y0: cy - ry * 0.12, sh };
471
+
472
+ // colors
473
+ const skinD = shade(skin, 0.88), skinDD = shade(skin, 0.68), hairD = shade(hair, old ? 0.8 : 0.72);
474
+ const lip = mix(shade(skin, 0.55), '#9A3A28', 0.55), inner = mix(shade(skin, 0.4), '#5A1A12', 0.6);
475
+ const clothC = hsl(clothHue, 55, baby ? 72 : 52), clothD = hsl(clothHue, 50, baby ? 58 : 38), clothL = hsl(clothHue, 60, 82);
476
+ const bg = hsl(bgHue, 55, 74), bgL = hsl(bgHue, 60, 85), bgD = hsl(bgHue, 45, 62);
477
+ const id = 'ha' + H.hex.slice(0, 12) + gender[0] + age[0] + RACES.indexOf(race) + (fp ? 'f' : 'n');
478
+ const R0 = fp ? 106 : 128;
479
+
480
+ const o = [];
481
+ o.push(`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256" width="${size}" height="${size}" role="img"><title>avatar ${H.hex.slice(0, 8)}</title>`);
482
+ o.push(`<defs><clipPath id="${id}"><circle cx="128" cy="128" r="${R0}"/></clipPath>`);
483
+ const bw = G0.bw;
484
+ const bodyPath = d`M${cx - bw} 256 C${cx - bw} ${sh + 6} ${cx - bw * 0.55} ${sh - 6} ${cx} ${sh - 7} C${cx + bw * 0.55} ${sh - 6} ${cx + bw} ${sh + 6} ${cx + bw} 256Z`;
485
+ o.push(`<clipPath id="${id}b"><path d="${bodyPath}"/></clipPath></defs>`);
486
+
487
+ // fingerprint ring: 128 bits of the hash, one slot each, clockwise from 12 o'clock
488
+ if (fp) {
489
+ o.push(`<circle cx="128" cy="128" r="128" fill="${hsl(bgHue, 35, 22)}"/>`);
490
+ let on = '', off = '';
491
+ for (let i = 0; i < 128; i++) {
492
+ const a = (i + 0.5) / 128 * 2 * Math.PI - Math.PI / 2, c = Math.cos(a), s = Math.sin(a);
493
+ if ((b[i >> 3] >> (7 - (i & 7))) & 1) on += d`M${128 + 110 * c} ${128 + 110 * s}L${128 + 123 * c} ${128 + 123 * s}`;
494
+ else off += d`M${128 + 116.5 * c} ${128 + 116.5 * s}h0`;
495
+ }
496
+ const tc = hsl(bgHue, 70, 82);
497
+ o.push(`<path d="${on}" stroke="${tc}" stroke-width="3"/>`);
498
+ o.push(`<path d="${off}" stroke="${tc}" stroke-opacity="0.35" stroke-width="2.2" stroke-linecap="round"/>`);
499
+ }
500
+
501
+ o.push(`<g clip-path="url(#${id})">`);
502
+ o.push(C(128, 128, R0, bg));
503
+ const da = deco / 256 * 2 * Math.PI;
504
+ o.push(C(128 + 72 * Math.cos(da), 128 + 72 * Math.sin(da), 26, bgL, ' opacity="0.7"'));
505
+ o.push(C(128 - 78 * Math.cos(da), 128 - 78 * Math.sin(da), 36, bgD, ' opacity="0.45"'));
506
+
507
+ const hairParts = HAIR_FN[style](g, hair, clothC, hairD);
508
+ if (hairParts.back) o.push(hairParts.back);
509
+
510
+ // neck + body
511
+ o.push(`<rect x="${cx - 10}" y="${r1(cy + ry * 0.6)}" width="20" height="${r1(sh - cy - ry * 0.6 + 10)}" fill="${skinD}"/>`);
512
+ const bodyFill = cloth === 'suit' ? hsl(clothHue, 25, 30) : cloth === 'overalls' ? hsl(clothHue, 45, 82) : clothC;
513
+ o.push(P(bodyPath, bodyFill));
514
+ const crew = () => P(d`M${cx - 13} ${sh - 7} Q${cx} ${sh + 7} ${cx + 13} ${sh - 7} Z`, skinD) + S(d`M${cx - 13} ${sh - 6} Q${cx} ${sh + 8} ${cx + 13} ${sh - 6}`, cloth === 'overalls' ? hsl(clothHue, 35, 65) : clothD, 3);
515
+ if (cloth === 'tee' || cloth === 'onesie') {
516
+ o.push(crew());
517
+ if (cloth === 'onesie') o.push(C(cx - 6, sh + 13, 2, '#FFFFFF') + C(cx + 6, sh + 13, 2, '#FFFFFF'));
518
+ } else if (cloth === 'stripes') {
519
+ let st = '';
520
+ for (let k = 0; k < 9; k++) st += d`M${cx - bw} ${sh + 5 + k * 10}H${cx + bw}`;
521
+ o.push(`<g clip-path="url(#${id}b)">${S(st, clothL, 4.5)}</g>` + crew());
522
+ } else if (cloth === 'overalls') {
523
+ const dn = hsl(215, 40, 45), dnD = hsl(215, 40, 34);
524
+ o.push(`<g clip-path="url(#${id}b)"><rect x="${cx - bw}" y="${sh + 48}" width="${2 * bw}" height="60" fill="${dn}"/></g>`);
525
+ o.push(`<rect x="${cx - 19}" y="${sh + 14}" width="38" height="40" rx="3" fill="${dn}"/>`);
526
+ o.push(`<rect x="${cx - 9}" y="${sh + 24}" width="18" height="12" rx="2" fill="none" stroke="${dnD}" stroke-width="1.2"/>`);
527
+ o.push(S(d`M${cx - 30} ${sh - 4} L${cx - 15} ${sh + 18}M${cx + 30} ${sh - 4} L${cx + 15} ${sh + 18}`, dn, 6));
528
+ o.push(C(cx - 15, sh + 18, 2.2, '#E8C547') + C(cx + 15, sh + 18, 2.2, '#E8C547'));
529
+ o.push(crew());
530
+ } else if (cloth === 'hoodie') {
531
+ o.push(P(d`M${cx - 14} ${sh - 7} L${cx} ${sh + 14} L${cx + 14} ${sh - 7} Z`, skinD));
532
+ o.push(S(d`M${cx - 15} ${sh - 7} L${cx} ${sh + 15} L${cx + 15} ${sh - 7}`, clothD, 4));
533
+ o.push(S(d`M${cx - 7} ${sh + 6} L${cx - 8} ${sh + 30}M${cx + 7} ${sh + 6} L${cx + 8} ${sh + 30}`, clothL, 2.5));
534
+ } else if (cloth === 'collar') {
535
+ o.push(P(d`M${cx - 10} ${sh - 7} L${cx} ${sh + 8} L${cx + 10} ${sh - 7} Z`, skinD));
536
+ o.push(P(d`M${cx - 14} ${sh - 8} L${cx - 1} ${sh + 8} L${cx - 18} ${sh + 5} Z`, '#F4F4F0') + P(d`M${cx + 14} ${sh - 8} L${cx + 1} ${sh + 8} L${cx + 18} ${sh + 5} Z`, '#F4F4F0'));
537
+ o.push(C(cx, sh + 16, 1.7, clothD) + C(cx, sh + 28, 1.7, clothD));
538
+ } else if (cloth === 'suit') {
539
+ const tie = hsl(clothHue + 180, 55, 45), lap = hsl(clothHue, 25, 22);
540
+ o.push(P(d`M${cx - 15} ${sh - 7} L${cx} ${sh + 44} L${cx + 15} ${sh - 7} Z`, '#F4F4F0'));
541
+ o.push(P(d`M${cx - 9} ${sh - 7} L${cx} ${sh + 2} L${cx + 9} ${sh - 7} Z`, skinD));
542
+ o.push(P(d`M${cx - 3.5} ${sh} L${cx + 3.5} ${sh} L${cx + 2.5} ${sh + 6} L${cx - 2.5} ${sh + 6} Z M${cx - 2.5} ${sh + 6} L${cx + 2.5} ${sh + 6} L${cx + 5} ${sh + 32} L${cx} ${sh + 38} L${cx - 5} ${sh + 32} Z`, tie));
543
+ o.push(S(d`M${cx - 17} ${sh - 6} L${cx - 5} ${sh + 30}M${cx + 17} ${sh - 6} L${cx + 5} ${sh + 30}`, lap, 2.5));
544
+ } else if (cloth === 'turtleneck') {
545
+ o.push(`<rect x="${cx - 13}" y="${sh - 17}" width="26" height="14" rx="6" fill="${clothD}"/>`);
546
+ o.push(S(d`M${cx - 11} ${sh - 12} L${cx + 11} ${sh - 12}M${cx - 11} ${sh - 8} L${cx + 11} ${sh - 8}`, clothC, 1.4));
547
+ } else {
548
+ o.push(P(d`M${cx - 16} ${sh - 7} L${cx} ${sh + 40} L${cx + 16} ${sh - 7} Z`, clothL));
549
+ o.push(P(d`M${cx - 10} ${sh - 7} Q${cx} ${sh + 4} ${cx + 10} ${sh - 7} Z`, skinD));
550
+ o.push(S(d`M${cx - 16} ${sh - 7} L${cx} ${sh + 40} L${cx + 16} ${sh - 7}`, clothD, 3));
551
+ o.push(C(cx + 6, sh + 22, 2, clothD) + C(cx + 3, sh + 32, 2, clothD));
552
+ }
553
+ if (hairParts.mid) o.push(hairParts.mid);
554
+
555
+ // ears + head
556
+ o.push(E(L + 1, cy + 4, 7, baby ? 9 : 10, skinD) + E(R - 1, cy + 4, 7, baby ? 9 : 10, skinD));
557
+ o.push(E(cx, cy, rx, ry, skin));
558
+
559
+ const eyeY = cy + ry * (baby ? 0.14 : 0.04), ex = rx * 0.4;
560
+ const noseY = eyeY + ry * 0.27, mouthY = cy + ry * 0.5;
561
+
562
+ // age lines
563
+ const wl = ` stroke="${skinDD}" stroke-opacity="0.55"`;
564
+ if (middle) o.push(S(d`M${cx - rx * 0.35} ${top + ry * 0.48} Q${cx} ${top + ry * 0.44} ${cx + rx * 0.35} ${top + ry * 0.48}`, skinDD, 1.4, ' stroke-opacity="0.45"'));
565
+ if (old) {
566
+ let w = d`M${cx - rx * 0.4} ${top + ry * 0.44} Q${cx} ${top + ry * 0.4} ${cx + rx * 0.4} ${top + ry * 0.44}M${cx - rx * 0.32} ${top + ry * 0.54} Q${cx} ${top + ry * 0.5} ${cx + rx * 0.32} ${top + ry * 0.54}`;
567
+ for (const sgn of [-1, 1]) {
568
+ const xo = cx + sgn * (ex + 10);
569
+ w += d`M${xo} ${eyeY - 2} L${xo + sgn * 4} ${eyeY - 4}M${xo} ${eyeY + 1} L${xo + sgn * 4.5} ${eyeY + 2}`;
570
+ w += d`M${cx + sgn * ex - 5} ${eyeY + 7} Q${cx + sgn * ex} ${eyeY + 9.5} ${cx + sgn * ex + 5} ${eyeY + 7}`;
571
+ w += d`M${cx + sgn * 11} ${noseY + 4} Q${cx + sgn * 15} ${mouthY - 2} ${cx + sgn * 13} ${mouthY + 4}`;
572
+ }
573
+ o.push(`<path d="${w}" fill="none" stroke-width="1.4" stroke-linecap="round"${wl}/>`);
574
+ }
575
+ if (blush) { const bc = mix(skin, '#E0505E', 0.4); o.push(E(cx - rx * 0.58, eyeY + ry * 0.3, 7, 4, bc, ' opacity="0.5"') + E(cx + rx * 0.58, eyeY + ry * 0.3, 7, 4, bc, ' opacity="0.5"')); }
576
+ if (freckles) {
577
+ for (const sgn of [-1, 1]) for (const [dx, dy] of [[-4, -1], [0, 1.5], [4, -0.5], [1, -3]])
578
+ o.push(C(cx + sgn * rx * 0.45 + dx, eyeY + ry * 0.24 + dy, 1.1, skinDD, ' opacity="0.6"'));
579
+ }
580
+
581
+ // facial hair
582
+ const mustache = P(d`M${cx - 14} ${mouthY - 2} Q${cx - 8} ${mouthY - 10} ${cx} ${mouthY - 7} Q${cx + 8} ${mouthY - 10} ${cx + 14} ${mouthY - 2} Q${cx + 6} ${mouthY - 5} ${cx} ${mouthY - 4} Q${cx - 6} ${mouthY - 5} ${cx - 14} ${mouthY - 2} Z`, hair);
583
+ const beardInner = d` Q${cx + rx * 0.6} ${cy + ry * 0.25} ${cx + rx * 0.35} ${cy + ry * 0.55} Q${cx} ${cy + ry * 0.68} ${cx - rx * 0.35} ${cy + ry * 0.55} Q${cx - rx * 0.6} ${cy + ry * 0.25} ${L} ${cy - ry * 0.05} Z`;
584
+ if (beard === 'stubble') o.push(P(d`M${L + 2} ${cy + ry * 0.15} Q${L + 4} ${cy + ry * 0.95} ${cx} ${cy + ry * 1.02} Q${R - 4} ${cy + ry * 0.95} ${R - 2} ${cy + ry * 0.15} Q${cx + rx * 0.5} ${cy + ry * 0.5} ${cx} ${cy + ry * 0.42} Q${cx - rx * 0.5} ${cy + ry * 0.5} ${L + 2} ${cy + ry * 0.15} Z`, hair, ' opacity="0.22"'));
585
+ else if (beard === 'beard') o.push(P(d`M${L} ${cy - ry * 0.05} Q${L + 1} ${cy + ry * 0.95} ${cx} ${cy + ry * 1.16} Q${R - 1} ${cy + ry * 0.95} ${R} ${cy - ry * 0.05}` + beardInner, hair) + mustache);
586
+ else if (beard === 'longbeard') o.push(P(d`M${L} ${cy - ry * 0.05} Q${L + 1} ${cy + ry * 1.15} ${cx} ${cy + ry * 1.62} Q${R - 1} ${cy + ry * 1.15} ${R} ${cy - ry * 0.05}` + beardInner, hair) + S(d`M${cx - 6} ${cy + ry * 0.95} Q${cx - 4} ${cy + ry * 1.25} ${cx - 2} ${cy + ry * 1.4}M${cx + 6} ${cy + ry * 0.95} Q${cx + 4} ${cy + ry * 1.25} ${cx + 2} ${cy + ry * 1.4}`, hairD, 1.2, ' stroke-opacity="0.6"') + mustache);
587
+ else if (beard === 'mustache') o.push(mustache);
588
+ else if (beard === 'goatee') o.push(mustache + P(d`M${cx - 7} ${mouthY + 7} Q${cx} ${cy + ry * 1.1} ${cx + 7} ${mouthY + 7} Q${cx} ${mouthY + 10} ${cx - 7} ${mouthY + 7} Z`, hair));
589
+ else if (beard === 'chinstrap') o.push(S(d`M${L + 1} ${cy - ry * 0.05} Q${L + 2} ${cy + ry * 0.95} ${cx} ${cy + ry * 1.06} Q${R - 2} ${cy + ry * 0.95} ${R - 1} ${cy - ry * 0.05}`, hair, 4));
590
+ else if (beard === 'soulpatch') o.push(E(cx, mouthY + 8, 2.6, 3.4, hair));
591
+ else if (beard === 'sideburns') o.push(P(d`M${L + 0.5} ${g.y0 + 2} L${L + 6} ${g.y0 + 2} L${L + 6} ${cy + ry * 0.3} Q${L + 3} ${cy + ry * 0.36} ${L + 1} ${cy + ry * 0.3} Z M${R - 0.5} ${g.y0 + 2} L${R - 6} ${g.y0 + 2} L${R - 6} ${cy + ry * 0.3} Q${R - 3} ${cy + ry * 0.36} ${R - 1} ${cy + ry * 0.3} Z`, hair));
592
+ else if (beard === 'handlebar') o.push(S(d`M${cx} ${mouthY - 6} Q${cx - 9} ${mouthY - 10} ${cx - 15} ${mouthY - 5} Q${cx - 19} ${mouthY - 1} ${cx - 17} ${mouthY - 8}M${cx} ${mouthY - 6} Q${cx + 9} ${mouthY - 10} ${cx + 15} ${mouthY - 5} Q${cx + 19} ${mouthY - 1} ${cx + 17} ${mouthY - 8}`, hair, 3.6));
593
+
594
+ // nose
595
+ if (noseStyle === 'hook') o.push(S(d`M${cx - 1} ${noseY - 8} Q${cx - 5} ${noseY + 2} ${cx + 1} ${noseY + 3}`, skinDD, 2.4));
596
+ else if (noseStyle === 'button') o.push(E(cx, noseY, baby ? 3.5 : 4, baby ? 2.6 : 3, skinD));
597
+ else o.push(C(cx - 3, noseY + 1, 1.4, skinDD) + C(cx + 3, noseY + 1, 1.4, skinDD));
598
+
599
+ // mouth
600
+ if (mouthStyle === 'smile') o.push(S(d`M${cx - 10} ${mouthY - 2} Q${cx} ${mouthY + 8} ${cx + 10} ${mouthY - 2}`, lip, 3));
601
+ else if (mouthStyle === 'grin') o.push(P(d`M${cx - 11} ${mouthY - 3} Q${cx} ${mouthY + 13} ${cx + 11} ${mouthY - 3} Z`, inner) + P(d`M${cx - 9.5} ${mouthY - 2.6} L${cx + 9.5} ${mouthY - 2.6} Q${cx} ${mouthY + 2.5} ${cx - 9.5} ${mouthY - 2.6} Z`, '#FFFFFF'));
602
+ else if (mouthStyle === 'o') o.push(E(cx, mouthY + 1, 3.5, 4, inner));
603
+ else if (mouthStyle === 'smirk') o.push(S(d`M${cx - 8} ${mouthY + 1} Q${cx + 2} ${mouthY + 4} ${cx + 10} ${mouthY - 3}`, lip, 3));
604
+ else if (mouthStyle === 'flat') o.push(S(d`M${cx - 7} ${mouthY + 1} L${cx + 7} ${mouthY + 1}`, lip, 3));
605
+ else {
606
+ const pc = hsl(clothHue, 70, 66);
607
+ o.push(S(d`M${cx} ${mouthY + 5} m-3.5 0 a3.5 3.5 0 1 0 7 0 a3.5 3.5 0 1 0 -7 0`, pc, 1.8) + E(cx, mouthY, 9, 5.5, pc) + C(cx, mouthY, 2.6, hsl(clothHue, 70, 82)));
608
+ }
609
+
610
+ // eyes
611
+ for (const sgn of [-1, 1]) {
612
+ const x = cx + sgn * ex, y = eyeY;
613
+ if (eyeStyle === 'dot') {
614
+ o.push(C(x, y, baby ? 5.5 : 4.3, INK) + C(x + 1.4, y - 1.6, 1.5, '#FFFFFF'));
615
+ } else if (eyeStyle === 'happy') {
616
+ o.push(S(d`M${x - 6} ${y + 1.5} Q${x} ${y - 5} ${x + 6} ${y + 1.5}`, INK, 3));
617
+ } else {
618
+ const sx = baby ? 8 : 7, sy = baby ? 7 : 5.6, ir = baby ? 5.4 : 4;
619
+ o.push(E(x, y, sx, sy, '#FFFFFF', ` stroke="${INK}" stroke-opacity="0.5" stroke-width="1.2"`) + C(x, y + 0.3, ir, eyeColor) + C(x, y + 0.3, ir * 0.5, '#1E1A18') + C(x + 1.4, y - 1.3, 1.3, '#FFFFFF'));
620
+ if (eyeStyle === 'sleepy') o.push(P(d`M${x - 7.6} ${y + 0.5} A7.6 6.2 0 0 1 ${x + 7.6} ${y + 0.5} Z`, skinD) + S(d`M${x - 7.6} ${y + 0.5} L${x + 7.6} ${y + 0.5}`, INK, 1.6));
621
+ }
622
+ if (female && !baby) o.push(S(d`M${x + sgn * 6} ${y - 3} L${x + sgn * 9.5} ${y - 6.5}`, INK, 2));
623
+ }
624
+
625
+ // brows
626
+ const by = eyeY - (baby ? 12 : 11.5);
627
+ let bp = '';
628
+ for (const sgn of [-1, 1]) {
629
+ const x = cx + sgn * ex;
630
+ if (browStyle === 'straight') bp += d`M${x - 6} ${by}L${x + 6} ${by}`;
631
+ else if (browStyle === 'angled') bp += d`M${x - sgn * 6} ${by - 1}L${x + sgn * 6} ${by + 1.5}`;
632
+ else { const yy = by - (browStyle === 'raised' ? 2 : 0); bp += d`M${x - 6.5} ${yy + 1.5}Q${x} ${yy - 3.5} ${x + 6.5} ${yy + 1.5}`; }
633
+ }
634
+ o.push(S(bp, hairD, baby ? 1.6 : browStyle === 'thick' ? 4.5 : 3, baby ? ' stroke-opacity="0.45"' : ''));
635
+
636
+ o.push(hairParts.front);
637
+
638
+ // glasses
639
+ if (glasses !== 'none') {
640
+ const sun = glasses === 'sun';
641
+ const lens = sun ? ` fill="#1E2228" fill-opacity="0.88" stroke="${INK}" stroke-width="2.2"` : ` fill="#FFFFFF" fill-opacity="0.18" stroke="${glassColor}" stroke-width="2.2"`;
642
+ const fc = sun ? INK : glassColor;
643
+ let gl = '';
644
+ for (const sgn of [-1, 1]) {
645
+ const x = cx + sgn * ex;
646
+ if (glasses === 'round') gl += `<circle cx="${r1(x)}" cy="${r1(eyeY)}" r="9.5"${lens}/>`;
647
+ else if (glasses === 'rect') gl += `<rect x="${r1(x - 10.5)}" y="${r1(eyeY - 7.5)}" width="21" height="15" rx="4"${lens}/>`;
648
+ else if (sun) gl += `<path d="${d`M${x - 11} ${eyeY - 6} L${x + 11} ${eyeY - 6} Q${x + 11} ${eyeY + 8} ${x} ${eyeY + 8} Q${x - 11} ${eyeY + 8} ${x - 11} ${eyeY - 6} Z`}"${lens}/>` + S(d`M${x - 7} ${eyeY - 3} L${x - 3} ${eyeY - 3}`, '#FFFFFF', 1.4, ' stroke-opacity="0.5"');
649
+ else gl += `<path d="${d`M${x - 10.5} ${eyeY - 5} Q${x - 10.5} ${eyeY + 7} ${x} ${eyeY + 7} Q${x + 10.5} ${eyeY + 7} ${x + 10.5} ${eyeY - 5} Z`}" fill="#FFFFFF" fill-opacity="0.18" stroke="${glassColor}" stroke-width="1.2"/>` + S(d`M${x - 10.5} ${eyeY - 5} L${x + 10.5} ${eyeY - 5}`, glassColor, 3.2);
650
+ }
651
+ const inset = glasses === 'round' ? 9.5 : glasses === 'sun' ? 11 : 10.5, gy = glasses === 'half' || sun ? eyeY - 5 : eyeY;
652
+ gl += S(d`M${cx - ex + inset} ${gy} Q${cx} ${gy - 3} ${cx + ex - inset} ${gy}M${cx - ex - inset} ${gy - 1} L${L + 1} ${gy - 3}M${cx + ex + inset} ${gy - 1} L${R - 1} ${gy - 3}`, fc, 2.2);
653
+ o.push(gl);
654
+ }
655
+
656
+ if (hat !== 'none') o.push(headwear(hat, g, hatHue));
657
+
658
+ // accessories
659
+ if (acc === 'headphones') {
660
+ const hp = '#3A3A38', ac = hsl(clothHue, 60, 60);
661
+ o.push(S(d`M${L - 7} ${cy} A${rx + 7} ${ry + 13} 0 0 1 ${R + 7} ${cy}`, hp, 6));
662
+ o.push(`<rect x="${r1(L - 11)}" y="${r1(cy - 12)}" width="12" height="26" rx="5" fill="${hp}"/><rect x="${r1(R - 1)}" y="${r1(cy - 12)}" width="12" height="26" rx="5" fill="${hp}"/>`);
663
+ o.push(`<rect x="${r1(L - 8)}" y="${r1(cy - 6)}" width="5" height="14" rx="2.5" fill="${ac}"/><rect x="${r1(R + 3)}" y="${r1(cy - 6)}" width="5" height="14" rx="2.5" fill="${ac}"/>`);
664
+ } else if (acc === 'earrings') {
665
+ o.push(C(L + 1, cy + 15, 2.6, '#D4A537') + C(R - 1, cy + 15, 2.6, '#D4A537'));
666
+ }
667
+ if (baby && female && hat === 'none' && (X(2) & 1) && style !== 'sprout') {
668
+ const bx = cx + rx * 0.45, byy = top + ry * 0.12, pk = hsl(340, 70, 72);
669
+ o.push(P(d`M${bx} ${byy} L${bx - 9} ${byy - 6} L${bx - 9} ${byy + 6} Z M${bx} ${byy} L${bx + 9} ${byy - 6} L${bx + 9} ${byy + 6} Z`, pk) + C(bx, byy, 2.8, hsl(340, 60, 55)));
670
+ }
671
+
672
+ o.push('</g></svg>');
673
+ return o.join('');
674
+ }
675
+
676
+ const api = { svg, hash, normalize, sha256, GENDERS, AGES, RACES, version: 'v1' };
677
+ if (typeof module !== 'undefined' && module.exports) module.exports = api;
678
+ else root.HashAvatar = api;
679
+ })(typeof globalThis !== 'undefined' ? globalThis : this);