3个版本 (稳定)
1.1.0 | 2023年7月2日 |
---|---|
1.0.0 | 2022年6月28日 |
0.9.2 | 2022年5月10日 |
0.9.1 |
|
0.9.0 |
|
#774 in 魔法豆
2,589 每月下载量
用于 pawprint
135KB
1.5K SLoC
eth-blockies-rs
一个纯Rust轻量级库,用于获取以太坊风格方块头像的原始数据,可用于生成方块头像图标图像、打印到终端等。
在获取以太坊风格方块头像的原始RGB数据以及完整的png图像文件时非常有用。
支持一般的Rust bin/lib和WebAssembly (wasm) 目标。
实时演示
- 查看wasm演示 (以太坊风格方块头像生成器)以生成您定制的方块头像。
库
文档
- 有关详细信息,请查看docs.rs上的完整库文档。
先决条件
- 将依赖关系添加到Rust crate的
Cargo.toml
...或无需[dependencies] eth-blockies = "1.1"
compressed_png
默认crate功能[dependencies] eth-blockies = { version = "1.1", default-features = false }
库基本用法
-
定义要使用的方块头像类型(大小)
- 为
Blockies
创建别名类型
use eth_blockies::{Blockies, BlockiesGenerator}; // Blockies < size (const), T > type Icon<T> = Blockies<15, T>;
- 注:对于以太坊地址方块头像,
EthBlockies
已预定义如下
// use statement for Ethereum address blockies use eth_blockies::{EthBlockies, SeedInput, BlockiesGenerator}; // type 'EthBlockies<T>' is predefined as 'Blockies<8, T>'
- 为
-
选择输入种子类型
- 查看
SeedInput
以获取输入种子类型的完整列表
// 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]);
- 注意:对于以太坊地址种子,在作为输入种子传递之前,请先应用
to_ethaddr_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);
- 查看
-
选择输出数据类型
- 查看
BlockiesGenerator
获取完整的输出数据类型列表
// 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));
- 查看
示例
-
生成以太坊地址blockies(使用
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); }
-
在wasm目标上生成html
img
blockies元素// 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功能
compressed_png
(默认启用)- 此功能启用以下功能
- 此功能添加以下外部依赖
deflate
crate
- 如果不需要png压缩,请在添加crate时禁用此功能,如下所示
- 例如
- Shell:
cargo add [email protected] --no-default-features
- Cargo.toml:
eth-blockies = { version = "1.1", default-features = false }
- Shell:
- 例如
二进制文件
安装
$ cargo install eth-blockies
二进制文件使用
usage: eth-blockies <seed> [output-fmt (ansi|image)] [OPTIONS...]
<seed> Seed to generate blockies (e.g. Ethereum wallet address)
[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 "generic_seed_not_ethaddr" --size=15
$ eth-blockies "generic_seed_not_ethaddr" ansi --size=15 --dimension=15x15
$ eth-blockies "generic_seed_not_ethaddr" a -s 15 -d 15x15
- Outputs from following commands are all the same:
$ eth-blockies "generic_seed" image > blockies.png
$ eth-blockies "generic_seed" i -d128x128 -oblockies.png
$ eth-blockies "generic_seed" i -d 128x128 -o blockies.png
作者
Kim Hwiwon <[email protected]>
许可
MIT许可(MIT)
依赖项
~62KB