@iconoma-icons/core 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 0xnocap
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,103 @@
1
+ # @iconoma-icons/core
2
+
3
+ Core engine for [Iconoma](https://github.com/0xnocap/iconoma) — deterministic SVG icon generation from any string.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ npm install @iconoma-icons/core
9
+ ```
10
+
11
+ You'll also want `@iconoma-icons/collection` for the built-in styles, unless you're writing your own.
12
+
13
+ ## API
14
+
15
+ ### `createIcon(style, options?)`
16
+
17
+ Generates a deterministic SVG icon from a style function and options.
18
+
19
+ ```js
20
+ import { createIcon } from '@iconoma-icons/core';
21
+ import { pixel } from '@iconoma-icons/collection';
22
+
23
+ const icon = createIcon(pixel, {
24
+ seed: 'user@example.com',
25
+ size: 128,
26
+ colors: ['#FF006E', '#8338EC', '#3A86FF', '#FFBE0B', '#FB5607'],
27
+ isCircle: false,
28
+ borderRadius: 0,
29
+ });
30
+
31
+ icon.toString(); // '<svg xmlns="http://www.w3.org/2000/svg" ...>...</svg>'
32
+ icon.toDataUri(); // 'data:image/svg+xml;charset=utf-8,%3Csvg...'
33
+ ```
34
+
35
+ **Returns:** `Result` — an object with two methods:
36
+
37
+ | Method | Returns | Description |
38
+ |--------|---------|-------------|
39
+ | `toString()` | `string` | Raw SVG markup. Use with `innerHTML`, `fs.writeFileSync()`, etc. |
40
+ | `toDataUri()` | `string` | Data URI string. Use with `<img src="...">` or CSS `background-image`. |
41
+
42
+ **Options:**
43
+
44
+ | Option | Type | Default | Description |
45
+ |--------|------|---------|-------------|
46
+ | `seed` | `string` | random | Any string. Determines the icon appearance. Same seed = same icon. |
47
+ | `size` | `number` | `128` | Width & height in pixels. |
48
+ | `colors` | `string[]` | `['#FF006E', '#8338EC', '#3A86FF', '#FFBE0B', '#FB5607']` | Array of hex color strings (e.g. `'#FF006E'`). 3-5 colors recommended. |
49
+ | `isCircle` | `boolean` | `false` | Clip to circle via SVG `<clipPath>`. |
50
+ | `borderRadius` | `number` | `0` | Rounded corner radius in px. Ignored if `isCircle` is true. |
51
+ | `styleOptions` | `Record<string, any>` | style defaults | Style-specific tuning params. See `@iconoma-icons/collection` docs for options per style. |
52
+
53
+ ### `SeededRandom`
54
+
55
+ Deterministic pseudo-random number generator. Used internally and exposed for custom styles.
56
+
57
+ ```ts
58
+ import { SeededRandom } from '@iconoma-icons/core';
59
+
60
+ const rng = new SeededRandom(42);
61
+
62
+ rng.random(); // float in [0, 1)
63
+ rng.randomInt(0, 10); // integer in [0, 10] inclusive
64
+ rng.randomElement(array); // picks one element
65
+ rng.shuffle(array); // Fisher-Yates shuffle, returns the shuffled array
66
+ ```
67
+
68
+ ### `Style` type
69
+
70
+ A style is a function with this signature:
71
+
72
+ ```ts
73
+ type Style = (rng: SeededRandom, options: Options) => string;
74
+ ```
75
+
76
+ It receives a seeded RNG and the resolved options, and returns an **SVG body string** (the markup *inside* the `<svg>` tag — rects, paths, polygons, etc). The `createIcon` function wraps this in a proper SVG element with viewBox, clipping, etc.
77
+
78
+ ### Writing a custom style
79
+
80
+ ```ts
81
+ import { createIcon, SeededRandom } from '@iconoma-icons/core';
82
+ import type { Style, Options } from '@iconoma-icons/core';
83
+
84
+ const myStyle: Style = (rng: SeededRandom, options: Options): string => {
85
+ const size = options.size || 128;
86
+ const colors = options.colors || ['#000', '#fff'];
87
+ const bg = rng.randomElement(colors);
88
+ const fg = rng.randomElement(colors);
89
+
90
+ // Return SVG body markup (everything inside <svg>)
91
+ return `
92
+ <rect width="${size}" height="${size}" fill="${bg}"/>
93
+ <circle cx="${size/2}" cy="${size/2}" r="${size/3}" fill="${fg}"/>
94
+ `;
95
+ };
96
+
97
+ const icon = createIcon(myStyle, { seed: 'hello', size: 256 });
98
+ console.log(icon.toString());
99
+ ```
100
+
101
+ ## License
102
+
103
+ [MIT](../../LICENSE)
@@ -0,0 +1,35 @@
1
+ declare class SeededRandom {
2
+ private seed;
3
+ constructor(seed: number);
4
+ random(): number;
5
+ randomInt(min: number, max: number): number;
6
+ randomElement<T>(array: T[]): T;
7
+ shuffle<T>(array: T[]): T[];
8
+ }
9
+
10
+ interface Options {
11
+ /** Unique string seed — same seed always produces the same icon */
12
+ seed?: string;
13
+ /** Icon size in pixels (default: 128) */
14
+ size?: number;
15
+ /** Color palette to use. Falls back to a built-in default if omitted */
16
+ colors?: string[];
17
+ /** Clip to a circle */
18
+ isCircle?: boolean;
19
+ /** Rounded corner radius (ignored if isCircle is true) */
20
+ borderRadius?: number;
21
+ /** Style-specific parameters (e.g. blockSize, waveCount, etc.) */
22
+ styleOptions?: Record<string, any>;
23
+ }
24
+ interface Result {
25
+ /** Returns the raw SVG markup string */
26
+ toString: () => string;
27
+ /** Returns a data URI suitable for <img src="..."> */
28
+ toDataUri: () => string;
29
+ }
30
+ type Style = {
31
+ (rng: SeededRandom, options: Options): string;
32
+ };
33
+ declare function createIcon(style: Style, options?: Options): Result;
34
+
35
+ export { type Options, type Result, SeededRandom, type Style, createIcon };
@@ -0,0 +1,35 @@
1
+ declare class SeededRandom {
2
+ private seed;
3
+ constructor(seed: number);
4
+ random(): number;
5
+ randomInt(min: number, max: number): number;
6
+ randomElement<T>(array: T[]): T;
7
+ shuffle<T>(array: T[]): T[];
8
+ }
9
+
10
+ interface Options {
11
+ /** Unique string seed — same seed always produces the same icon */
12
+ seed?: string;
13
+ /** Icon size in pixels (default: 128) */
14
+ size?: number;
15
+ /** Color palette to use. Falls back to a built-in default if omitted */
16
+ colors?: string[];
17
+ /** Clip to a circle */
18
+ isCircle?: boolean;
19
+ /** Rounded corner radius (ignored if isCircle is true) */
20
+ borderRadius?: number;
21
+ /** Style-specific parameters (e.g. blockSize, waveCount, etc.) */
22
+ styleOptions?: Record<string, any>;
23
+ }
24
+ interface Result {
25
+ /** Returns the raw SVG markup string */
26
+ toString: () => string;
27
+ /** Returns a data URI suitable for <img src="..."> */
28
+ toDataUri: () => string;
29
+ }
30
+ type Style = {
31
+ (rng: SeededRandom, options: Options): string;
32
+ };
33
+ declare function createIcon(style: Style, options?: Options): Result;
34
+
35
+ export { type Options, type Result, SeededRandom, type Style, createIcon };
package/dist/index.js ADDED
@@ -0,0 +1,103 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ SeededRandom: () => SeededRandom,
24
+ createIcon: () => createIcon
25
+ });
26
+ module.exports = __toCommonJS(index_exports);
27
+
28
+ // src/seededRandom.ts
29
+ var SeededRandom = class {
30
+ seed;
31
+ constructor(seed) {
32
+ this.seed = seed;
33
+ }
34
+ // Returns a random number between 0 and 1
35
+ random() {
36
+ let t = this.seed += 1831565813;
37
+ t = Math.imul(t ^ t >>> 15, t | 1);
38
+ t ^= t + Math.imul(t ^ t >>> 7, t | 61);
39
+ return ((t ^ t >>> 14) >>> 0) / 4294967296;
40
+ }
41
+ // Returns a random integer between min (inclusive) and max (inclusive)
42
+ randomInt(min, max) {
43
+ return Math.floor(this.random() * (max - min + 1)) + min;
44
+ }
45
+ // Returns a random element from an array
46
+ randomElement(array) {
47
+ return array[Math.floor(this.random() * array.length)];
48
+ }
49
+ // Shuffles an array
50
+ shuffle(array) {
51
+ const shuffled = [...array];
52
+ for (let i = shuffled.length - 1; i > 0; i--) {
53
+ const j = Math.floor(this.random() * (i + 1));
54
+ [shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
55
+ }
56
+ return shuffled;
57
+ }
58
+ };
59
+
60
+ // src/index.ts
61
+ var FALLBACK_COLORS = ["#FF006E", "#8338EC", "#3A86FF", "#FFBE0B", "#FB5607"];
62
+ function hashSeed(seedString) {
63
+ let hash = 0;
64
+ for (let i = 0; i < seedString.length; i++) {
65
+ hash = Math.imul(31, hash) + seedString.charCodeAt(i) | 0;
66
+ }
67
+ return hash;
68
+ }
69
+ function createIcon(style, options = {}) {
70
+ const seedString = options.seed !== void 0 ? String(options.seed) : Math.random().toString();
71
+ const rngSeed = hashSeed(seedString);
72
+ const rng = new SeededRandom(rngSeed);
73
+ const size = options.size || 128;
74
+ const resolvedOptions = {
75
+ ...options,
76
+ size,
77
+ colors: options.colors && options.colors.length > 0 ? options.colors : FALLBACK_COLORS
78
+ };
79
+ const svgBody = style(rng, resolvedOptions);
80
+ let clipPathDef = "";
81
+ let clipGroupStart = '<g stroke="none" stroke-width="0">';
82
+ const clipGroupEnd = "</g>";
83
+ if (options.isCircle) {
84
+ const clipPathId = "circle-clip-" + rngSeed;
85
+ clipPathDef = `<clipPath id="${clipPathId}"><circle cx="${size / 2}" cy="${size / 2}" r="${size / 2}"/></clipPath>`;
86
+ clipGroupStart = `<g clip-path="url(#${clipPathId})" stroke="none" stroke-width="0">`;
87
+ } else if (options.borderRadius && options.borderRadius > 0) {
88
+ const clipPathId = "rounded-clip-" + rngSeed;
89
+ const r = Math.min(options.borderRadius, size / 2);
90
+ clipPathDef = `<clipPath id="${clipPathId}"><rect width="${size}" height="${size}" rx="${r}"/></clipPath>`;
91
+ clipGroupStart = `<g clip-path="url(#${clipPathId})" stroke="none" stroke-width="0">`;
92
+ }
93
+ const svgString = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${size} ${size}" width="${size}" height="${size}" shape-rendering="crispEdges">${clipPathDef ? `<defs>${clipPathDef}</defs>` : ""}${clipGroupStart}${svgBody}${clipGroupEnd}</svg>`;
94
+ return {
95
+ toString: () => svgString,
96
+ toDataUri: () => `data:image/svg+xml;charset=utf-8,${encodeURIComponent(svgString)}`
97
+ };
98
+ }
99
+ // Annotate the CommonJS export names for ESM import in node:
100
+ 0 && (module.exports = {
101
+ SeededRandom,
102
+ createIcon
103
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,75 @@
1
+ // src/seededRandom.ts
2
+ var SeededRandom = class {
3
+ seed;
4
+ constructor(seed) {
5
+ this.seed = seed;
6
+ }
7
+ // Returns a random number between 0 and 1
8
+ random() {
9
+ let t = this.seed += 1831565813;
10
+ t = Math.imul(t ^ t >>> 15, t | 1);
11
+ t ^= t + Math.imul(t ^ t >>> 7, t | 61);
12
+ return ((t ^ t >>> 14) >>> 0) / 4294967296;
13
+ }
14
+ // Returns a random integer between min (inclusive) and max (inclusive)
15
+ randomInt(min, max) {
16
+ return Math.floor(this.random() * (max - min + 1)) + min;
17
+ }
18
+ // Returns a random element from an array
19
+ randomElement(array) {
20
+ return array[Math.floor(this.random() * array.length)];
21
+ }
22
+ // Shuffles an array
23
+ shuffle(array) {
24
+ const shuffled = [...array];
25
+ for (let i = shuffled.length - 1; i > 0; i--) {
26
+ const j = Math.floor(this.random() * (i + 1));
27
+ [shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
28
+ }
29
+ return shuffled;
30
+ }
31
+ };
32
+
33
+ // src/index.ts
34
+ var FALLBACK_COLORS = ["#FF006E", "#8338EC", "#3A86FF", "#FFBE0B", "#FB5607"];
35
+ function hashSeed(seedString) {
36
+ let hash = 0;
37
+ for (let i = 0; i < seedString.length; i++) {
38
+ hash = Math.imul(31, hash) + seedString.charCodeAt(i) | 0;
39
+ }
40
+ return hash;
41
+ }
42
+ function createIcon(style, options = {}) {
43
+ const seedString = options.seed !== void 0 ? String(options.seed) : Math.random().toString();
44
+ const rngSeed = hashSeed(seedString);
45
+ const rng = new SeededRandom(rngSeed);
46
+ const size = options.size || 128;
47
+ const resolvedOptions = {
48
+ ...options,
49
+ size,
50
+ colors: options.colors && options.colors.length > 0 ? options.colors : FALLBACK_COLORS
51
+ };
52
+ const svgBody = style(rng, resolvedOptions);
53
+ let clipPathDef = "";
54
+ let clipGroupStart = '<g stroke="none" stroke-width="0">';
55
+ const clipGroupEnd = "</g>";
56
+ if (options.isCircle) {
57
+ const clipPathId = "circle-clip-" + rngSeed;
58
+ clipPathDef = `<clipPath id="${clipPathId}"><circle cx="${size / 2}" cy="${size / 2}" r="${size / 2}"/></clipPath>`;
59
+ clipGroupStart = `<g clip-path="url(#${clipPathId})" stroke="none" stroke-width="0">`;
60
+ } else if (options.borderRadius && options.borderRadius > 0) {
61
+ const clipPathId = "rounded-clip-" + rngSeed;
62
+ const r = Math.min(options.borderRadius, size / 2);
63
+ clipPathDef = `<clipPath id="${clipPathId}"><rect width="${size}" height="${size}" rx="${r}"/></clipPath>`;
64
+ clipGroupStart = `<g clip-path="url(#${clipPathId})" stroke="none" stroke-width="0">`;
65
+ }
66
+ const svgString = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${size} ${size}" width="${size}" height="${size}" shape-rendering="crispEdges">${clipPathDef ? `<defs>${clipPathDef}</defs>` : ""}${clipGroupStart}${svgBody}${clipGroupEnd}</svg>`;
67
+ return {
68
+ toString: () => svgString,
69
+ toDataUri: () => `data:image/svg+xml;charset=utf-8,${encodeURIComponent(svgString)}`
70
+ };
71
+ }
72
+ export {
73
+ SeededRandom,
74
+ createIcon
75
+ };
package/package.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ "name": "@iconoma-icons/core",
3
+ "version": "1.0.0",
4
+ "description": "Core generation engine for Iconoma — deterministic SVG icon generator.",
5
+ "license": "MIT",
6
+ "author": "0xnocap",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/0xnocap/iconoma.git",
10
+ "directory": "packages/core"
11
+ },
12
+ "keywords": [
13
+ "icon",
14
+ "svg",
15
+ "avatar",
16
+ "deterministic",
17
+ "identicon",
18
+ "generator",
19
+ "seed"
20
+ ],
21
+ "main": "dist/index.js",
22
+ "module": "dist/index.mjs",
23
+ "types": "dist/index.d.ts",
24
+ "exports": {
25
+ ".": {
26
+ "import": {
27
+ "types": "./dist/index.d.mts",
28
+ "default": "./dist/index.mjs"
29
+ },
30
+ "require": {
31
+ "types": "./dist/index.d.ts",
32
+ "default": "./dist/index.js"
33
+ }
34
+ }
35
+ },
36
+ "files": [
37
+ "dist",
38
+ "README.md",
39
+ "LICENSE"
40
+ ],
41
+ "sideEffects": false,
42
+ "scripts": {
43
+ "build": "tsup src/index.ts --format cjs,esm --dts"
44
+ },
45
+ "devDependencies": {
46
+ "tsup": "^8.0.2",
47
+ "typescript": "^5.2.2"
48
+ }
49
+ }