A lightweight library in pure Rust to get Ethereum blockies raw data (or png images), which can be used for creating blockies icon images, printing to terminal, etc.
Supports wide range of targets including Rust bin/lib, and WebAssembly (wasm).
Cargo.toml
toml
[dependencies]
eth-blockies = "0.9.0"
let addr = "0xe686c14FF9C11038F2B1c9aD617F2346CFB817dC" .addr_canonicalize();
// get 2D array of (r, g, b) { let blockiesdatargb = ethblockiesdata(&addr); }
// get 2D array of grayscale { fn rgbtograyscale((r, g, b): RgbPixel) -> u8 { (r as f64 * 0.299 + g as f64 * 0.587 + b as f64 * 0.114) as u8 }
let blockies_data_grayscale = eth_blockies_data_mapped(&addr, rgb_to_grayscale);
}
// get (color palette, palette index of each pixel) { let (colorpalette, paletteidxbitmap) = ethblockiesindexeddata(&addr); } ```
text.png
, on Rust binary/library target
```rust
use eth_blockies::*;let addr = "0xe686c14FF9C11038F2B1c9aD617F2346CFB817dC" .addrcanonicalize(); let dimension = (128, 128); let imgpngdata = ethblockiespngdata(addr, dimension);
use std::io::Write; std::fs::File::create("test.png").unwrap().writeall(&imgpng_data); ```
Generate a html img
element of a generated blockies, on wasm target
```rust
// addr to blockies datauri,
// which can be used directly in img elem attribute 'src' or css 'url()'
fn ethblockiesdatauri(addr: &str) -> Option
let imgdatabase64 = ethblockiespngdatabase64(addr.addr_canonicalize(), (8, 8));
String::fromutf8(imgdatabase64) .map(|data| "data:image/png;base64,".toowned() + &data) .ok() }
use web_sys::*;
let addr = "0xe686c14FF9C11038F2B1c9aD617F2346CFB817dC";
window() .andthen(|w| w.document()) .andthen(|doc| doc.body().zip(doc.createelement("img").ok())) .andthen(|(body, img)| { // set data uri to img src ethblockiesdatauri(addr) .andthen(|datauri| img.setattribute("src", &data_uri).ok());
img.set_attribute(
"style",
concat!(
"image-rendering: pixelated !important; ", // no blur on scaling
"width: 120px; height: 120px;",
),
);
body.append_child(&img).ok()
});
```
Kim Hwiwon \ The MIT License (MIT)License