@unmade/svg-helpers 0.1.0 → 0.1.2

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.
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const a=o=>{let e=Array.from(o),t=!1;return e[0]==="#"&&(e.shift(),t=!0),e.length===3&&(e=e.map(r=>`${r}${r}`)),`${t?"#":""}${e.join("").toLowerCase()}`};exports.expandHex=a;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const p=require("./named-colours.cjs"),m=r=>{let t=Array.from(r),s=!1;return t[0]==="#"&&(t.shift(),s=!0),t.length===3&&(t=t.map(e=>`${e}${e}`)),`${s?"#":""}${t.join("").toLowerCase()}`},h=r=>Math.max(0,Math.min(255,Math.round(r))).toString(16).padStart(2,"0"),l=(...r)=>{const[t,s,e]=Array.isArray(r[0])?r[0]:r;return`#${h(t)}${h(s)}${h(e)}`},f=(r,t,s)=>{if(t===0){const d=Math.round(s*255);return[d,d,d]}const e=(r%360+360)%360,o=(1-Math.abs(2*s-1))*t,u=o*(1-Math.abs(e/60%2-1)),c=s-o/2;let n=0,a=0,i=0;return e<60?(n=o,a=u,i=0):e<120?(n=u,a=o,i=0):e<180?(n=0,a=o,i=u):e<240?(n=0,a=u,i=o):e<300?(n=u,a=0,i=o):(n=o,a=0,i=u),[Math.round((n+c)*255),Math.round((a+c)*255),Math.round((i+c)*255)]},b=r=>/^#?([0-9a-f]{3}|[0-9a-f]{6})$/i.test(r),x=r=>{const t=r.trim().toLowerCase();if(t.startsWith("#")||/^[0-9a-f]{3}([0-9a-f]{3})?$/.test(t))return b(t)?m(t):void 0;if(t.startsWith("rgb")){const e=t.match(/rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*(?:,\s*[\d.]+\s*)?\)/);return e?l(Number.parseInt(e[1],10),Number.parseInt(e[2],10),Number.parseInt(e[3],10)):void 0}if(t.startsWith("hsl")){const e=t.match(/hsla?\(\s*(\d+)\s*,\s*([\d.]+)%\s*,\s*([\d.]+)%\s*(?:,\s*[\d.]+\s*)?\)/);if(!e)return;const o=Number.parseInt(e[1],10),u=Number.parseFloat(e[2])/100,c=Number.parseFloat(e[3])/100,[n,a,i]=f(o,u,c);return l(n,a,i)}const s=p.CSS_NAMED_COLOURS[t];if(s)return s};exports.expandHex=m;exports.hslToRgb=f;exports.isHex=b;exports.normaliseColour=x;exports.rgbToHex=l;
@@ -1,7 +1,51 @@
1
- const a = (o) => {
2
- let e = Array.from(o), t = !1;
3
- return e[0] === "#" && (e.shift(), t = !0), e.length === 3 && (e = e.map((r) => `${r}${r}`)), `${t ? "#" : ""}${e.join("").toLowerCase()}`;
1
+ import { CSS_NAMED_COLOURS as f } from "./named-colours.mjs";
2
+ const l = (s) => {
3
+ let t = Array.from(s), e = !1;
4
+ return t[0] === "#" && (t.shift(), e = !0), t.length === 3 && (t = t.map((r) => `${r}${r}`)), `${e ? "#" : ""}${t.join("").toLowerCase()}`;
5
+ }, m = (s) => Math.max(0, Math.min(255, Math.round(s))).toString(16).padStart(2, "0"), d = (...s) => {
6
+ const [t, e, r] = Array.isArray(s[0]) ? s[0] : s;
7
+ return `#${m(t)}${m(e)}${m(r)}`;
8
+ }, b = (s, t, e) => {
9
+ if (t === 0) {
10
+ const h = Math.round(e * 255);
11
+ return [h, h, h];
12
+ }
13
+ const r = (s % 360 + 360) % 360, n = (1 - Math.abs(2 * e - 1)) * t, c = n * (1 - Math.abs(r / 60 % 2 - 1)), u = e - n / 2;
14
+ let o = 0, a = 0, i = 0;
15
+ return r < 60 ? (o = n, a = c, i = 0) : r < 120 ? (o = c, a = n, i = 0) : r < 180 ? (o = 0, a = n, i = c) : r < 240 ? (o = 0, a = c, i = n) : r < 300 ? (o = c, a = 0, i = n) : (o = n, a = 0, i = c), [
16
+ Math.round((o + u) * 255),
17
+ Math.round((a + u) * 255),
18
+ Math.round((i + u) * 255)
19
+ ];
20
+ }, p = (s) => /^#?([0-9a-f]{3}|[0-9a-f]{6})$/i.test(s), M = (s) => {
21
+ const t = s.trim().toLowerCase();
22
+ if (t.startsWith("#") || /^[0-9a-f]{3}([0-9a-f]{3})?$/.test(t))
23
+ return p(t) ? l(t) : void 0;
24
+ if (t.startsWith("rgb")) {
25
+ const r = t.match(
26
+ /rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*(?:,\s*[\d.]+\s*)?\)/
27
+ );
28
+ return r ? d(
29
+ Number.parseInt(r[1], 10),
30
+ Number.parseInt(r[2], 10),
31
+ Number.parseInt(r[3], 10)
32
+ ) : void 0;
33
+ }
34
+ if (t.startsWith("hsl")) {
35
+ const r = t.match(
36
+ /hsla?\(\s*(\d+)\s*,\s*([\d.]+)%\s*,\s*([\d.]+)%\s*(?:,\s*[\d.]+\s*)?\)/
37
+ );
38
+ if (!r) return;
39
+ const n = Number.parseInt(r[1], 10), c = Number.parseFloat(r[2]) / 100, u = Number.parseFloat(r[3]) / 100, [o, a, i] = b(n, c, u);
40
+ return d(o, a, i);
41
+ }
42
+ const e = f[t];
43
+ if (e) return e;
4
44
  };
5
45
  export {
6
- a as expandHex
46
+ l as expandHex,
47
+ b as hslToRgb,
48
+ p as isHex,
49
+ M as normaliseColour,
50
+ d as rgbToHex
7
51
  };
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e={aliceblue:"#f0f8ff",antiquewhite:"#faebd7",aqua:"#00ffff",aquamarine:"#7fffd4",azure:"#f0ffff",beige:"#f5f5dc",bisque:"#ffe4c4",black:"#000000",blanchedalmond:"#ffebcd",blue:"#0000ff",blueviolet:"#8a2be2",brown:"#a52a2a",burlywood:"#deb887",cadetblue:"#5f9ea0",chartreuse:"#7fff00",chocolate:"#d2691e",coral:"#ff7f50",cornflowerblue:"#6495ed",cornsilk:"#fff8dc",crimson:"#dc143c",cyan:"#00ffff",darkblue:"#00008b",darkcyan:"#008b8b",darkgoldenrod:"#b8860b",darkgray:"#a9a9a9",darkgreen:"#006400",darkgrey:"#a9a9a9",darkkhaki:"#bdb76b",darkmagenta:"#8b008b",darkolivegreen:"#556b2f",darkorange:"#ff8c00",darkorchid:"#9932cc",darkred:"#8b0000",darksalmon:"#e9967a",darkseagreen:"#8fbc8f",darkslateblue:"#483d8b",darkslategray:"#2f4f4f",darkslategrey:"#2f4f4f",darkturquoise:"#00ced1",darkviolet:"#9400d3",deeppink:"#ff1493",deepskyblue:"#00bfff",dimgray:"#696969",dimgrey:"#696969",dodgerblue:"#1e90ff",firebrick:"#b22222",floralwhite:"#fffaf0",forestgreen:"#228b22",fuchsia:"#ff00ff",gainsboro:"#dcdcdc",ghostwhite:"#f8f8ff",gold:"#ffd700",goldenrod:"#daa520",gray:"#808080",green:"#008000",greenyellow:"#adff2f",grey:"#808080",honeydew:"#f0fff0",hotpink:"#ff69b4",indianred:"#cd5c5c",indigo:"#4b0082",ivory:"#fffff0",khaki:"#f0e68c",lavender:"#e6e6fa",lavenderblush:"#fff0f5",lawngreen:"#7cfc00",lemonchiffon:"#fffacd",lightblue:"#add8e6",lightcoral:"#f08080",lightcyan:"#e0ffff",lightgoldenrodyellow:"#fafad2",lightgray:"#d3d3d3",lightgreen:"#90ee90",lightgrey:"#d3d3d3",lightpink:"#ffb6c1",lightsalmon:"#ffa07a",lightseagreen:"#20b2aa",lightskyblue:"#87cefa",lightslategray:"#778899",lightslategrey:"#778899",lightsteelblue:"#b0c4de",lightyellow:"#ffffe0",lime:"#00ff00",limegreen:"#32cd32",linen:"#faf0e6",magenta:"#ff00ff",maroon:"#800000",mediumaquamarine:"#66cdaa",mediumblue:"#0000cd",mediumorchid:"#ba55d3",mediumpurple:"#9370db",mediumseagreen:"#3cb371",mediumslateblue:"#7b68ee",mediumspringgreen:"#00fa9a",mediumturquoise:"#48d1cc",mediumvioletred:"#c71585",midnightblue:"#191970",mintcream:"#f5fffa",mistyrose:"#ffe4e1",moccasin:"#ffe4b5",navajowhite:"#ffdead",navy:"#000080",oldlace:"#fdf5e6",olive:"#808000",olivedrab:"#6b8e23",orange:"#ffa500",orangered:"#ff4500",orchid:"#da70d6",palegoldenrod:"#eee8aa",palegreen:"#98fb98",paleturquoise:"#afeeee",palevioletred:"#db7093",papayawhip:"#ffefd5",peachpuff:"#ffdab9",peru:"#cd853f",pink:"#ffc0cb",plum:"#dda0dd",powderblue:"#b0e0e6",purple:"#800080",rebeccapurple:"#663399",red:"#ff0000",rosybrown:"#bc8f8f",royalblue:"#4169e1",saddlebrown:"#8b4513",salmon:"#fa8072",sandybrown:"#f4a460",seagreen:"#2e8b57",seashell:"#fff5ee",sienna:"#a0522d",silver:"#c0c0c0",skyblue:"#87ceeb",slateblue:"#6a5acd",slategray:"#708090",slategrey:"#708090",snow:"#fffafa",springgreen:"#00ff7f",steelblue:"#4682b4",tan:"#d2b48c",teal:"#008080",thistle:"#d8bfd8",tomato:"#ff6347",turquoise:"#40e0d0",violet:"#ee82ee",wheat:"#f5deb3",white:"#ffffff",whitesmoke:"#f5f5f5",yellow:"#ffff00",yellowgreen:"#9acd32"};exports.CSS_NAMED_COLOURS=e;
@@ -0,0 +1,153 @@
1
+ const e = {
2
+ aliceblue: "#f0f8ff",
3
+ antiquewhite: "#faebd7",
4
+ aqua: "#00ffff",
5
+ aquamarine: "#7fffd4",
6
+ azure: "#f0ffff",
7
+ beige: "#f5f5dc",
8
+ bisque: "#ffe4c4",
9
+ black: "#000000",
10
+ blanchedalmond: "#ffebcd",
11
+ blue: "#0000ff",
12
+ blueviolet: "#8a2be2",
13
+ brown: "#a52a2a",
14
+ burlywood: "#deb887",
15
+ cadetblue: "#5f9ea0",
16
+ chartreuse: "#7fff00",
17
+ chocolate: "#d2691e",
18
+ coral: "#ff7f50",
19
+ cornflowerblue: "#6495ed",
20
+ cornsilk: "#fff8dc",
21
+ crimson: "#dc143c",
22
+ cyan: "#00ffff",
23
+ darkblue: "#00008b",
24
+ darkcyan: "#008b8b",
25
+ darkgoldenrod: "#b8860b",
26
+ darkgray: "#a9a9a9",
27
+ darkgreen: "#006400",
28
+ darkgrey: "#a9a9a9",
29
+ darkkhaki: "#bdb76b",
30
+ darkmagenta: "#8b008b",
31
+ darkolivegreen: "#556b2f",
32
+ darkorange: "#ff8c00",
33
+ darkorchid: "#9932cc",
34
+ darkred: "#8b0000",
35
+ darksalmon: "#e9967a",
36
+ darkseagreen: "#8fbc8f",
37
+ darkslateblue: "#483d8b",
38
+ darkslategray: "#2f4f4f",
39
+ darkslategrey: "#2f4f4f",
40
+ darkturquoise: "#00ced1",
41
+ darkviolet: "#9400d3",
42
+ deeppink: "#ff1493",
43
+ deepskyblue: "#00bfff",
44
+ dimgray: "#696969",
45
+ dimgrey: "#696969",
46
+ dodgerblue: "#1e90ff",
47
+ firebrick: "#b22222",
48
+ floralwhite: "#fffaf0",
49
+ forestgreen: "#228b22",
50
+ fuchsia: "#ff00ff",
51
+ gainsboro: "#dcdcdc",
52
+ ghostwhite: "#f8f8ff",
53
+ gold: "#ffd700",
54
+ goldenrod: "#daa520",
55
+ gray: "#808080",
56
+ green: "#008000",
57
+ greenyellow: "#adff2f",
58
+ grey: "#808080",
59
+ honeydew: "#f0fff0",
60
+ hotpink: "#ff69b4",
61
+ indianred: "#cd5c5c",
62
+ indigo: "#4b0082",
63
+ ivory: "#fffff0",
64
+ khaki: "#f0e68c",
65
+ lavender: "#e6e6fa",
66
+ lavenderblush: "#fff0f5",
67
+ lawngreen: "#7cfc00",
68
+ lemonchiffon: "#fffacd",
69
+ lightblue: "#add8e6",
70
+ lightcoral: "#f08080",
71
+ lightcyan: "#e0ffff",
72
+ lightgoldenrodyellow: "#fafad2",
73
+ lightgray: "#d3d3d3",
74
+ lightgreen: "#90ee90",
75
+ lightgrey: "#d3d3d3",
76
+ lightpink: "#ffb6c1",
77
+ lightsalmon: "#ffa07a",
78
+ lightseagreen: "#20b2aa",
79
+ lightskyblue: "#87cefa",
80
+ lightslategray: "#778899",
81
+ lightslategrey: "#778899",
82
+ lightsteelblue: "#b0c4de",
83
+ lightyellow: "#ffffe0",
84
+ lime: "#00ff00",
85
+ limegreen: "#32cd32",
86
+ linen: "#faf0e6",
87
+ magenta: "#ff00ff",
88
+ maroon: "#800000",
89
+ mediumaquamarine: "#66cdaa",
90
+ mediumblue: "#0000cd",
91
+ mediumorchid: "#ba55d3",
92
+ mediumpurple: "#9370db",
93
+ mediumseagreen: "#3cb371",
94
+ mediumslateblue: "#7b68ee",
95
+ mediumspringgreen: "#00fa9a",
96
+ mediumturquoise: "#48d1cc",
97
+ mediumvioletred: "#c71585",
98
+ midnightblue: "#191970",
99
+ mintcream: "#f5fffa",
100
+ mistyrose: "#ffe4e1",
101
+ moccasin: "#ffe4b5",
102
+ navajowhite: "#ffdead",
103
+ navy: "#000080",
104
+ oldlace: "#fdf5e6",
105
+ olive: "#808000",
106
+ olivedrab: "#6b8e23",
107
+ orange: "#ffa500",
108
+ orangered: "#ff4500",
109
+ orchid: "#da70d6",
110
+ palegoldenrod: "#eee8aa",
111
+ palegreen: "#98fb98",
112
+ paleturquoise: "#afeeee",
113
+ palevioletred: "#db7093",
114
+ papayawhip: "#ffefd5",
115
+ peachpuff: "#ffdab9",
116
+ peru: "#cd853f",
117
+ pink: "#ffc0cb",
118
+ plum: "#dda0dd",
119
+ powderblue: "#b0e0e6",
120
+ purple: "#800080",
121
+ rebeccapurple: "#663399",
122
+ red: "#ff0000",
123
+ rosybrown: "#bc8f8f",
124
+ royalblue: "#4169e1",
125
+ saddlebrown: "#8b4513",
126
+ salmon: "#fa8072",
127
+ sandybrown: "#f4a460",
128
+ seagreen: "#2e8b57",
129
+ seashell: "#fff5ee",
130
+ sienna: "#a0522d",
131
+ silver: "#c0c0c0",
132
+ skyblue: "#87ceeb",
133
+ slateblue: "#6a5acd",
134
+ slategray: "#708090",
135
+ slategrey: "#708090",
136
+ snow: "#fffafa",
137
+ springgreen: "#00ff7f",
138
+ steelblue: "#4682b4",
139
+ tan: "#d2b48c",
140
+ teal: "#008080",
141
+ thistle: "#d8bfd8",
142
+ tomato: "#ff6347",
143
+ turquoise: "#40e0d0",
144
+ violet: "#ee82ee",
145
+ wheat: "#f5deb3",
146
+ white: "#ffffff",
147
+ whitesmoke: "#f5f5f5",
148
+ yellow: "#ffff00",
149
+ yellowgreen: "#9acd32"
150
+ };
151
+ export {
152
+ e as CSS_NAMED_COLOURS
153
+ };
package/dist/colours.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=require("./colours/format.cjs"),e={svg:"svg",circle:"circle",desc:"desc",ellipse:"ellipse",rect:"rect",line:"line",path:"path",polygon:"polygon",polyline:"polyline",stop:"stop"},o={fill:"fill",stroke:"stroke"},E={tiling:[e.circle,e.ellipse,e.rect,e.line,e.path,e.polygon,e.polyline,e.stop,`[${o.fill}]`,`[${o.stroke}]`],nonTiling:[e.circle,e.desc,e.ellipse,e.rect,e.line,e.path,e.polygon,e.polyline,e.stop,`[${o.fill}]`,`[${o.stroke}]`]},a=l=>Array.from(l.querySelectorAll(E.tiling.map(t=>`*:not(clipPath) > ${t}`).join(", "))),u=l=>{const t=l.getAttribute(o.stroke);return t==="none"||!t?!1:t},d=l=>{const g=a(l).reduce((r,n)=>{const i=n.getAttribute("stop-color"),c=n.getAttribute(o.fill)||"#000",p=u(n);return!i&&c.startsWith("#")&&r.add(s.expandHex(c)),i&&r.add(s.expandHex(i)),p&&r.add(s.expandHex(p)),r},new Set);return Array.from(g)};exports.COLOURABLE_ELEMENT_SELECTORS=E;exports.ELEMENTS=e;exports.getColourableElements=a;exports.getUniqueColoursFromSVG=d;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const f=require("@unmade/platform"),l=require("./colours/format.cjs"),d=require("./colours/named-colours.cjs"),e={svg:"svg",circle:"circle",desc:"desc",ellipse:"ellipse",rect:"rect",line:"line",path:"path",polygon:"polygon",polyline:"polyline",stop:"stop"},n={fill:"fill",stroke:"stroke"},p={tiling:[e.circle,e.ellipse,e.rect,e.line,e.path,e.polygon,e.polyline,e.stop,`[${n.fill}]`,`[${n.stroke}]`],nonTiling:[e.circle,e.desc,e.ellipse,e.rect,e.line,e.path,e.polygon,e.polyline,e.stop,`[${n.fill}]`,`[${n.stroke}]`]},S=o=>Array.from(o.querySelectorAll(p.tiling.map(r=>`*:not(clipPath) > ${r}`).join(", "))),C=o=>{const r=o.getAttribute(n.stroke);return r==="none"||!r?!1:r},a=o=>{if(!(o==="none"||o==="transparent"||o.startsWith("url(")))return l.normaliseColour(o)},m=o=>{const s=S(o).reduce((i,c)=>{const g=c.getAttribute("stop-color"),E=c.getAttribute(n.fill)||"#000",u=C(c);if(!g){const t=a(E);t&&i.add(t)}if(g){const t=a(g);t&&i.add(t)}if(u){const t=a(u);t&&i.add(t)}return i},new Set);return Array.from(s)},T=o=>{const s=new f.DOMParser().parseFromString(o,"image/svg+xml").querySelector("svg");return s?m(s):[]};exports.expandHex=l.expandHex;exports.hslToRgb=l.hslToRgb;exports.isHex=l.isHex;exports.normaliseColour=l.normaliseColour;exports.rgbToHex=l.rgbToHex;exports.CSS_NAMED_COLOURS=d.CSS_NAMED_COLOURS;exports.COLOURABLE_ELEMENT_SELECTORS=p;exports.ELEMENTS=e;exports.getColourableElements=S;exports.getColoursFromSvgString=T;exports.getUniqueColoursFromSVG=m;
package/dist/colours.mjs CHANGED
@@ -1,4 +1,7 @@
1
- import { expandHex as s } from "./colours/format.mjs";
1
+ import { DOMParser as f } from "@unmade/platform";
2
+ import { normaliseColour as m } from "./colours/format.mjs";
3
+ import { expandHex as x, hslToRgb as h, isHex as O, rgbToHex as b } from "./colours/format.mjs";
4
+ import { CSS_NAMED_COLOURS as R } from "./colours/named-colours.mjs";
2
5
  const e = {
3
6
  svg: "svg",
4
7
  circle: "circle",
@@ -10,10 +13,10 @@ const e = {
10
13
  polygon: "polygon",
11
14
  polyline: "polyline",
12
15
  stop: "stop"
13
- }, l = {
16
+ }, n = {
14
17
  fill: "fill",
15
18
  stroke: "stroke"
16
- }, g = {
19
+ }, u = {
17
20
  tiling: [
18
21
  e.circle,
19
22
  e.ellipse,
@@ -23,8 +26,8 @@ const e = {
23
26
  e.polygon,
24
27
  e.polyline,
25
28
  e.stop,
26
- `[${l.fill}]`,
27
- `[${l.stroke}]`
29
+ `[${n.fill}]`,
30
+ `[${n.stroke}]`
28
31
  ],
29
32
  nonTiling: [
30
33
  e.circle,
@@ -36,31 +39,54 @@ const e = {
36
39
  e.polygon,
37
40
  e.polyline,
38
41
  e.stop,
39
- `[${l.fill}]`,
40
- `[${l.stroke}]`
42
+ `[${n.fill}]`,
43
+ `[${n.stroke}]`
41
44
  ]
42
- }, f = (o) => Array.from(
43
- o.querySelectorAll(
44
- g.tiling.map((t) => `*:not(clipPath) > ${t}`).join(", ")
45
+ }, d = (t) => Array.from(
46
+ t.querySelectorAll(
47
+ u.tiling.map((r) => `*:not(clipPath) > ${r}`).join(", ")
45
48
  )
46
- ), u = (o) => {
47
- const t = o.getAttribute(l.stroke);
48
- return t === "none" || !t ? !1 : t;
49
- }, d = (o) => {
50
- const a = f(o).reduce(
51
- (r, n) => {
52
- const i = n.getAttribute("stop-color"), c = n.getAttribute(l.fill) || "#000", p = u(n);
53
- return !i && c.startsWith("#") && r.add(s(c)), i && r.add(s(i)), p && r.add(s(p)), r;
49
+ ), S = (t) => {
50
+ const r = t.getAttribute(n.stroke);
51
+ return r === "none" || !r ? !1 : r;
52
+ }, p = (t) => {
53
+ if (!(t === "none" || t === "transparent" || t.startsWith("url(")))
54
+ return m(t);
55
+ }, E = (t) => {
56
+ const l = d(t).reduce(
57
+ (s, i) => {
58
+ const c = i.getAttribute("stop-color"), a = i.getAttribute(n.fill) || "#000", g = S(i);
59
+ if (!c) {
60
+ const o = p(a);
61
+ o && s.add(o);
62
+ }
63
+ if (c) {
64
+ const o = p(c);
65
+ o && s.add(o);
66
+ }
67
+ if (g) {
68
+ const o = p(g);
69
+ o && s.add(o);
70
+ }
71
+ return s;
54
72
  },
55
73
  /* @__PURE__ */ new Set()
56
74
  );
57
- return Array.from(
58
- a
59
- );
75
+ return Array.from(l);
76
+ }, C = (t) => {
77
+ const l = new f().parseFromString(t, "image/svg+xml").querySelector("svg");
78
+ return l ? E(l) : [];
60
79
  };
61
80
  export {
62
- g as COLOURABLE_ELEMENT_SELECTORS,
81
+ u as COLOURABLE_ELEMENT_SELECTORS,
82
+ R as CSS_NAMED_COLOURS,
63
83
  e as ELEMENTS,
64
- f as getColourableElements,
65
- d as getUniqueColoursFromSVG
84
+ x as expandHex,
85
+ d as getColourableElements,
86
+ C as getColoursFromSvgString,
87
+ E as getUniqueColoursFromSVG,
88
+ h as hslToRgb,
89
+ O as isHex,
90
+ m as normaliseColour,
91
+ b as rgbToHex
66
92
  };
package/dist/index.d.ts CHANGED
@@ -3,6 +3,14 @@ export declare const COLOURABLE_ELEMENT_SELECTORS: {
3
3
  nonTiling: string[];
4
4
  };
5
5
 
6
+ /**
7
+ * CSS named colours mapped to their lowercase hex equivalents.
8
+ *
9
+ * Lookup keys are lowercase to allow case-insensitive matching.
10
+ * @see https://www.w3.org/TR/css-color-4/#named-colors
11
+ */
12
+ export declare const CSS_NAMED_COLOURS: Readonly<Record<string, string>>;
13
+
6
14
  export declare const ELEMENTS: {
7
15
  svg: string;
8
16
  circle: string;
@@ -16,6 +24,14 @@ export declare const ELEMENTS: {
16
24
  stop: string;
17
25
  };
18
26
 
27
+ /**
28
+ * Convert a short hex to full length.
29
+ *
30
+ * @param short - the initial hex value
31
+ * @returns the expanded hex value
32
+ */
33
+ export declare const expandHex: (short: string) => string;
34
+
19
35
  /**
20
36
  * Fetch all colourable elements from an SVG
21
37
  *
@@ -24,12 +40,60 @@ export declare const ELEMENTS: {
24
40
  */
25
41
  export declare const getColourableElements: (svg: SVGElement) => SVGElement[];
26
42
 
43
+ /**
44
+ * Extract unique normalised colours from a raw SVG string.
45
+ *
46
+ * Parses the string via DOMParser, then delegates to `getUniqueColoursFromSVG`.
47
+ * This is a convenience function for working with SVG glyph data from fonts.
48
+ *
49
+ * @param svgString - Raw SVG markup
50
+ * @returns Array of unique `#rrggbb` hex colour strings
51
+ */
52
+ export declare const getColoursFromSvgString: (svgString: string) => string[];
53
+
27
54
  /**
28
55
  * Get a list of the unique colours used in an SVG document.
29
56
  *
57
+ * Extracts fill, stroke, and stop-color attributes from all colourable
58
+ * elements. Normalises all colour formats (hex, rgb, hsl, named) to
59
+ * canonical `#rrggbb` lowercase hex. Elements inside `<clipPath>` are excluded.
60
+ *
30
61
  * @param {SVGElement} svg - the document
31
- * @returns {Array} the colours (with shortened hex codes expanded to full)
62
+ * @returns {string[]} unique colour hex strings
63
+ */
64
+ export declare const getUniqueColoursFromSVG: (svg: SVGElement) => string[];
65
+
66
+ /**
67
+ * Convert HSL values to RGB.
68
+ *
69
+ * @param h - Hue in degrees (0–360)
70
+ * @param s - Saturation as a fraction (0–1)
71
+ * @param l - Lightness as a fraction (0–1)
72
+ * @returns [r, g, b] with each channel in 0–255
73
+ */
74
+ export declare const hslToRgb: (h: number, s: number, l: number) => [number, number, number];
75
+
76
+ /**
77
+ * Validate whether a string is a hex colour (3 or 6 digit, with optional #).
78
+ */
79
+ export declare const isHex: (str: string) => boolean;
80
+
81
+ /**
82
+ * Normalise any CSS colour value to a canonical `#rrggbb` lowercase hex string.
83
+ *
84
+ * Supported formats:
85
+ * - Hex: `#RGB`, `#RRGGBB` (with or without `#`)
86
+ * - RGB/RGBA: `rgb(r, g, b)`, `rgba(r, g, b, a)`
87
+ * - HSL/HSLA: `hsl(h, s%, l%)`, `hsla(h, s%, l%, a)`
88
+ * - CSS named colours: `red`, `blue`, `coral`, etc.
89
+ *
90
+ * @returns Normalised `#rrggbb` string, or `undefined` if the value is not a recognised colour
91
+ */
92
+ export declare const normaliseColour: (value: string) => string | undefined;
93
+
94
+ /**
95
+ * Convert RGB channel values (0–255) to a `#rrggbb` hex string.
32
96
  */
33
- export declare const getUniqueColoursFromSVG: (svg: SVGElement) => unknown[];
97
+ export declare const rgbToHex: (...args: [r: number, g: number, b: number] | [[number, number, number]]) => string;
34
98
 
35
99
  export { }
package/package.json CHANGED
@@ -1,15 +1,15 @@
1
1
  {
2
2
  "name": "@unmade/svg-helpers",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "Set of helper functions for working with SVG content",
5
5
  "type": "module",
6
- "main": "./dist/index.cjs",
7
- "module": "./dist/index.js",
6
+ "main": "./dist/colours.cjs",
7
+ "module": "./dist/colours.mjs",
8
8
  "types": "./dist/index.d.ts",
9
9
  "exports": {
10
10
  ".": {
11
- "import": "./dist/index.js",
12
- "require": "./dist/index.cjs",
11
+ "import": "./dist/colours.mjs",
12
+ "require": "./dist/colours.cjs",
13
13
  "types": "./dist/index.d.ts"
14
14
  }
15
15
  },
@@ -22,11 +22,14 @@
22
22
  "test:watch": "vitest",
23
23
  "typecheck": "tsc --noEmit"
24
24
  },
25
+ "dependencies": {
26
+ "@unmade/platform": "^0.1.2"
27
+ },
25
28
  "devDependencies": {
26
- "typescript": "^5.3.0",
27
- "vite": "^5.0.0",
28
- "vite-plugin-dts": "^3.7.0",
29
- "vitest": "^1.2.0"
29
+ "typescript": "^5.9.3",
30
+ "vite": "^7.3.1",
31
+ "vite-plugin-dts": "^4.5.4",
32
+ "vitest": "^3.2.4"
30
33
  },
31
34
  "keywords": [
32
35
  "svg"