rdenticon 0.1.4

An implementation of jdenticon in pure Rust.
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
#![allow(
    clippy::cast_precision_loss,
    clippy::cast_lossless,
    clippy::cast_possible_truncation,
    clippy::cast_sign_loss
)]

mod config;
mod hsl;

pub use config::*;
use hsl::corrected_hsl_to_rgb;
use ril::prelude::*;
pub use ril::{self, ImageFormat};

/// Colors used by an identicon.
struct ColorCandidates {
    light_gray: Rgba,
    dark_gray: Rgba,
    light_color: Rgba,
    mid_color: Rgba,
    dark_color: Rgba,
}

impl ColorCandidates {
    #[inline]
    const fn get_from_rotation_index(&self, index: usize) -> Rgba {
        match index {
            0 => self.dark_gray,
            1 => self.mid_color,
            2 => self.light_gray,
            3 => self.light_color,
            _ /* 4 */ => self.dark_color,
        }
    }
}

impl Config {
    /// Retrieves a hue allowed by the configured hues.
    pub(crate) fn resolve_hue(&self, hue: f64) -> f64 {
        if self.hues.is_empty() {
            hue
        } else {
            self.hues[(hue / 360.0 * self.hues.len() as f64) as usize]
        }
    }

    /// Retrieves a color lightness that conforms to the configured lightness range. The lightness
    /// is expected to be in the range `[0.0, 1.0]`.
    #[inline]
    pub(crate) fn resolve_color_lightness(&self, lightness: f64) -> f64 {
        (self.color_lightness.end() - self.color_lightness.start())
            .mul_add(lightness, *self.color_lightness.start())
    }

    /// Retrieves a grayscale lightness that conforms to the configured lightness range. The
    /// lightness is expected to be in the range `[0.0, 1.0]`.
    #[inline]
    pub(crate) fn resolve_grayscale_lightness(&self, lightness: f64) -> f64 {
        (self.grayscale_lightness.end() - self.grayscale_lightness.start())
            .mul_add(lightness, *self.grayscale_lightness.start())
    }

    /// Retrieves a set of color candidates that conform to this configuration.
    pub(crate) fn color_candidates(&self, hue: f64) -> ColorCandidates {
        let hue = self.resolve_hue(hue);

        macro_rules! resolve {
            ($s:ident, $l_meth:ident, $l_value:literal) => {{
                corrected_hsl_to_rgb(hue, self.$s, self.$l_meth($l_value)).into_rgba()
            }};
            (@grayscale $l_value:literal) => {{
                resolve!(grayscale_saturation, resolve_grayscale_lightness, $l_value)
            }};
            (@color $l_value:literal) => {{
                resolve!(color_saturation, resolve_color_lightness, $l_value)
            }};
        }

        ColorCandidates {
            light_gray: resolve!(@grayscale 1.0),
            dark_gray: resolve!(@grayscale 0.0),
            light_color: resolve!(@color 1.0),
            mid_color: resolve!(@color 0.5),
            dark_color: resolve!(@color 0.0),
        }
    }
}

// (x, y, size, rotation)
#[derive(Copy, Clone, Default)]
struct Transform {
    x: u32,
    y: u32,
    pub rotation: u8,
    right: u32,
    bottom: u32,
}

impl Transform {
    pub(crate) const fn new(x: u32, y: u32, size: u32, rotation: u8) -> Self {
        Self {
            x,
            y,
            rotation,
            right: x + size,
            bottom: y + size,
        }
    }

    pub(crate) const fn transform(&self, (x, y): (u32, u32), (w, h): (u32, u32)) -> (u32, u32) {
        match self.rotation {
            0 => (self.x + x, self.y + y),
            1 => (self.right - y - h, self.y + x),
            2 => (self.right - x - w, self.bottom - y - h),
            _ /* 3 */ => (self.x + y, self.bottom - x - w),
        }
    }
}

struct ShapeRenderer<'a> {
    image: &'a mut Image<Rgba>,
    pub current_transform: Transform,
}

impl<'a> ShapeRenderer<'a> {
    pub fn new(image: &'a mut Image<Rgba>) -> Self {
        Self {
            image,
            current_transform: Transform::default(),
        }
    }

    pub fn polygon(
        &mut self,
        color: Rgba,
        points: impl IntoIterator<Item = (u32, u32)>,
    ) -> &mut Self {
        let polygon = Polygon::from_vertices(
            points
                .into_iter()
                .map(|pos| self.current_transform.transform(pos, (0, 0))),
        )
        .with_fill(color);

        self.image.draw(&polygon);
        self
    }

    pub fn circle(&mut self, color: Rgba, top_left: (u32, u32), diameter: u32) -> &mut Self {
        let (x, y) = self
            .current_transform
            .transform(top_left, (diameter, diameter));
        let circle = Ellipse::from_bounding_box(x, y, x + diameter, y + diameter).with_fill(color);

        self.image.draw(&circle);
        self
    }

    // top left is top left of the bounding box
    // this creates a right triangle
    pub fn triangle<const ROTATION: usize>(
        &mut self,
        color: Rgba,
        (x, y): (u32, u32),
        (w, h): (u32, u32),
    ) -> &mut Self {
        let (a, b, c, d) = ((x + w, y), (x + w, y + h), (x, y + h), (x, y));
        let points = match ROTATION % 4 {
            0 => [b, c, d],
            1 => [a, c, d],
            2 => [a, b, d],
            3 => [a, b, c],
            // SAFETY: `rotation % 4` on an unsigned int is always in the range `[0, 3]`.
            _ => unsafe { std::hint::unreachable_unchecked() },
        };

        self.polygon(color, points);
        self
    }

    pub fn rectangle(
        &mut self,
        color: Rgba,
        top_left: (u32, u32),
        mut size: (u32, u32),
    ) -> &mut Self {
        let (x, y) = self.current_transform.transform(top_left, size);
        if self.current_transform.rotation & 1 == 1 {
            std::mem::swap(&mut size.0, &mut size.1);
        }

        let rect = Rectangle::new()
            .with_position(x, y)
            .with_size(size.0, size.1 + 1)
            .with_fill(color);

        self.image.draw(&rect);
        self
    }

    // top left is top left of the bounding box
    pub fn rhombus(&mut self, color: Rgba, top_left: (u32, u32), size: (u32, u32)) -> &mut Self {
        self.polygon(
            color,
            [
                (top_left.0 + size.0 / 2, top_left.1),
                (top_left.0 + size.0, top_left.1 + size.1 / 2),
                (top_left.0 + size.0 / 2, top_left.1 + size.1),
                (top_left.0, top_left.1 + size.1 / 2),
            ],
        )
    }
}

#[inline]
fn pad_zeroes<const FROM: usize, const TO: usize>(arr: [u8; FROM]) -> [u8; TO] {
    let mut b = [0; TO];
    b[TO - FROM..].copy_from_slice(&arr);
    b
}

fn into_nibbles(hash: [u8; 20]) -> [u8; 40] {
    let mut nibbles = [0; 40];
    for i in 0..20 {
        nibbles[i * 2] = hash[i] >> 4;
        nibbles[i * 2 + 1] = hash[i] & 0x0f;
    }
    nibbles
}

#[inline]
fn hash_substring_u32<const LEN: usize>(nibbles: &[u8; 40], start: usize) -> u32 {
    let nibbles = pad_zeroes::<LEN, 8>(unsafe {
        // SAFETY: The hash is always 20 bytes long
        nibbles[start..start + LEN].try_into().unwrap_unchecked()
    });
    // Join nibbles back into bytes
    let mut bytes = [0; 4];
    for i in 0..4 {
        bytes[i] = nibbles[i * 2] << 4 | nibbles[i * 2 + 1];
    }

    u32::from_be_bytes(bytes)
}

#[allow(clippy::too_many_arguments)]
fn render_shape(
    hash: &[u8; 40],
    shape_index: usize,
    rotation_index: Option<usize>,
    renderer: &mut ShapeRenderer,
    color: Rgba,
    background_color: Rgba,
    cell_offset: u32,
    cell_size: u32,
    render_fn: impl Fn(&mut ShapeRenderer, Rgba, Rgba, u32, u8, usize),
    render_positions: impl IntoIterator<Item = (u32, u32)>,
) {
    let mut rotation = rotation_index.map(|idx| hash[idx]).unwrap_or_default();
    let shape_index = hash[shape_index];

    render_positions
        .into_iter()
        .enumerate()
        .for_each(|(i, (x, y))| {
            renderer.current_transform = Transform::new(
                cell_offset + x * cell_size,
                cell_offset + y * cell_size,
                cell_size,
                rotation % 4,
            );
            rotation += 1;

            render_fn(renderer, color, background_color, cell_size, shape_index, i);
        });
}

fn render_outer(
    renderer: &mut ShapeRenderer,
    color: Rgba,
    _background_color: Rgba,
    cell_size: u32,
    shape_index: u8,
    _position_index: usize,
) {
    match shape_index % 4 {
        0 => renderer.triangle::<0>(color, (0, 0), (cell_size, cell_size)),
        1 => renderer.triangle::<0>(color, (0, cell_size / 2), (cell_size, cell_size / 2)),
        2 => renderer.rhombus(color, (0, 0), (cell_size, cell_size)),
        _ /* 3 */ => {
            let m = cell_size / 6;
            renderer.circle(color, (m, m), cell_size - 2 * m)
        },
    };
}

#[allow(clippy::too_many_lines)]
fn render_center(
    renderer: &mut ShapeRenderer,
    color: Rgba,
    background_color: Rgba,
    cell_size: u32,
    shape_index: u8,
    position_index: usize,
) {
    match shape_index % 14 {
        0 => {
            let k = (cell_size as f64 * 0.42) as u32;
            renderer.polygon(
                color,
                [
                    (0, 0),
                    (cell_size, 0),
                    (cell_size, cell_size - k * 2),
                    (cell_size - k, cell_size),
                    (0, cell_size),
                ],
            );
        }
        1 => {
            let w = cell_size / 2;
            let h = (cell_size as f64 * 0.8) as u32;

            renderer.triangle::<2>(color, (cell_size - w, 0), (w, h));
        }
        2 => {
            let w = cell_size / 3;
            let dw = cell_size - w;

            renderer.rectangle(color, (w, w), (dw, dw));
        }
        3 => {
            let inner = cell_size as f64 / 10.0;
            // "Use fixed outer border widths in small icons to ensure the border is drawn"
            // https://github.com/dmester/jdenticon/blob/master/src/renderer/shapes.js#L41
            let outer = if cell_size < 6 {
                1
            } else if cell_size < 8 {
                2
            } else {
                cell_size / 4
            };

            let inner = if inner > 1.0 { inner as u32 } else { 1 };
            let p = cell_size - inner - outer;

            renderer.rectangle(color, (outer, outer), (p, p));
        }
        4 => {
            let m = (cell_size as f64 * 0.15) as u32;
            let w = cell_size / 2;
            let p = cell_size - w - m;

            renderer.circle(color, (p, p), w);
        }
        5 => {
            let inner = cell_size / 10;
            let outer = (cell_size as f64 * 0.4) as u32;

            renderer
                .rectangle(color, (0, 0), (cell_size, cell_size))
                .polygon(
                    background_color,
                    [
                        (outer, outer),
                        (cell_size - inner, outer),
                        (outer + (cell_size - outer - inner) / 2, cell_size - inner),
                    ],
                );
        }
        6 => {
            let tenth = cell_size / 10;
            let four_tenths = tenth * 4;
            let seven_tenths = tenth * 7;

            renderer.polygon(
                color,
                [
                    (0, 0),
                    (cell_size, 0),
                    (cell_size, seven_tenths),
                    (four_tenths, four_tenths),
                    (seven_tenths, cell_size),
                    (0, cell_size),
                ],
            );
        }
        7 | 11 => {
            let half_cell = cell_size / 2;
            let diff = cell_size - half_cell;
            renderer.triangle::<3>(color, (half_cell, half_cell), (diff, diff));
        }
        8 => {
            let half_cell = cell_size / 2;
            let diff = cell_size - half_cell;

            renderer
                .rectangle(color, (0, 0), (cell_size, diff))
                .rectangle(color, (0, half_cell), (diff, diff))
                .triangle::<1>(color, (half_cell, half_cell), (diff, diff));
        }
        9 => {
            let inner = (cell_size as f64 * 0.14) as u32;
            let outer = if cell_size < 4 {
                1
            } else if cell_size < 6 {
                2
            } else {
                (cell_size as f64 * 0.35) as u32
            };

            let p = cell_size - outer - inner;
            renderer
                .rectangle(color, (0, 0), (cell_size, cell_size))
                .rectangle(background_color, (outer, outer), (p, p));
        }
        10 => {
            let inner = cell_size as f64 * 0.12;
            let outer = (inner * 3.0) as u32;
            let inner = inner as u32;

            renderer
                .rectangle(color, (0, 0), (cell_size, cell_size))
                .circle(background_color, (outer, outer), cell_size - inner - outer);
        }
        12 => {
            let m = cell_size / 4;
            let p = cell_size - m;

            renderer
                .rectangle(color, (0, 0), (cell_size, cell_size))
                .rectangle(background_color, (m, m), (p, p));
        }
        13 if position_index == 0 => {
            let fcell = cell_size as f64;
            let m = (fcell * 0.4) as u32;
            let w = (fcell * 1.2) as u32;

            renderer.circle(color, (m, m), w);
        }
        _ => (),
    }
}

/// Renders an identicon for the given hash. The hash is strictly 20-bytes long. If your hash is
/// shorter, you should pad it. Similarly, if your hash is longer, you should truncate it.
///
/// # Returns
/// A ril [`Image`] with the identicon rendered on it. See [`Image::save_inferred`] to save the
/// image to a file, and similarly [`Image::encode`] to encode the image to a buffer in memory.
///
/// Saving identicons to different encodings require different features to be enabled. By default,
/// rdenticon enables the `ril/png` feature. If, for example, I wanted to save identicons as JPEGs,
/// I would enable the `ril/jpeg` feature. See the [`ril`] crate for more information on features.
pub fn render_identicon(hash: [u8; 20], config: &Config) -> Image<Rgba> {
    const SIDE_POSITIONS: [(u32, u32); 8] = [
        (1, 0),
        (2, 0),
        (2, 3),
        (1, 3),
        (0, 1),
        (3, 1),
        (3, 2),
        (0, 2),
    ];
    const CORNER_POSITIONS: [(u32, u32); 4] = [(0, 0), (3, 0), (3, 3), (0, 3)];
    const CENTER_POSITIONS: [(u32, u32); 4] = [(1, 1), (2, 1), (2, 2), (1, 2)];

    let mut image = Image::new(config.size, config.size, config.background_color);

    let padding = (config.padding * config.size as f64).round() as u32;
    let size = config.size - padding * 2;

    let cell = size / 4;
    let offset = padding + size / 2 - cell * 2;

    let hash = into_nibbles(hash);
    let hue = 360.0 * hash_substring_u32::<7>(&hash, 33) as f64 / 0xfffffff as f64;
    let color_candidates = config.color_candidates(hue);

    let mut selected_indices = [!0; 3];
    // `.contains` optimization
    macro_rules! contains_opt {
        ($value:literal) => {{
            selected_indices[0] == $value
                || selected_indices[1] == $value
                || selected_indices[2] == $value
        }};
    }

    for i in 0..3 {
        let index = hash[i + 8] % 5;
        let index = match index {
            0 | 4 if contains_opt!(0) || contains_opt!(4) => 1,
            2 | 3 if contains_opt!(2) || contains_opt!(3) => 1,
            _ => index,
        };

        selected_indices[i] = index;
    }

    let [side_color, corner_color, center_color] = selected_indices;
    let (side_color, corner_color, center_color) = (
        color_candidates.get_from_rotation_index(side_color as usize),
        color_candidates.get_from_rotation_index(corner_color as usize),
        color_candidates.get_from_rotation_index(center_color as usize),
    );

    let mut renderer = ShapeRenderer::new(&mut image);
    macro_rules! render {
        (
            $shape_index:literal,
            $rotation_index:expr,
            $color:ident,
            $render_fn:ident,
            $render_positions:ident
        ) => {
            render_shape(
                &hash,
                $shape_index,
                $rotation_index,
                &mut renderer,
                $color,
                config.background_color,
                offset,
                cell,
                $render_fn,
                $render_positions,
            );
        };
    }

    render!(2, Some(3), side_color, render_outer, SIDE_POSITIONS);
    render!(4, Some(5), corner_color, render_outer, CORNER_POSITIONS);
    render!(1, None, center_color, render_center, CENTER_POSITIONS);

    image
}

/// Generates an identicon for the given message. The message can be something like a username or a
/// unique key.
///
/// # Note
/// Identicons are hashed with SHA-1, which is not cryptographically secure. If you need a secure
/// hash (or if you simply do not want to use SHA-1), generate a hash of 20 bytes with a separate
/// algorithm and pass those bytes manually to [`render_identicon`].
///
/// # Returns
/// A ril [`Image`] with the identicon rendered on it. See [`Image::save_inferred`] to save the
/// image to a file, and similarly [`Image::encode`] to encode the image to a buffer in memory.
///
/// Saving identicons to different encodings require different features to be enabled. By default,
/// rdenticon enables the `ril/png` feature. If, for example, I wanted to save identicons as JPEGs,
/// I would enable the `ril/jpeg` feature. See the [`ril`] crate for more information on features.
///
/// # Example
/// ```no_run
/// fn main() -> rdenticon::ril::Result<()> {
///     // Build configuration
///     let config = rdenticon::Config::builder()
///         .size(512) // Generate a 512x512 image
///         .padding(0.1) // Add a 10% padding
///         .background_color(rdenticon::Rgba::transparent()) // Make the background transparent
///         .build()
///         .expect("invalid config");
///
///     // Render the identicon
///     let image = rdenticon::generate_identicon("super-cool-username", &config);
///
///     // Save the identicon to a file
///     image.save_inferred("identicon.png")?;
///
///     // OR: Save the identicon to memory
///     let mut out = Vec::new();
///     image.encode(rdenticon::ImageFormat::Png, &mut out)?;
///
///     Ok(())
/// }
/// ```
pub fn generate_identicon(message: impl AsRef<str>, config: &Config) -> Image<Rgba> {
    let hash = sha1_smol::Sha1::from(message.as_ref()).digest().bytes();
    render_identicon(hash, config)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_rdenticon() -> ril::Result<()> {
        // Build configuration
        let config = Config::builder()
            .size(512) // Generate a 512x512 image
            .padding(0.1) // Add a 10% padding
            .background_color(Rgba::white())
            .build()
            .expect("invalid config");

        let image = generate_identicon("sample", &config);
        image.save_inferred("identicon.png")
    }
}