An open API service indexing awesome lists of open source software.

https://github.com/rustq/colorid-wasm

The unique 4-colors-ID string generator in WASM
https://github.com/rustq/colorid-wasm

javascript nodejs

Last synced: 4 months ago
JSON representation

The unique 4-colors-ID string generator in WASM

Awesome Lists containing this project

README

          

# colorid-wasm

[![license](https://img.shields.io/npm/l/colorid-wasm?color=red)](https://revolunet.mit-license.org/) [![npm](https://img.shields.io/npm/v/colorid-wasm?color=cyan)](https://www.npmjs.com/package/colorid-wasm) [![downloads](https://img.shields.io/npm/dm/colorid-wasm)](https://www.npmjs.com/package/colorid-wasm)

Color as Identify - The unique 4-colors-ID string generator in `WASM`

The performance of `ColorID` is better than `UUID` and `NanoID` (The algorithm of `ColorID` is actually very similar to the `UUID.V4`)

If we use `ColorID` to represent identities in social networks, we can easily generate personalized social information for users, such as avatars, NFTs, etc.

The `ColorID` is consisted by 4 colors in the RGB channel from 12 unsigned 8 bits numbers (`u8`), so the theoretical total of `ColorID` is `(2^8)^12` = `2^96` ≈ `7*10^28`, which means that even if the `ColorID` is generated for every 7.8 billion people in the world every second, it will take 300 billion years to use up all `ColorIDs`. And `ColorID` provides safety by using hardware random generator, can also be used in clusters.

Because the Four color theorem, if we need to color the regions of any avatars or NFTs so that no two adjacent regions have the same color, four colors are enough!

一款基于 4 种颜色来表示身份的 `WASM` 随机 ID 生成器。

在性能表现上 `ColorID` 比 `UUID`、`NanoID` 等库表现更好 (虽然 `ColorID` 的生成算法实际上和 `UUID.V4` 非常相似)。

如果我们用 `ColorID` 表示社交网络中的身份,可以轻松地为用户生成个性化的社交信息:如头像、数字藏品等。

实际上 `ColorID` 是由 4 种颜色在 RGB 通道内的 12 个 8 位无符号整型数字 (`u8`) 组成,故 `ColorID` 理论上的总数为 `(2^8)^12` = `2^96` ≈ `7*10^28`,也就是说即使每秒都为全球 78 亿人中的每个人都生成一个 `ColorID`,也要花 3000 亿年才会将所有 `ColorID` 用完。此外 `ColorID` 使用强随机函数来生成随机数,故也可在集群使用中提供安全性。

因为四色定理的原因,如果我们想要为头像或者数字藏品涂色且不会出现邻接区域颜色相同, 有四种颜色就足够了!

## Install

```shell
$ npm i colorid-wasm
```

## Usage

`in module`

```js
import * as wasm from "colorid-wasm";

await wasm.default();

wasm.colorid(); // #5B34F9-#34F9DF-#F9DF4E-#DF4EB5
```

`in React`

```js
React.useEffect(() => {
import("colorid-wasm").then(async wasm => {

await wasm.default();

wasm.colorid(); // #631707-#4D4E40-#5BBD69-#4FC6B4

})
}, []);
```

## Performance

`wasm benchmark`

```rust
uuid v4 68.29999995231628 ms
colorid-wasm 73.70000004768372 ms
crypto.randomUUID 78.20000004768372 ms
nanoid 123.90000009536743 ms
```

## Devlopment

```shell
$ cd colorid

$ cargo test
$ cargo build

$ cd ../
```

```shell
$ cd colorid-wasm

$ wasm-pack build --release --target web
```

## Benchmark

```shell
$ npm i
$ npm run http-server
```

```shell
$ npm run cypress:open
```

## License

[MIT](https://opensource.org/licenses/MIT)