Skip to content

Commit e6273ce

Browse files
Merge pull request #8 from bchainhub/update/fix-01
Fixes
2 parents 41b77c0 + 3b66f30 commit e6273ce

File tree

6 files changed

+89
-78
lines changed

6 files changed

+89
-78
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@blockchainhub/blo",
3-
"version": "1.2.5",
3+
"version": "1.2.6",
44
"license": "CORE",
55
"author": "Pierre Bertet <[email protected]>",
66
"contributors": [
@@ -44,9 +44,9 @@
4444
"devDependencies": {
4545
"globals": "^15.14.0",
4646
"typescript": "^5.7.3",
47-
"vite": "^6.0.7",
47+
"vite": "^6.1.0",
4848
"vite-plugin-dts": "^4.5.0",
49-
"esbuild": "^0.24.2"
49+
"esbuild": "^0.25.0"
5050
},
5151
"engines": {
5252
"node": ">=16"

src/image.ts

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,44 +8,44 @@ const SATURATION_RANGE = 60;
88
const LIGHTNESS_DIVISOR = 25;
99

1010
export function image(address: Address, options: BloOptions = {}): BloImage {
11-
const { seed } = options;
12-
const random = seedRandom(seed || address.toLowerCase());
13-
const palette = randomPalette(random);
14-
const data = randomImageData(random);
15-
return [data, palette];
11+
const { seed } = options;
12+
const random = seedRandom(seed || address.toLowerCase());
13+
const palette = randomPalette(random);
14+
const data = randomImageData(random);
15+
return [data, palette];
1616
}
1717

1818
export function randomImageData(random: () => number): Uint8Array {
19-
// Use a single allocation
20-
const data = new Uint8Array(32);
21-
22-
// Unroll the loop for better performance
23-
for (let i = 0; i < 32; i += 4) {
24-
data[i] = Math.floor(random() * COLOR_THRESHOLD);
25-
data[i + 1] = Math.floor(random() * COLOR_THRESHOLD);
26-
data[i + 2] = Math.floor(random() * COLOR_THRESHOLD);
27-
data[i + 3] = Math.floor(random() * COLOR_THRESHOLD);
28-
}
29-
30-
return data;
19+
// Use a single allocation
20+
const data = new Uint8Array(32);
21+
22+
// Unroll the loop for better performance
23+
for (let i = 0; i < 32; i += 4) {
24+
data[i] = Math.floor(random() * COLOR_THRESHOLD);
25+
data[i + 1] = Math.floor(random() * COLOR_THRESHOLD);
26+
data[i + 2] = Math.floor(random() * COLOR_THRESHOLD);
27+
data[i + 3] = Math.floor(random() * COLOR_THRESHOLD);
28+
}
29+
30+
return data;
3131
}
3232

3333
export function randomPalette(random: () => number): Palette {
34-
return {
35-
primary: randomColor(random),
36-
background: randomColor(random),
37-
accent: randomColor(random)
38-
};
34+
return {
35+
primary: randomColor(random),
36+
background: randomColor(random),
37+
accent: randomColor(random)
38+
};
3939
}
4040

4141
export function randomColor(rand: () => number): Hsl {
42-
// Pre-allocate the array
43-
const color = new Uint16Array(3);
42+
// Pre-allocate the array
43+
const color = new Uint16Array(3);
4444

45-
// Optimize calculations
46-
color[0] = rand() * 360;
47-
color[1] = SATURATION_BASE + (rand() * SATURATION_RANGE);
48-
color[2] = (rand() + rand() + rand() + rand()) * LIGHTNESS_DIVISOR;
45+
// Optimize calculations
46+
color[0] = rand() * 360;
47+
color[1] = SATURATION_BASE + (rand() * SATURATION_RANGE);
48+
color[2] = (rand() + rand() + rand() + rand()) * LIGHTNESS_DIVISOR;
4949

50-
return color;
50+
return color;
5151
}

src/index.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
import type { Address, BloOptions } from "./types";
2-
import { image } from "./image";
2+
import { image, randomImageData, randomPalette, randomColor } from "./image";
33
import { svg } from "./svg";
4+
import { seedRandom } from "./random";
45

56
export type {
6-
Address,
7-
BloImage,
8-
BloOptions,
9-
Hsl,
10-
Palette,
11-
PaletteIndex,
7+
Address,
8+
BloImage,
9+
BloOptions,
10+
Hsl,
11+
Palette,
12+
PaletteIndex,
1213
} from "./types";
1314

1415
const defaultOpts: BloOptions = { size: 64 };
1516
const mergeOpts = (opts: BloOptions = {}) => ({ ...defaultOpts, ...opts });
1617

1718
export const blo = (a: Address, o: BloOptions = {}) =>
18-
"data:image/svg+xml;base64," + btoa(bloSvg(a, o));
19+
"data:image/svg+xml;base64," + btoa(bloSvg(a, o));
1920

2021
export const bloSvg = (a: Address, o: BloOptions = {}) =>
21-
svg(a, mergeOpts(o));
22+
svg(a, mergeOpts(o));
2223

2324
export const bloImage = (a: Address, o: BloOptions = {}) =>
24-
image(a, mergeOpts(o));
25+
image(a, mergeOpts(o));
26+
27+
export { seedRandom, randomImageData, randomPalette, randomColor };

src/random.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
export const seedRandom = (s: string) => {
2-
const a = new Array(4).fill(0)
3-
for (let i = 0; i < s.length; i++) {
4-
a[i%4] = ((a[i%4] << 5) - a[i%4]) + s.charCodeAt(i)
5-
}
6-
return () => {
7-
const t = a[0] ^ (a[0] << 11)
8-
a[0] = a[1]
9-
a[1] = a[2]
10-
a[2] = a[3]
11-
a[3] = (a[3] ^ (a[3] >> 19) ^ t ^ (t >> 8))
12-
return (a[3]>>>0) / ((1 << 31)>>>0)
13-
}
2+
const a = new Array(4).fill(0)
3+
for (let i = 0; i < s.length; i++) {
4+
a[i%4] = ((a[i%4] << 5) - a[i%4]) + s.charCodeAt(i)
5+
}
6+
return () => {
7+
const t = a[0] ^ (a[0] << 11)
8+
a[0] = a[1]
9+
a[1] = a[2]
10+
a[2] = a[3]
11+
a[3] = (a[3] ^ (a[3] >> 19) ^ t ^ (t >> 8))
12+
return (a[3]>>>0) / ((1 << 31)>>>0)
13+
}
1414
}

src/svg.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@ import { randomPalette } from "./image";
44
import { seedRandom } from "./random";
55

66
export const svg = (a: Address, o: BloOptions): string => {
7-
const r = seedRandom(o.seed || a);
8-
const { background: b, primary: p, accent: c } = randomPalette(r);
9-
const s = o.size || 64;
10-
const scale = s / 8;
11-
const d = new Array(64);
7+
const r = seedRandom(o.seed || a);
8+
const { background: b, primary: p, accent: c } = randomPalette(r);
9+
const s = o.size || 64;
10+
const scale = s / 8;
11+
const d = new Array(64);
1212

13-
for (let i = 0; i < 32; i++) {
14-
const x = i % 4, y = i / 4 | 0;
15-
const v = Math.floor(r() * 2.3);
16-
d[y * 8 + x] = v;
17-
d[y * 8 + (7 - x)] = v;
18-
}
13+
for (let i = 0; i < 32; i++) {
14+
const x = i % 4, y = i / 4 | 0;
15+
const v = Math.floor(r() * 2.3);
16+
d[y * 8 + x] = v;
17+
d[y * 8 + (7 - x)] = v;
18+
}
1919

20-
return `<svg xmlns="http://www.w3.org/2000/svg" width="${s}" height="${s}" viewBox="0 0 ${s} ${s}">
20+
return `<svg xmlns="http://www.w3.org/2000/svg" width="${s}" height="${s}" viewBox="0 0 ${s} ${s}">
2121
<rect width="${s}" height="${s}" fill="hsl(${b[0]},${b[1]}%,${b[2]}%)"/>
2222
<g fill="hsl(${p[0]},${p[1]}%,${p[2]}%)">
2323
${d.map((v,i)=>v===1?`<rect width="${scale}" height="${scale}" x="${(i%8)*scale}" y="${(i/8|0)*scale}"/>`:'').join('')}

src/types.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,22 @@ export type Address = string;
33

44
// 4x8 grid of the image left side, as 32 PaletteIndex items.
55
// The right side is omitted as it's a mirror of the left side.
6-
export type BloImage = [Uint8Array, Palette];
6+
export type BloImage = [BloImageData, Palette];
7+
8+
export type BloImageData = Uint8Array;
79

810
// Colors used by a given icon.
911
export type Palette = {
10-
background: Hsl;
11-
primary: Hsl;
12-
accent: Hsl;
12+
background: Hsl;
13+
primary: Hsl;
14+
accent: Hsl;
1315
};
1416

1517
// Or using const for better type inference
1618
export const PaletteIndexes = {
17-
BACKGROUND: 0,
18-
PRIMARY: 1,
19-
ACCENT: 2,
19+
BACKGROUND: 0,
20+
PRIMARY: 1,
21+
ACCENT: 2,
2022
} as const;
2123
export type PaletteIndex = 0 | 1 | 2;
2224

@@ -37,12 +39,18 @@ export type BloSvgFunction = (address: Address, options: BloOptions) => string;
3739
export type BloImageFunction = (address: Address, options: BloOptions) => BloImage;
3840

3941
export type HslValues = {
40-
hue: number; // 0-360
41-
saturation: number; // 0-100
42-
lightness: number; // 0-100
42+
hue: number; // 0-360
43+
saturation: number; // 0-100
44+
lightness: number; // 0-100
4345
};
4446

4547
export interface BloOptions {
46-
size?: number;
47-
seed?: string;
48+
size?: number;
49+
seed?: string;
4850
}
51+
52+
// Add function types
53+
export type SeedRandom = (s: string) => () => number;
54+
export type RandomColor = (r: () => number) => Hsl;
55+
export type RandomPalette = (r: () => number) => Palette;
56+
export type ImageFunction = (a: Address, o: BloOptions) => BloImage;

0 commit comments

Comments
 (0)