@nebulaaa/color 1.0.0 → 1.1.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/dist/color.cjs CHANGED
@@ -107,15 +107,15 @@ function Hex2Rgb(hex, options = {}) {
107
107
  const green = (number >> 8) & 255;
108
108
  const blue = number & 255;
109
109
  // NaN 的 typeof 也是 'number',须显式排除,否则 css 格式会产出 'rgb(... / NaN%)'
110
- const alpha = typeof options.alpha === 'number' && !Number.isNaN(options.alpha) ? options.alpha : alphaFromHex;
111
- if (options.format === 'array') {
110
+ const alpha = typeof options.Alpha === 'number' && !Number.isNaN(options.Alpha) ? options.Alpha : alphaFromHex;
111
+ if (options.Format === 'array') {
112
112
  return [red, green, blue, alpha];
113
113
  }
114
- if (options.format === 'css') {
114
+ if (options.Format === 'css') {
115
115
  const alphaString = alpha === 1 ? '' : ` / ${Number((alpha * 100).toFixed(2))}%`;
116
116
  return `rgb(${red} ${green} ${blue}${alphaString})`;
117
117
  }
118
- return { red, green, blue, alpha };
118
+ return { R: red, G: green, B: blue, A: alpha };
119
119
  }
120
120
  /**
121
121
  Convert RGB(A) color to HEX.
package/dist/color.d.ts CHANGED
@@ -6,7 +6,7 @@ interface RgbaOptions {
6
6
 
7
7
  @default 'object'
8
8
  */
9
- readonly format?: 'object' | 'array' | 'css';
9
+ readonly Format?: 'object' | 'array' | 'css';
10
10
  /**
11
11
  Set the alpha of the color.
12
12
 
@@ -14,15 +14,15 @@ interface RgbaOptions {
14
14
 
15
15
  The number must be in the range 0 to 1.
16
16
  */
17
- readonly alpha?: number;
17
+ readonly Alpha?: number;
18
18
  }
19
19
  interface RgbaObject {
20
- red: number;
21
- green: number;
22
- blue: number;
23
- alpha: number;
20
+ R: number;
21
+ G: number;
22
+ B: number;
23
+ A: number;
24
24
  }
25
- type RgbaTuple = [red: number, green: number, blue: number, alpha: number];
25
+ type RgbaTuple = [R: number, G: number, B: number, A: number];
26
26
 
27
27
  /**
28
28
  Convert HEX color to RGBA.
@@ -1 +1 @@
1
- "use strict";const e="a-f\\d",r=`#?[${e}]{3}[${e}]?`,t=`#?[${e}]{6}([${e}]{2})?`,n=new RegExp(`[^#${e}]`,"i"),o=new RegExp(`^${r}$|^${t}$`,"i");exports.Hex2Rgb=function(e,r={}){if("string"!=typeof e||n.test(e)||!o.test(e))throw new TypeError("Expected a valid hex string");if(null===r||"object"!=typeof r)throw new TypeError("Expected options as an object");let t=1;8===(e=e.replace(/^#/,"")).length&&(t=Number.parseInt(e.slice(6,8),16)/255,e=e.slice(0,6)),4===e.length&&(t=Number.parseInt(e.slice(3,4).repeat(2),16)/255,e=e.slice(0,3)),3===e.length&&(e=e[0]+e[0]+e[1]+e[1]+e[2]+e[2]);const i=Number.parseInt(e,16),a=i>>16,s=i>>8&255,p=255&i,l="number"!=typeof r.alpha||Number.isNaN(r.alpha)?t:r.alpha;if("array"===r.format)return[a,s,p,l];if("css"===r.format){return`rgb(${a} ${s} ${p}${1===l?"":` / ${Number((100*l).toFixed(2))}%`})`}return{red:a,green:s,blue:p,alpha:l}},exports.IsValidChannel=function(e){return!(!Number.isInteger(e)||e<0||e>255)},exports.Rgb2Hex=function(e,r,t,n){let o=`${e}${null!=n?n:""}`.includes("%");if("string"!=typeof e||r)void 0!==n&&(n=Number.parseFloat(String(n)));else{const i=(e=>{const r=e.replace(/rgba?\(([^)]+)\)/,"$1").split(/[,\s/]+/).filter(Boolean);if(r.length<3)return;const t=(e,r)=>{if((e=e.trim()).endsWith("%")){const t=Number.parseFloat(e);if(!Number.isFinite(t))throw new TypeError("Invalid or unsupported color format.");return Math.min(t*r/100,r)}const t=Number.parseFloat(e);if(!Number.isFinite(t))throw new TypeError("Invalid or unsupported color format.");return Math.min(t,r)},n=t(r[0],255),o=t(r[1],255),i=t(r[2],255);let a;return 4===r.length&&(a=t(r[3],1)),[n,o,i,a]})(e);if(!i)throw new TypeError("Invalid or unsupported color format.");o=!1,[e,r,t,n]=i}const i=e=>!Number.isInteger(e)||e<0||e>255;if("number"!=typeof e||"number"!=typeof r||"number"!=typeof t||i(e)||i(r)||i(t))throw new TypeError("Expected three numbers between 0 and 255");if("number"==typeof n){if(!o&&n>=0&&n<=1)n=Math.round(255*n);else{if(!(o&&n>=0&&n<=100))throw new TypeError(`Expected alpha value (${n}) as a fraction or percentage`);n=Math.round(255*n/100)}n=(256|n).toString(16).slice(1)}else n="";return((e,r,t,n)=>(t|r<<8|e<<16|1<<24).toString(16).slice(1)+n)(e,r,t,n)};
1
+ "use strict";const e="a-f\\d",r=`#?[${e}]{3}[${e}]?`,t=`#?[${e}]{6}([${e}]{2})?`,n=new RegExp(`[^#${e}]`,"i"),o=new RegExp(`^${r}$|^${t}$`,"i");exports.Hex2Rgb=function(e,r={}){if("string"!=typeof e||n.test(e)||!o.test(e))throw new TypeError("Expected a valid hex string");if(null===r||"object"!=typeof r)throw new TypeError("Expected options as an object");let t=1;8===(e=e.replace(/^#/,"")).length&&(t=Number.parseInt(e.slice(6,8),16)/255,e=e.slice(0,6)),4===e.length&&(t=Number.parseInt(e.slice(3,4).repeat(2),16)/255,e=e.slice(0,3)),3===e.length&&(e=e[0]+e[0]+e[1]+e[1]+e[2]+e[2]);const i=Number.parseInt(e,16),s=i>>16,a=i>>8&255,p=255&i,l="number"!=typeof r.Alpha||Number.isNaN(r.Alpha)?t:r.Alpha;if("array"===r.Format)return[s,a,p,l];if("css"===r.Format){return`rgb(${s} ${a} ${p}${1===l?"":` / ${Number((100*l).toFixed(2))}%`})`}return{R:s,G:a,B:p,A:l}},exports.IsValidChannel=function(e){return!(!Number.isInteger(e)||e<0||e>255)},exports.Rgb2Hex=function(e,r,t,n){let o=`${e}${null!=n?n:""}`.includes("%");if("string"!=typeof e||r)void 0!==n&&(n=Number.parseFloat(String(n)));else{const i=(e=>{const r=e.replace(/rgba?\(([^)]+)\)/,"$1").split(/[,\s/]+/).filter(Boolean);if(r.length<3)return;const t=(e,r)=>{if((e=e.trim()).endsWith("%")){const t=Number.parseFloat(e);if(!Number.isFinite(t))throw new TypeError("Invalid or unsupported color format.");return Math.min(t*r/100,r)}const t=Number.parseFloat(e);if(!Number.isFinite(t))throw new TypeError("Invalid or unsupported color format.");return Math.min(t,r)},n=t(r[0],255),o=t(r[1],255),i=t(r[2],255);let s;return 4===r.length&&(s=t(r[3],1)),[n,o,i,s]})(e);if(!i)throw new TypeError("Invalid or unsupported color format.");o=!1,[e,r,t,n]=i}const i=e=>!Number.isInteger(e)||e<0||e>255;if("number"!=typeof e||"number"!=typeof r||"number"!=typeof t||i(e)||i(r)||i(t))throw new TypeError("Expected three numbers between 0 and 255");if("number"==typeof n){if(!o&&n>=0&&n<=1)n=Math.round(255*n);else{if(!(o&&n>=0&&n<=100))throw new TypeError(`Expected alpha value (${n}) as a fraction or percentage`);n=Math.round(255*n/100)}n=(256|n).toString(16).slice(1)}else n="";return((e,r,t,n)=>(t|r<<8|e<<16|1<<24).toString(16).slice(1)+n)(e,r,t,n)};
@@ -1 +1 @@
1
- const e="a-f\\d",r=`#?[${e}]{3}[${e}]?`,t=`#?[${e}]{6}([${e}]{2})?`,n=new RegExp(`[^#${e}]`,"i"),o=new RegExp(`^${r}$|^${t}$`,"i");function i(e,r={}){if("string"!=typeof e||n.test(e)||!o.test(e))throw new TypeError("Expected a valid hex string");if(null===r||"object"!=typeof r)throw new TypeError("Expected options as an object");let t=1;8===(e=e.replace(/^#/,"")).length&&(t=Number.parseInt(e.slice(6,8),16)/255,e=e.slice(0,6)),4===e.length&&(t=Number.parseInt(e.slice(3,4).repeat(2),16)/255,e=e.slice(0,3)),3===e.length&&(e=e[0]+e[0]+e[1]+e[1]+e[2]+e[2]);const i=Number.parseInt(e,16),a=i>>16,s=i>>8&255,p=255&i,l="number"!=typeof r.alpha||Number.isNaN(r.alpha)?t:r.alpha;if("array"===r.format)return[a,s,p,l];if("css"===r.format){return`rgb(${a} ${s} ${p}${1===l?"":` / ${Number((100*l).toFixed(2))}%`})`}return{red:a,green:s,blue:p,alpha:l}}function a(e,r,t,n){let o=`${e}${null!=n?n:""}`.includes("%");if("string"!=typeof e||r)void 0!==n&&(n=Number.parseFloat(String(n)));else{const i=(e=>{const r=e.replace(/rgba?\(([^)]+)\)/,"$1").split(/[,\s/]+/).filter(Boolean);if(r.length<3)return;const t=(e,r)=>{if((e=e.trim()).endsWith("%")){const t=Number.parseFloat(e);if(!Number.isFinite(t))throw new TypeError("Invalid or unsupported color format.");return Math.min(t*r/100,r)}const t=Number.parseFloat(e);if(!Number.isFinite(t))throw new TypeError("Invalid or unsupported color format.");return Math.min(t,r)},n=t(r[0],255),o=t(r[1],255),i=t(r[2],255);let a;return 4===r.length&&(a=t(r[3],1)),[n,o,i,a]})(e);if(!i)throw new TypeError("Invalid or unsupported color format.");o=!1,[e,r,t,n]=i}const i=e=>!Number.isInteger(e)||e<0||e>255;if("number"!=typeof e||"number"!=typeof r||"number"!=typeof t||i(e)||i(r)||i(t))throw new TypeError("Expected three numbers between 0 and 255");if("number"==typeof n){if(!o&&n>=0&&n<=1)n=Math.round(255*n);else{if(!(o&&n>=0&&n<=100))throw new TypeError(`Expected alpha value (${n}) as a fraction or percentage`);n=Math.round(255*n/100)}n=(256|n).toString(16).slice(1)}else n="";return((e,r,t,n)=>(t|r<<8|e<<16|1<<24).toString(16).slice(1)+n)(e,r,t,n)}function s(e){return!(!Number.isInteger(e)||e<0||e>255)}export{i as Hex2Rgb,s as IsValidChannel,a as Rgb2Hex};
1
+ const e="a-f\\d",r=`#?[${e}]{3}[${e}]?`,t=`#?[${e}]{6}([${e}]{2})?`,n=new RegExp(`[^#${e}]`,"i"),o=new RegExp(`^${r}$|^${t}$`,"i");function i(e,r={}){if("string"!=typeof e||n.test(e)||!o.test(e))throw new TypeError("Expected a valid hex string");if(null===r||"object"!=typeof r)throw new TypeError("Expected options as an object");let t=1;8===(e=e.replace(/^#/,"")).length&&(t=Number.parseInt(e.slice(6,8),16)/255,e=e.slice(0,6)),4===e.length&&(t=Number.parseInt(e.slice(3,4).repeat(2),16)/255,e=e.slice(0,3)),3===e.length&&(e=e[0]+e[0]+e[1]+e[1]+e[2]+e[2]);const i=Number.parseInt(e,16),s=i>>16,a=i>>8&255,p=255&i,l="number"!=typeof r.Alpha||Number.isNaN(r.Alpha)?t:r.Alpha;if("array"===r.Format)return[s,a,p,l];if("css"===r.Format){return`rgb(${s} ${a} ${p}${1===l?"":` / ${Number((100*l).toFixed(2))}%`})`}return{R:s,G:a,B:p,A:l}}function s(e,r,t,n){let o=`${e}${null!=n?n:""}`.includes("%");if("string"!=typeof e||r)void 0!==n&&(n=Number.parseFloat(String(n)));else{const i=(e=>{const r=e.replace(/rgba?\(([^)]+)\)/,"$1").split(/[,\s/]+/).filter(Boolean);if(r.length<3)return;const t=(e,r)=>{if((e=e.trim()).endsWith("%")){const t=Number.parseFloat(e);if(!Number.isFinite(t))throw new TypeError("Invalid or unsupported color format.");return Math.min(t*r/100,r)}const t=Number.parseFloat(e);if(!Number.isFinite(t))throw new TypeError("Invalid or unsupported color format.");return Math.min(t,r)},n=t(r[0],255),o=t(r[1],255),i=t(r[2],255);let s;return 4===r.length&&(s=t(r[3],1)),[n,o,i,s]})(e);if(!i)throw new TypeError("Invalid or unsupported color format.");o=!1,[e,r,t,n]=i}const i=e=>!Number.isInteger(e)||e<0||e>255;if("number"!=typeof e||"number"!=typeof r||"number"!=typeof t||i(e)||i(r)||i(t))throw new TypeError("Expected three numbers between 0 and 255");if("number"==typeof n){if(!o&&n>=0&&n<=1)n=Math.round(255*n);else{if(!(o&&n>=0&&n<=100))throw new TypeError(`Expected alpha value (${n}) as a fraction or percentage`);n=Math.round(255*n/100)}n=(256|n).toString(16).slice(1)}else n="";return((e,r,t,n)=>(t|r<<8|e<<16|1<<24).toString(16).slice(1)+n)(e,r,t,n)}function a(e){return!(!Number.isInteger(e)||e<0||e>255)}export{i as Hex2Rgb,a as IsValidChannel,s as Rgb2Hex};
package/dist/color.mjs CHANGED
@@ -105,15 +105,15 @@ function Hex2Rgb(hex, options = {}) {
105
105
  const green = (number >> 8) & 255;
106
106
  const blue = number & 255;
107
107
  // NaN 的 typeof 也是 'number',须显式排除,否则 css 格式会产出 'rgb(... / NaN%)'
108
- const alpha = typeof options.alpha === 'number' && !Number.isNaN(options.alpha) ? options.alpha : alphaFromHex;
109
- if (options.format === 'array') {
108
+ const alpha = typeof options.Alpha === 'number' && !Number.isNaN(options.Alpha) ? options.Alpha : alphaFromHex;
109
+ if (options.Format === 'array') {
110
110
  return [red, green, blue, alpha];
111
111
  }
112
- if (options.format === 'css') {
112
+ if (options.Format === 'css') {
113
113
  const alphaString = alpha === 1 ? '' : ` / ${Number((alpha * 100).toFixed(2))}%`;
114
114
  return `rgb(${red} ${green} ${blue}${alphaString})`;
115
115
  }
116
- return { red, green, blue, alpha };
116
+ return { R: red, G: green, B: blue, A: alpha };
117
117
  }
118
118
  /**
119
119
  Convert RGB(A) color to HEX.
package/dist/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nebulaaa/color",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "颜色转换扩展包",
5
5
  "author": "doooreyn",
6
6
  "license": "MIT",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nebulaaa/color",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "颜色转换扩展包",
5
5
  "main": "./dist/color.cjs",
6
6
  "module": "./dist/color.mjs",