@unmade/svg-helpers 0.1.1 → 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 p=require("./named-colours.cjs"),m=s=>{let t=Array.from(s),r=!1;return t[0]==="#"&&(t.shift(),r=!0),t.length===3&&(t=t.map(e=>`${e}${e}`)),`${r?"#":""}${t.join("").toLowerCase()}`},h=s=>Math.max(0,Math.min(255,Math.round(s))).toString(16).padStart(2,"0"),l=(s,t,r)=>`#${h(s)}${h(t)}${h(r)}`,f=(s,t,r)=>{if(t===0){const d=Math.round(r*255);return[d,d,d]}const e=(s%360+360)%360,o=(1-Math.abs(2*r-1))*t,u=o*(1-Math.abs(e/60%2-1)),c=r-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=s=>/^#?([0-9a-f]{3}|[0-9a-f]{6})$/i.test(s),x=s=>{const t=s.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 r=p.CSS_NAMED_COLOURS[t];if(r)return r};exports.expandHex=m;exports.hslToRgb=f;exports.isHex=b;exports.normaliseColour=x;exports.rgbToHex=l;
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,39 +1,42 @@
1
1
  import { CSS_NAMED_COLOURS as f } from "./named-colours.mjs";
2
- const l = (r) => {
3
- let t = Array.from(r), e = !1;
4
- return t[0] === "#" && (t.shift(), e = !0), t.length === 3 && (t = t.map((s) => `${s}${s}`)), `${e ? "#" : ""}${t.join("").toLowerCase()}`;
5
- }, m = (r) => Math.max(0, Math.min(255, Math.round(r))).toString(16).padStart(2, "0"), d = (r, t, e) => `#${m(r)}${m(t)}${m(e)}`, b = (r, t, e) => {
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) => {
6
9
  if (t === 0) {
7
- const u = Math.round(e * 255);
8
- return [u, u, u];
10
+ const h = Math.round(e * 255);
11
+ return [h, h, h];
9
12
  }
10
- const s = (r % 360 + 360) % 360, n = (1 - Math.abs(2 * e - 1)) * t, c = n * (1 - Math.abs(s / 60 % 2 - 1)), h = e - n / 2;
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;
11
14
  let o = 0, a = 0, i = 0;
12
- return s < 60 ? (o = n, a = c, i = 0) : s < 120 ? (o = c, a = n, i = 0) : s < 180 ? (o = 0, a = n, i = c) : s < 240 ? (o = 0, a = c, i = n) : s < 300 ? (o = c, a = 0, i = n) : (o = n, a = 0, i = c), [
13
- Math.round((o + h) * 255),
14
- Math.round((a + h) * 255),
15
- Math.round((i + h) * 255)
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)
16
19
  ];
17
- }, p = (r) => /^#?([0-9a-f]{3}|[0-9a-f]{6})$/i.test(r), M = (r) => {
18
- const t = r.trim().toLowerCase();
20
+ }, p = (s) => /^#?([0-9a-f]{3}|[0-9a-f]{6})$/i.test(s), M = (s) => {
21
+ const t = s.trim().toLowerCase();
19
22
  if (t.startsWith("#") || /^[0-9a-f]{3}([0-9a-f]{3})?$/.test(t))
20
23
  return p(t) ? l(t) : void 0;
21
24
  if (t.startsWith("rgb")) {
22
- const s = t.match(
25
+ const r = t.match(
23
26
  /rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*(?:,\s*[\d.]+\s*)?\)/
24
27
  );
25
- return s ? d(
26
- Number.parseInt(s[1], 10),
27
- Number.parseInt(s[2], 10),
28
- Number.parseInt(s[3], 10)
28
+ return r ? d(
29
+ Number.parseInt(r[1], 10),
30
+ Number.parseInt(r[2], 10),
31
+ Number.parseInt(r[3], 10)
29
32
  ) : void 0;
30
33
  }
31
34
  if (t.startsWith("hsl")) {
32
- const s = t.match(
35
+ const r = t.match(
33
36
  /hsla?\(\s*(\d+)\s*,\s*([\d.]+)%\s*,\s*([\d.]+)%\s*(?:,\s*[\d.]+\s*)?\)/
34
37
  );
35
- if (!s) return;
36
- const n = Number.parseInt(s[1], 10), c = Number.parseFloat(s[2]) / 100, h = Number.parseFloat(s[3]) / 100, [o, a, i] = b(n, c, h);
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);
37
40
  return d(o, a, i);
38
41
  }
39
42
  const e = f[t];
package/dist/index.d.ts CHANGED
@@ -94,6 +94,6 @@ export declare const normaliseColour: (value: string) => string | undefined;
94
94
  /**
95
95
  * Convert RGB channel values (0–255) to a `#rrggbb` hex string.
96
96
  */
97
- export declare const rgbToHex: (r: number, g: number, b: number) => string;
97
+ export declare const rgbToHex: (...args: [r: number, g: number, b: number] | [[number, number, number]]) => string;
98
98
 
99
99
  export { }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unmade/svg-helpers",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "Set of helper functions for working with SVG content",
5
5
  "type": "module",
6
6
  "main": "./dist/colours.cjs",