A lightweight library in pure Rust to get raw data of Ethereum-style blocky identicon, 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.
Supports general Rust bin/lib, and WebAssembly (wasm) target.
Cargo.toml
of a Rust crate
toml
[dependencies]
eth-blockies = "1.1"
...or without compressed_png
default crate feature:
toml
[dependencies]
eth-blockies = { version = "1.1", default-features = false }
Define a blockies type (size) to use
Blockies
```rust
use eth_blockies::{Blockies, BlockiesGenerator};// Blockies < size (const), T >
type Icon
EthBlockies
is predefined as follows
rust
// use statement for Ethereum address blockies
use eth_blockies::{EthBlockies, SeedInput, BlockiesGenerator};
// type 'EthBlockies<T>' is predefined as 'Blockies<8, T>'
<h2>
Select an input seed type
Check for SeedInput
to get full list of input seed types
rust
// 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
```rust
// generate Ethereum address blockies from various input type
let seedfromstr = "0xe686c14FF9C11038F2B1c9aD617F2346CFB817dC" .toethaddrseed(); let fromstr = EthBlockies::data(seedfrom_str);
let seedfrombytes = [ 0xe6, 0x86, 0xc1, 0x4f, 0xf9, 0xc1, 0x10, 0x38, 0xf2, 0xb1, 0xc9, 0xad, 0x61, 0x7f, 0x23, 0x46, 0xcf, 0xb8, 0x17, 0xdc, ].toethaddrseed(); let frombytes = EthBlockies::data(seedfrom_bytes);
Select an output data type
BlockiesGenerator
to get full list of output data types
rust
// 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));
Generate an Ethereum address blockies (with to_ethaddr_seed()
)
```rust use eth_blockies::{EthBlockies, SeedInput, BlockiesGenerator};
let seed = "0xe686c14FF9C11038F2B1c9aD617F2346CFB817dC" .toethaddrseed(); // required for Ethereum address blockies
// 8x8 2D-array of (r, g, b) { let ethblockiesfrom_addr = EthBlockies::data(&seed); }
// uncompressed png data in byte vector { let ethblockiespngfromaddr = EthBlockies::pngdata(&seed, (128, 128)); // use below for compressed png // EthBlockies::compressedpng_data(&seed, (128, 128));
// write data as png file use std::io::Write; std::fs::File::create("eth-blockies.png").unwrap() .writeall(ðblockiespngfrom_addr); } ```
Generate an html img
blockies element, on wasm target
```rust
// addr to blockies data uri scheme,
// which can be used directly in img elem 'src' or css 'url()'
fn ethblockiesdatauri(addr: &str) -> String {
use ethblockies::{EthBlockies, SeedInput, BlockiesGenerator};
let addrinput = addr.toethaddrseed(); let outputdim = (128, 128); let dataurioutput = true;
EthBlockies::pngdatabase64( addrinput, outputdim, dataurioutput) }
use web_sys::*;
let addr = "0xe686c14FF9C11038F2B1c9aD617F2346CFB817dC";
window() .andthen(|w| w.document()) .andthen(|doc| doc.body().zip(doc.createelement("img").ok())) .andthen(|(body, img)| { // create a new img html element with generated datauri img.setattribute("src", ðblockiesdatauri(addr)) // then attach to body .andthen(|| body.appendchild(&img)) .ok() }); ```
compressed_png
(Enabled by default)
compressed_png_data()
compressed_png_data_base64()
deflate
cratecargo add eth-blockies@1.1 --no-default-features
eth-blockies = { version = "1.1", default-features = false }
console
$ cargo install eth-blockies
```text
usage: eth-blockies
[output-fmt] - ansi (Default) Generate ansi sequence of blockies, usually for printing to terminal - image Generate png image data of blockies
[OPTIONS...]:
-e --ethseed Interpret seed string as Ethereum address,
and canonicalize seed (to lowercase + set '0x' prefix)
to get Ethereum blockies correctly
-a --ascii (only for 'ansi' mode) Get non-compact, big blockies
with ascii (non-unicode)
-r --raw (only for 'image' mode) Get uncompressed, raw png image
-s --size=<BLOCKIES_SIZE>
Blockies size: # of elems per side (1-32) (Default: '8')
-d --dimension=<WIDTH>x<HEIGHT>
Dimensions of output in the form of '(width)x(height)'
If not given, following is used (Default):
- ('ansi' mode) '(blockies_size)x(blockies_size)'
- ('image' mode) '128x128'
-o --outfile=<FILENAME>
File name to write output
If the parameter is not given, stdout is used (Default)
examples:
Outputs from following commands are all the same: $ eth-blockies 0xe686c14ff9c11038f2b1c9ad617f2346cfb817dc $ eth-blockies e686c14FF9C11038F2B1c9aD617F2346CFB817dC -e
Outputs from following commands are all the same: $ eth-blockies "genericseednotethaddr" --size=15 $ eth-blockies "genericseednotethaddr" ansi --size=15 --dimension=15x15 $ eth-blockies "genericseednot_ethaddr" a -s 15 -d 15x15
Outputs from following commands are all the same: $ eth-blockies "genericseed" image > blockies.png $ eth-blockies "genericseed" i -d128x128 -oblockies.png $ eth-blockies "generic_seed" i -d 128x128 -o blockies.png ```
Kim Hwiwon \ The MIT License (MIT)License