Crate eth_blockies
source ·Expand description
A lightweight library in pure Rust to get Ethereum-style blocky identicon data, which can be used for generating blockies icon images, printing to terminal, etc.
Useful when getting raw RGB data of Ethereum-style blockies, as well as complete png image files.
Basic Usage
-
Define a blockies type (size) to use
- Make an alias type of
Blockies
use eth_blockies::{Blockies, BlockiesGenerator}; // Blockies < size (const), T > type Icon<T> = Blockies<15, T>;
- Cf) For Ethereum address blockies,
EthBlockies
is predefined as follows
// use statement for Ethereum address blockies use eth_blockies::{EthBlockies, SeedInput, BlockiesGenerator}; // type 'EthBlockies<T>' is predefined as 'Blockies<8, T>'
- Make an alias type of
-
Select an input seed type
- Check for
SeedInput
to get full list of input seed types
// generate blockies from various input type let from_string = Icon::data("eth-blockies".to_string()); let from_byte_vec = Icon::data(vec![0x0c, 0x93, 0xa3, 0x2e]);
- Cf) For Ethereum address seeds, apply
to_ethaddr_seed()
before passing as input seed
// generate Ethereum address blockies from various input type let seed_from_str = "0xe686c14FF9C11038F2B1c9aD617F2346CFB817dC" .to_ethaddr_seed(); let from_str = EthBlockies::data(seed_from_str); let seed_from_bytes = [ 0xe6, 0x86, 0xc1, 0x4f, 0xf9, 0xc1, 0x10, 0x38, 0xf2, 0xb1, 0xc9, 0xad, 0x61, 0x7f, 0x23, 0x46, 0xcf, 0xb8, 0x17, 0xdc, ].to_ethaddr_seed(); let from_bytes = EthBlockies::data(seed_from_bytes);
- Check for
-
Select an output data type
- Check for
BlockiesGenerator
to get full list of output data types
// generate blockies in various forms let in_rgb_2d_arr = Icon::data("eth-blockies"); let in_indexed_2d_arr = Icon::indexed_data("eth-blockies"); let in_gray_2d_arr = Icon::data_mapped("eth-blockies", to_gray); let in_png_data_vec = Icon::png_data("eth-blockies", (128, 128));
- Check for
Example
-
Generate an Ethereum address blockies (with
to_ethaddr_seed()
)use eth_blockies::{EthBlockies, SeedInput, BlockiesGenerator}; let seed = "0xe686c14FF9C11038F2B1c9aD617F2346CFB817dC" .to_ethaddr_seed(); // required for Ethereum address blockies // 8x8 2D-array of (r, g, b) { let eth_blockies_from_addr = EthBlockies::data(&seed); } // uncompressed png data in byte vector { let eth_blockies_png_from_addr = EthBlockies::png_data(&seed, (128, 128)); // use below for compressed png // EthBlockies::compressed_png_data(&seed, (128, 128)); // write data as png file use std::io::Write; std::fs::File::create("eth-blockies.png").unwrap() .write_all(ð_blockies_png_from_addr); }
-
Generate an html
img
blockies element, on wasm targetⓘ// addr to blockies data uri scheme, // which can be used directly in img elem 'src' or css 'url()' fn eth_blockies_data_uri(addr: &str) -> String { use eth_blockies::{EthBlockies, SeedInput, BlockiesGenerator}; let addr_input = addr.to_ethaddr_seed(); let output_dim = (128, 128); let data_uri_output = true; EthBlockies::png_data_base64( addr_input, output_dim, data_uri_output) } use web_sys::*; let addr = "0xe686c14FF9C11038F2B1c9aD617F2346CFB817dC"; window() .and_then(|w| w.document()) .and_then(|doc| doc.body().zip(doc.create_element("img").ok())) .and_then(|(body, img)| { // create a new img html element with generated data_uri img.set_attribute("src", ð_blockies_data_uri(addr)) // then attach to body .and_then(|_| body.append_child(&img)) .ok() });
Cargo Features
compressed_png
(Enabled by default)- This feature enables following functions:
- This feature adds a following external dependency:
deflate
crate
- If png compression is not needed,
disable this feature as follows when adding the crate:
- E.g.
- Shell:
cargo add eth-blockies@1.1 --no-default-features
- Cargo.toml:
eth-blockies = { version = "1.1", default-features = false }
- Shell:
- E.g.
Enums
- Type of colors in Ethereum-style blockies
Traits
- Trait for generating a new
Blockies
- Additional helper functions for generated
Blockies
objects - Available types of input seed
Type Definitions
- Ethereum-style blockies data of type
T
- Array map of elements
T
, composing Ethereum-style blockies - Unit RGB pixel data