hashavatar 1.1.0

Stable deterministic procedural avatars in Rust with configurable identity hashing, WebP, optional PNG/JPEG/GIF, and SVG export
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
use super::*;

pub fn render_monster_avatar_for_identity(
    spec: AvatarSpec,
    identity: &AvatarIdentity,
    background: AvatarBackground,
) -> Result<RgbaImage, AvatarSpecError> {
    spec.validate()?;

    let width = spec.width as i32;
    let height = spec.height as i32;
    let center_x = width / 2;
    let center_y = (height as f32 * 0.58) as i32;
    let mut image =
        ImageBuffer::from_pixel(spec.width, spec.height, Color::rgb(255, 255, 255).into());

    let skin = hsl_to_color(
        identity.unit_f32(0) * 360.0,
        0.48 + identity.unit_f32(1) * 0.24,
        0.46 + identity.unit_f32(2) * 0.20,
    );
    let shade = hsl_to_color(
        identity.unit_f32(3) * 360.0,
        0.38 + identity.unit_f32(4) * 0.18,
        0.24 + identity.unit_f32(5) * 0.10,
    );
    let accent = hsl_to_color(
        20.0 + identity.unit_f32(6) * 320.0,
        0.34 + identity.unit_f32(7) * 0.26,
        0.86,
    );
    let mouth = Color::rgb(48, 18, 24);
    let eye_white = Color::rgb(252, 248, 236);
    let pupil = Color::rgb(24, 20, 28);

    image
        .pixels_mut()
        .for_each(|pixel| *pixel = background_fill(background, accent).into());

    let head_rx = (width as f32 * (0.23 + identity.unit_f32(8) * 0.10)) as i32;
    let head_ry = (height as f32 * (0.22 + identity.unit_f32(9) * 0.11)) as i32;
    let horn_height = (height as f32 * (0.08 + identity.unit_f32(10) * 0.10)) as i32;
    let horn_width = (width as f32 * (0.06 + identity.unit_f32(11) * 0.05)) as i32;
    let eye_count = 1 + (identity.byte(12) % 3) as usize;
    let mouth_style = identity.byte(13) % 3;
    let body_style = identity.byte(14) % 3;
    let spot_count = 3 + (identity.byte(15) % 5) as i32;
    let tentacle_count = 2 + (identity.byte(16) % 4) as i32;

    draw_background_accent(
        &mut image,
        center_x,
        center_y,
        head_rx,
        head_ry,
        shade,
        0.30 + identity.unit_f32(17) * 0.25,
        background,
        identity,
    );

    match body_style {
        0 => draw_filled_ellipse_mut(
            &mut image,
            (center_x, center_y),
            head_rx,
            head_ry,
            skin.into(),
        ),
        1 => {
            draw_filled_ellipse_mut(
                &mut image,
                (center_x, center_y + head_ry / 8),
                head_rx,
                head_ry - head_ry / 8,
                skin.into(),
            );
            draw_polygon_mut(
                &mut image,
                &[
                    Point::new(center_x - head_rx, center_y),
                    Point::new(center_x, center_y - head_ry),
                    Point::new(center_x + head_rx, center_y),
                    Point::new(center_x + head_rx / 2, center_y + head_ry),
                    Point::new(center_x - head_rx / 2, center_y + head_ry),
                ],
                skin.into(),
            );
        }
        _ => {
            draw_filled_rect_mut(
                &mut image,
                Rect::at(center_x - head_rx, center_y - head_ry)
                    .of_size((head_rx * 2) as u32, (head_ry * 2) as u32),
                skin.into(),
            );
            draw_filled_circle_mut(
                &mut image,
                (center_x - head_rx, center_y - head_ry / 2),
                head_ry / 2,
                skin.into(),
            );
            draw_filled_circle_mut(
                &mut image,
                (center_x + head_rx, center_y - head_ry / 2),
                head_ry / 2,
                skin.into(),
            );
            draw_filled_circle_mut(
                &mut image,
                (center_x - head_rx, center_y + head_ry / 2),
                head_ry / 2,
                skin.into(),
            );
            draw_filled_circle_mut(
                &mut image,
                (center_x + head_rx, center_y + head_ry / 2),
                head_ry / 2,
                skin.into(),
            );
        }
    }

    if identity.byte(18).is_multiple_of(2) {
        draw_polygon_mut(
            &mut image,
            &[
                Point::new(center_x - head_rx / 2, center_y - head_ry),
                Point::new(
                    center_x - head_rx / 3 - horn_width,
                    center_y - head_ry - horn_height,
                ),
                Point::new(center_x - head_rx / 8, center_y - head_ry / 2),
            ],
            shade.into(),
        );
        draw_polygon_mut(
            &mut image,
            &[
                Point::new(center_x + head_rx / 2, center_y - head_ry),
                Point::new(
                    center_x + head_rx / 3 + horn_width,
                    center_y - head_ry - horn_height,
                ),
                Point::new(center_x + head_rx / 8, center_y - head_ry / 2),
            ],
            shade.into(),
        );
    } else {
        for spike in 0..3 {
            let spike_x = center_x - head_rx / 2 + spike * head_rx / 2;
            draw_polygon_mut(
                &mut image,
                &[
                    Point::new(spike_x - horn_width / 2, center_y - head_ry / 2),
                    Point::new(spike_x, center_y - head_ry - horn_height / 2),
                    Point::new(spike_x + horn_width / 2, center_y - head_ry / 2),
                ],
                shade.into(),
            );
        }
    }

    for index in 0..spot_count {
        let x = center_x - head_rx / 2 + (index * head_rx) / spot_count;
        let y = center_y - head_ry / 3 + ((index * 37 + identity.byte(19) as i32) % head_ry.max(1));
        let radius =
            (head_rx as f32 * (0.05 + ((index + 1) as f32 / spot_count as f32) * 0.06)) as i32;
        draw_filled_circle_mut(
            &mut image,
            (x, y),
            radius.max(3),
            Color::rgba(shade.0[0], shade.0[1], shade.0[2], 168).into(),
        );
    }

    let eye_y = center_y - head_ry / 5;
    let eye_rx = (head_rx as f32 * (0.10 + identity.unit_f32(20) * 0.08)) as i32;
    let eye_ry = (head_ry as f32 * (0.10 + identity.unit_f32(21) * 0.10)) as i32;
    let eye_spacing = if eye_count == 1 {
        0
    } else {
        (head_rx as f32 * 0.46 / (eye_count - 1) as f32) as i32
    };
    let eye_start = center_x - eye_spacing * ((eye_count.saturating_sub(1)) as i32) / 2;
    for index in 0..eye_count {
        let x = eye_start + eye_spacing * index as i32;
        draw_filled_ellipse_mut(&mut image, (x, eye_y), eye_rx, eye_ry, eye_white.into());
        if identity.byte(22).is_multiple_of(2) {
            draw_filled_ellipse_mut(
                &mut image,
                (x, eye_y),
                (eye_rx / 3).max(2),
                (eye_ry - 1).max(2),
                pupil.into(),
            );
        } else {
            draw_filled_circle_mut(&mut image, (x, eye_y), (eye_ry / 2).max(2), pupil.into());
        }
        draw_filled_circle_mut(
            &mut image,
            (x - eye_rx / 3, eye_y - eye_ry / 3),
            (eye_rx / 5).max(1),
            Color::rgba(255, 255, 255, 220).into(),
        );
    }

    let mouth_y = center_y + head_ry / 3;
    match mouth_style {
        0 => {
            draw_filled_ellipse_mut(
                &mut image,
                (center_x, mouth_y),
                head_rx / 3,
                head_ry / 8,
                mouth.into(),
            );
            for fang_x in [center_x - head_rx / 8, center_x + head_rx / 8] {
                draw_polygon_mut(
                    &mut image,
                    &[
                        Point::new(fang_x - head_rx / 24, mouth_y - 2),
                        Point::new(fang_x + head_rx / 24, mouth_y - 2),
                        Point::new(fang_x, mouth_y + head_ry / 5),
                    ],
                    eye_white.into(),
                );
            }
        }
        1 => {
            draw_smile_arc(
                &mut image,
                center_x - head_rx / 10,
                mouth_y,
                head_rx / 4,
                mouth,
                0.50,
            );
            draw_smile_arc(
                &mut image,
                center_x + head_rx / 10,
                mouth_y,
                head_rx / 4,
                mouth,
                0.50,
            );
            draw_line_segment_mut(
                &mut image,
                ((center_x - head_rx / 4) as f32, mouth_y as f32),
                ((center_x + head_rx / 4) as f32, mouth_y as f32),
                mouth.into(),
            );
        }
        _ => {
            draw_filled_rect_mut(
                &mut image,
                Rect::at(center_x - head_rx / 3, mouth_y - head_ry / 10)
                    .of_size((head_rx * 2 / 3) as u32, (head_ry / 5).max(1) as u32),
                mouth.into(),
            );
            for tooth in 0..4 {
                let tooth_x = center_x - head_rx / 4 + tooth * head_rx / 6;
                draw_polygon_mut(
                    &mut image,
                    &[
                        Point::new(tooth_x - head_rx / 30, mouth_y - head_ry / 10),
                        Point::new(tooth_x + head_rx / 30, mouth_y - head_ry / 10),
                        Point::new(tooth_x, mouth_y + head_ry / 14),
                    ],
                    eye_white.into(),
                );
            }
        }
    }

    if identity.byte(23).is_multiple_of(2) {
        for index in 0..tentacle_count {
            let start_x = center_x - head_rx / 2 + (index * head_rx) / tentacle_count;
            let start_y = center_y + head_ry - 4;
            let end_x = start_x + ((index % 2) * 2 - 1) * head_rx / 6;
            let end_y = start_y + head_ry / 2;
            draw_antialiased_line_segment_mut(
                &mut image,
                (start_x, start_y),
                (end_x, end_y),
                shade.into(),
                interpolate,
            );
            draw_filled_circle_mut(
                &mut image,
                (end_x, end_y),
                (head_rx / 18).max(2),
                shade.into(),
            );
        }
    }

    Ok(image)
}

#[derive(Clone, Copy)]
pub(crate) struct FaceLayout {
    pub(crate) center_x: i32,
    pub(crate) center_y: i32,
    pub(crate) head_rx: i32,
    pub(crate) head_ry: i32,
}

#[derive(Clone, Copy)]
pub(crate) enum CreatureEyeStyle {
    Round,
    Tall,
    Hollow,
}

#[derive(Clone, Copy)]
#[allow(dead_code)]
pub(crate) enum CreatureMouthStyle {
    Smile,
    Fang,
    Flat,
}

pub(crate) fn draw_creature_eyes(
    image: &mut RgbaImage,
    layout: FaceLayout,
    count: usize,
    style: CreatureEyeStyle,
    eye_white: Color,
    pupil: Color,
) {
    let spacing = if count <= 1 {
        0
    } else {
        (layout.head_rx as f32 * 0.48 / (count - 1) as f32) as i32
    };
    let start_x = layout.center_x - spacing * (count.saturating_sub(1) as i32) / 2;
    let eye_y = layout.center_y - layout.head_ry / 5;
    let eye_rx = (layout.head_rx as f32 * 0.12) as i32;
    let eye_ry = (layout.head_ry as f32 * 0.12) as i32;

    for index in 0..count {
        let x = start_x + spacing * index as i32;
        match style {
            CreatureEyeStyle::Round => {
                draw_filled_circle_mut(image, (x, eye_y), eye_rx.max(3), eye_white.into());
                draw_filled_circle_mut(image, (x, eye_y), (eye_rx / 2).max(2), pupil.into());
            }
            CreatureEyeStyle::Tall => {
                draw_filled_ellipse_mut(image, (x, eye_y), eye_rx, eye_ry + 4, eye_white.into());
                draw_filled_ellipse_mut(
                    image,
                    (x, eye_y),
                    (eye_rx / 3).max(2),
                    (eye_ry + 2).max(2),
                    pupil.into(),
                );
            }
            CreatureEyeStyle::Hollow => {
                draw_filled_ellipse_mut(image, (x, eye_y), eye_rx + 3, eye_ry + 5, pupil.into());
                draw_filled_ellipse_mut(
                    image,
                    (x, eye_y + 1),
                    (eye_rx / 2).max(2),
                    (eye_ry / 2).max(2),
                    Color::rgba(255, 255, 255, 20).into(),
                );
            }
        }
    }
}

pub(crate) fn draw_creature_mouth(
    image: &mut RgbaImage,
    layout: FaceLayout,
    style: CreatureMouthStyle,
    color: Color,
) {
    let mouth_y = layout.center_y + layout.head_ry / 3;
    match style {
        CreatureMouthStyle::Smile => {
            draw_smile_arc(
                image,
                layout.center_x - layout.head_rx / 10,
                mouth_y,
                layout.head_rx / 4,
                color,
                0.45,
            );
            draw_smile_arc(
                image,
                layout.center_x + layout.head_rx / 10,
                mouth_y,
                layout.head_rx / 4,
                color,
                0.45,
            );
        }
        CreatureMouthStyle::Fang => {
            draw_filled_ellipse_mut(
                image,
                (layout.center_x, mouth_y),
                layout.head_rx / 3,
                layout.head_ry / 8,
                color.into(),
            );
            for tooth_x in [
                layout.center_x - layout.head_rx / 7,
                layout.center_x + layout.head_rx / 7,
            ] {
                draw_polygon_mut(
                    image,
                    &[
                        Point::new(tooth_x - layout.head_rx / 26, mouth_y - 1),
                        Point::new(tooth_x + layout.head_rx / 26, mouth_y - 1),
                        Point::new(tooth_x, mouth_y + layout.head_ry / 5),
                    ],
                    Color::rgb(248, 246, 238).into(),
                );
            }
        }
        CreatureMouthStyle::Flat => {
            draw_filled_rect_mut(
                image,
                Rect::at(layout.center_x - layout.head_rx / 3, mouth_y - 3)
                    .of_size((layout.head_rx * 2 / 3) as u32, 6),
                color.into(),
            );
        }
    }
}