@react-fabric/utilities 1.0.0-beta.9 → 1.0.1
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/cjs/css/boundingBox.js +72 -0
- package/dist/cjs/css/cn.js +39 -0
- package/dist/cjs/css/imageColorset.js +96 -0
- package/dist/cjs/data/countries.js +98 -0
- package/dist/cjs/{_fileType.js → data/fileType.js} +37 -19
- package/dist/cjs/data/format.js +136 -0
- package/dist/cjs/helpers/debounce.js +63 -0
- package/dist/cjs/{useLogger.js → helpers/getLogger.js} +96 -96
- package/dist/cjs/helpers/lonlat.js +99 -0
- package/dist/cjs/index.js +99 -76
- package/dist/cjs/lists/dedupe.js +54 -0
- package/dist/cjs/lists/flatten.js +66 -0
- package/dist/cjs/{_getByPath.js → lists/getByPath.js} +58 -37
- package/dist/cjs/lists/groupBy.js +49 -0
- package/dist/cjs/matchers/compare.js +70 -0
- package/dist/cjs/matchers/isEmpty.js +34 -0
- package/dist/cjs/matchers/isEqual.js +82 -0
- package/dist/cjs/matchers/nullChecks.js +61 -0
- package/dist/cjs/matchers/typeChecks.js +127 -0
- package/dist/cjs/strings/ascii.js +105 -0
- package/dist/cjs/strings/hash.js +75 -0
- package/dist/cjs/strings/interpolate.js +47 -0
- package/dist/cjs/strings/matchString.js +54 -0
- package/dist/cjs/strings/template.js +48 -0
- package/dist/cjs/strings/tokenize.js +83 -0
- package/dist/cjs/utils/mergeRefs.js +56 -0
- package/dist/esm/css/boundingBox.js +71 -0
- package/dist/esm/css/cn.js +39 -0
- package/dist/esm/css/imageColorset.js +96 -0
- package/dist/esm/data/countries.js +98 -0
- package/dist/esm/{_fileType.js → data/fileType.js} +37 -19
- package/dist/esm/data/format.js +138 -0
- package/dist/esm/helpers/debounce.js +63 -0
- package/dist/esm/{useLogger.js → helpers/getLogger.js} +96 -96
- package/dist/esm/helpers/lonlat.js +97 -0
- package/dist/esm/index.js +54 -36
- package/dist/esm/lists/dedupe.js +54 -0
- package/dist/esm/lists/flatten.js +65 -0
- package/dist/esm/{_getByPath.js → lists/getByPath.js} +50 -29
- package/dist/esm/lists/groupBy.js +49 -0
- package/dist/esm/matchers/compare.js +70 -0
- package/dist/esm/matchers/isEmpty.js +34 -0
- package/dist/esm/matchers/isEqual.js +82 -0
- package/dist/esm/matchers/nullChecks.js +58 -0
- package/dist/esm/matchers/typeChecks.js +119 -0
- package/dist/esm/strings/ascii.js +105 -0
- package/dist/esm/strings/hash.js +73 -0
- package/dist/esm/strings/interpolate.js +47 -0
- package/dist/esm/strings/matchString.js +54 -0
- package/dist/esm/strings/template.js +48 -0
- package/dist/esm/strings/tokenize.js +82 -0
- package/dist/esm/utils/mergeRefs.js +56 -0
- package/dist/types/css/boundingBox.d.ts +46 -0
- package/dist/types/css/cn.d.ts +26 -0
- package/dist/types/css/imageColorset.d.ts +37 -0
- package/dist/types/data/countries.d.ts +136 -0
- package/dist/types/{_fileType.d.ts → data/fileType.d.ts} +41 -1
- package/dist/types/data/format.d.ts +122 -0
- package/dist/types/helpers/debounce.d.ts +38 -0
- package/dist/types/helpers/getLogger.d.ts +49 -0
- package/dist/types/helpers/lonlat.d.ts +48 -0
- package/dist/types/index.d.ts +54 -20
- package/dist/types/lists/dedupe.d.ts +36 -0
- package/dist/types/lists/flatten.d.ts +47 -0
- package/dist/types/lists/getByPath.d.ts +45 -0
- package/dist/types/lists/groupBy.d.ts +36 -0
- package/dist/types/matchers/compare.d.ts +38 -0
- package/dist/types/matchers/isEmpty.d.ts +38 -0
- package/dist/types/matchers/isEqual.d.ts +39 -0
- package/dist/types/matchers/nullChecks.d.ts +43 -0
- package/dist/types/matchers/typeChecks.d.ts +107 -0
- package/dist/types/strings/ascii.d.ts +34 -0
- package/dist/types/strings/hash.d.ts +46 -0
- package/dist/types/strings/interpolate.d.ts +34 -0
- package/dist/types/strings/matchString.d.ts +41 -0
- package/dist/types/strings/template.d.ts +35 -0
- package/dist/types/strings/tokenize.d.ts +44 -0
- package/dist/types/utils/mergeRefs.d.ts +36 -0
- package/package.json +13 -11
- package/src/css/boundingBox.ts +80 -0
- package/src/css/cn.ts +32 -0
- package/src/css/imageColorset.ts +97 -0
- package/src/data/countries.ts +126 -0
- package/src/{_fileType.ts → data/fileType.ts} +72 -89
- package/src/data/format.ts +192 -0
- package/src/helpers/debounce.ts +49 -0
- package/src/{useLogger.ts → helpers/getLogger.ts} +52 -101
- package/src/helpers/lonlat.ts +124 -0
- package/src/index.ts +57 -37
- package/src/lists/dedupe.ts +46 -0
- package/src/lists/flatten.ts +56 -0
- package/src/lists/getByPath.ts +122 -0
- package/src/lists/groupBy.ts +47 -0
- package/src/matchers/compare.ts +64 -0
- package/src/matchers/isEmpty.ts +54 -0
- package/src/matchers/isEqual.ts +71 -0
- package/src/matchers/nullChecks.ts +55 -0
- package/src/matchers/typeChecks.ts +146 -0
- package/src/strings/ascii.ts +100 -0
- package/src/strings/hash.ts +68 -0
- package/src/strings/interpolate.ts +42 -0
- package/src/strings/matchString.ts +49 -0
- package/src/strings/template.ts +43 -0
- package/src/strings/tokenize.ts +77 -0
- package/src/utils/mergeRefs.ts +47 -0
- package/src/{yup.ts → utils/yup.ts} +3 -3
- package/dist/cjs/_ascii.js +0 -76
- package/dist/cjs/_boundingBox.js +0 -59
- package/dist/cjs/_countries.js +0 -2805
- package/dist/cjs/_debounce.js +0 -53
- package/dist/cjs/_dedupe.js +0 -63
- package/dist/cjs/_fetch.js +0 -128
- package/dist/cjs/_format.js +0 -118
- package/dist/cjs/_hash.js +0 -67
- package/dist/cjs/_interpolate.js +0 -47
- package/dist/cjs/_isEqual.js +0 -108
- package/dist/cjs/_isType.js +0 -98
- package/dist/cjs/_mergeRefs.js +0 -40
- package/dist/cjs/_tokenize.js +0 -67
- package/dist/cjs/getImageColorset.js +0 -70
- package/dist/esm/_ascii.js +0 -76
- package/dist/esm/_boundingBox.js +0 -58
- package/dist/esm/_countries.js +0 -2805
- package/dist/esm/_debounce.js +0 -53
- package/dist/esm/_dedupe.js +0 -60
- package/dist/esm/_fetch.js +0 -128
- package/dist/esm/_format.js +0 -120
- package/dist/esm/_hash.js +0 -65
- package/dist/esm/_interpolate.js +0 -46
- package/dist/esm/_isEqual.js +0 -106
- package/dist/esm/_isType.js +0 -85
- package/dist/esm/_mergeRefs.js +0 -40
- package/dist/esm/_tokenize.js +0 -66
- package/dist/esm/getImageColorset.js +0 -70
- package/dist/types/_ascii.d.ts +0 -5
- package/dist/types/_boundingBox.d.ts +0 -10
- package/dist/types/_countries.d.ts +0 -85
- package/dist/types/_debounce.d.ts +0 -8
- package/dist/types/_dedupe.d.ts +0 -4
- package/dist/types/_fetch.d.ts +0 -23
- package/dist/types/_format.d.ts +0 -27
- package/dist/types/_getByPath.d.ts +0 -10
- package/dist/types/_hash.d.ts +0 -16
- package/dist/types/_interpolate.d.ts +0 -6
- package/dist/types/_isEqual.d.ts +0 -15
- package/dist/types/_isType.d.ts +0 -14
- package/dist/types/_mergeRefs.d.ts +0 -1
- package/dist/types/_tokenize.d.ts +0 -8
- package/dist/types/getImageColorset.d.ts +0 -1
- package/dist/types/useLogger.d.ts +0 -25
- package/src/_ascii.ts +0 -93
- package/src/_boundingBox.ts +0 -72
- package/src/_countries.ts +0 -3046
- package/src/_debounce.ts +0 -42
- package/src/_dedupe.ts +0 -56
- package/src/_fetch.ts +0 -109
- package/src/_format.ts +0 -141
- package/src/_getByPath.ts +0 -108
- package/src/_hash.ts +0 -62
- package/src/_interpolate.ts +0 -42
- package/src/_isEqual.ts +0 -102
- package/src/_isType.ts +0 -105
- package/src/_mergeRefs.ts +0 -33
- package/src/_tokenize.ts +0 -69
- package/src/getImageColorset.ts +0 -81
- /package/dist/cjs/{yup.js → utils/yup.js} +0 -0
- /package/dist/esm/{yup.js → utils/yup.js} +0 -0
- /package/dist/types/{yup.d.ts → utils/yup.d.ts} +0 -0
package/src/_isType.ts
DELETED
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* React Fabric
|
|
3
|
-
* @version: 1.0.0
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
* The MIT License (MIT)
|
|
7
|
-
* Copyright (c) 2024 Adarsh Pastakia
|
|
8
|
-
*
|
|
9
|
-
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
|
|
10
|
-
* and associated documentation files (the "Software"), to deal in the Software without restriction,
|
|
11
|
-
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
12
|
-
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
|
|
13
|
-
* subject to the following conditions:
|
|
14
|
-
*
|
|
15
|
-
* The above copyright notice and this permission notice shall be included in all copies or substantial
|
|
16
|
-
* portions of the Software.
|
|
17
|
-
*
|
|
18
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
|
19
|
-
* TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
20
|
-
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
21
|
-
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
22
|
-
*/
|
|
23
|
-
|
|
24
|
-
export const isUndefined = (value: AnyObject): value is undefined => {
|
|
25
|
-
return value === undefined;
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
export const isNull = (value: AnyObject): value is null => {
|
|
29
|
-
return value === null;
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
export const isNil = (val: AnyObject): val is undefined => {
|
|
33
|
-
return val === undefined || val === null;
|
|
34
|
-
};
|
|
35
|
-
export const isEmpty = (val: AnyObject): val is undefined => {
|
|
36
|
-
if (val instanceof Date) return false;
|
|
37
|
-
if (isNil(val) || val === "") {
|
|
38
|
-
return true;
|
|
39
|
-
}
|
|
40
|
-
if (Array.isArray(val) && val.filter(Boolean).length === 0) {
|
|
41
|
-
return true;
|
|
42
|
-
}
|
|
43
|
-
if (val instanceof Map || val instanceof Set) return val.size === 0;
|
|
44
|
-
if (["boolean", "number", "bigint", "function"].includes(typeof val)) {
|
|
45
|
-
return false;
|
|
46
|
-
}
|
|
47
|
-
return !!val && typeof val === "object" && Object.entries(val).length === 0;
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
export const isObject = (val: AnyObject): val is KeyValue => {
|
|
51
|
-
return (
|
|
52
|
-
!isNil(val) &&
|
|
53
|
-
!(val instanceof Date) &&
|
|
54
|
-
typeof val === "object" &&
|
|
55
|
-
!Array.isArray(val)
|
|
56
|
-
);
|
|
57
|
-
};
|
|
58
|
-
export const isArray = <T = AnyObject>(val: AnyObject): val is T[] => {
|
|
59
|
-
return Array.isArray(val);
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
export const isString = (value: AnyObject): value is string => {
|
|
63
|
-
return typeof value === "string";
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
export const isNumber = (value: AnyObject): value is number => {
|
|
67
|
-
return typeof value === "number";
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
export const isBoolean = (value: AnyObject): value is boolean => {
|
|
71
|
-
return typeof value === "boolean";
|
|
72
|
-
};
|
|
73
|
-
export const isTrue = (value: AnyObject = ""): value is true => {
|
|
74
|
-
return (
|
|
75
|
-
value === true ||
|
|
76
|
-
value === 1 ||
|
|
77
|
-
value?.toString().toLowerCase() === "yes" ||
|
|
78
|
-
value?.toString().toLowerCase() === "true"
|
|
79
|
-
);
|
|
80
|
-
};
|
|
81
|
-
export const isFalse = (value: AnyObject = ""): value is false => {
|
|
82
|
-
return (
|
|
83
|
-
value === false ||
|
|
84
|
-
value === 0 ||
|
|
85
|
-
value?.toString().toLowerCase() === "no" ||
|
|
86
|
-
value?.toString().toLowerCase() === "false"
|
|
87
|
-
);
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
export const isColor = (color: string) =>
|
|
91
|
-
color.startsWith?.("#") ||
|
|
92
|
-
color.startsWith?.("rgb") ||
|
|
93
|
-
color.startsWith?.("hsl") ||
|
|
94
|
-
color.startsWith?.("lab") ||
|
|
95
|
-
color === "transparent";
|
|
96
|
-
|
|
97
|
-
export const isSvgPath = (value: AnyObject): value is string => {
|
|
98
|
-
return (
|
|
99
|
-
typeof value === "string" && value.match(/^[Mm][\d.].*[\dzZ]$/) !== null
|
|
100
|
-
);
|
|
101
|
-
};
|
|
102
|
-
|
|
103
|
-
export const isRtl = () => {
|
|
104
|
-
return getComputedStyle(document.documentElement).direction === "rtl";
|
|
105
|
-
};
|
package/src/_mergeRefs.ts
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* React Fabric
|
|
3
|
-
* @version: 1.0.0
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
* The MIT License (MIT)
|
|
7
|
-
* Copyright (c) 2024 Adarsh Pastakia
|
|
8
|
-
*
|
|
9
|
-
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
|
|
10
|
-
* and associated documentation files (the "Software"), to deal in the Software without restriction,
|
|
11
|
-
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
12
|
-
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
|
|
13
|
-
* subject to the following conditions:
|
|
14
|
-
*
|
|
15
|
-
* The above copyright notice and this permission notice shall be included in all copies or substantial
|
|
16
|
-
* portions of the Software.
|
|
17
|
-
*
|
|
18
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
|
19
|
-
* TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
20
|
-
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
21
|
-
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
22
|
-
*/
|
|
23
|
-
|
|
24
|
-
/* istanbul ignore file */
|
|
25
|
-
|
|
26
|
-
export const mergeRefs =
|
|
27
|
-
(...refs: AnyObject[]) =>
|
|
28
|
-
(el: AnyObject) => {
|
|
29
|
-
refs.forEach((ref) => {
|
|
30
|
-
if (typeof ref === "function") ref(el);
|
|
31
|
-
if (ref && "current" in ref) ref.current = el;
|
|
32
|
-
});
|
|
33
|
-
};
|
package/src/_tokenize.ts
DELETED
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* React Fabric
|
|
3
|
-
* @version: 1.0.0
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
* The MIT License (MIT)
|
|
7
|
-
* Copyright (c) 2024 Adarsh Pastakia
|
|
8
|
-
*
|
|
9
|
-
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
|
|
10
|
-
* and associated documentation files (the "Software"), to deal in the Software without restriction,
|
|
11
|
-
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
12
|
-
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
|
|
13
|
-
* subject to the following conditions:
|
|
14
|
-
*
|
|
15
|
-
* The above copyright notice and this permission notice shall be included in all copies or substantial
|
|
16
|
-
* portions of the Software.
|
|
17
|
-
*
|
|
18
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
|
19
|
-
* TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
20
|
-
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
21
|
-
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
22
|
-
*/
|
|
23
|
-
|
|
24
|
-
import { isArray, isEmpty } from "./_isType";
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* split text into array of tokens by word boundaries
|
|
28
|
-
* @param str
|
|
29
|
-
* @param keywords
|
|
30
|
-
* @internal
|
|
31
|
-
*/
|
|
32
|
-
export const tokenize = (str: string, keywords?: string | string[]) => {
|
|
33
|
-
if (isEmpty(keywords)) {
|
|
34
|
-
return [[str, ""]];
|
|
35
|
-
}
|
|
36
|
-
const keys = isArray(keywords) ? keywords : [keywords];
|
|
37
|
-
const keyMap = keys.map((key) => {
|
|
38
|
-
// escape regex control characters
|
|
39
|
-
let escapedKey = key.replace(/([+*.?^$()[\]\\!&|\-=])/gm, "\\$1");
|
|
40
|
-
// place token within word boundaries if start/end with alpha-numeric
|
|
41
|
-
if (key.match(/^[a-z0-9]/i) != null) escapedKey = `\\b${escapedKey}`;
|
|
42
|
-
if (key.match(/[a-z0-9]$/i) != null) escapedKey = `${escapedKey}\\b`;
|
|
43
|
-
return escapedKey;
|
|
44
|
-
});
|
|
45
|
-
const regx = new RegExp(`(${keyMap.join("|")})`, "img");
|
|
46
|
-
let match;
|
|
47
|
-
let lastIndex = 0;
|
|
48
|
-
const tokens = [];
|
|
49
|
-
while ((match = regx.exec(str)) !== null) {
|
|
50
|
-
tokens.push([
|
|
51
|
-
str.substring(lastIndex, match.index),
|
|
52
|
-
str.substring(match.index, regx.lastIndex),
|
|
53
|
-
]);
|
|
54
|
-
lastIndex = regx.lastIndex;
|
|
55
|
-
}
|
|
56
|
-
tokens.push([str.substring(lastIndex), ""]);
|
|
57
|
-
return tokens;
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
export const iconToken = (str: string) => {
|
|
61
|
-
const split = str.trim().split(" ");
|
|
62
|
-
const first = split[0];
|
|
63
|
-
const last = split.length > 1 && split.pop();
|
|
64
|
-
return (
|
|
65
|
-
first && last
|
|
66
|
-
? `${first.charAt(0)}${last.charAt(0)}`
|
|
67
|
-
: `${first.substring(0, 2)}`
|
|
68
|
-
).toUpperCase();
|
|
69
|
-
};
|
package/src/getImageColorset.ts
DELETED
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* React Fabric
|
|
3
|
-
* @version: 1.0.0
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
* The MIT License (MIT)
|
|
7
|
-
* Copyright (c) 2024 Adarsh Pastakia
|
|
8
|
-
*
|
|
9
|
-
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
|
|
10
|
-
* and associated documentation files (the "Software"), to deal in the Software without restriction,
|
|
11
|
-
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
12
|
-
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
|
|
13
|
-
* subject to the following conditions:
|
|
14
|
-
*
|
|
15
|
-
* The above copyright notice and this permission notice shall be included in all copies or substantial
|
|
16
|
-
* portions of the Software.
|
|
17
|
-
*
|
|
18
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
|
19
|
-
* TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
20
|
-
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
21
|
-
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
22
|
-
*/
|
|
23
|
-
|
|
24
|
-
/* istanbul ignore file */
|
|
25
|
-
|
|
26
|
-
export const getImageColorset = (el: HTMLImageElement) => {
|
|
27
|
-
// create canvas
|
|
28
|
-
const canvas = document.createElement("canvas");
|
|
29
|
-
canvas.width = Math.min(500, el.naturalWidth);
|
|
30
|
-
canvas.height = Math.min(500, el.naturalHeight);
|
|
31
|
-
|
|
32
|
-
const ctx = canvas.getContext("2d");
|
|
33
|
-
if (ctx != null) {
|
|
34
|
-
ctx.drawImage(
|
|
35
|
-
el,
|
|
36
|
-
0,
|
|
37
|
-
0,
|
|
38
|
-
el.naturalWidth,
|
|
39
|
-
el.naturalHeight,
|
|
40
|
-
0,
|
|
41
|
-
0,
|
|
42
|
-
canvas.width,
|
|
43
|
-
canvas.height,
|
|
44
|
-
);
|
|
45
|
-
|
|
46
|
-
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
|
|
47
|
-
const data = imageData.data;
|
|
48
|
-
let r, g, b;
|
|
49
|
-
let dark = 0;
|
|
50
|
-
let light = 0;
|
|
51
|
-
let totalPixels = 0;
|
|
52
|
-
let transparent = data?.[3] === 0;
|
|
53
|
-
|
|
54
|
-
for (let x = 0, len = data.length; x < len; x += 4) {
|
|
55
|
-
if (data[x + 3] === 0) {
|
|
56
|
-
transparent = true;
|
|
57
|
-
continue;
|
|
58
|
-
}
|
|
59
|
-
if (transparent && data[x - 1] === 1) {
|
|
60
|
-
continue;
|
|
61
|
-
}
|
|
62
|
-
r = data[x];
|
|
63
|
-
g = data[x + 1];
|
|
64
|
-
b = data[x + 2];
|
|
65
|
-
totalPixels++;
|
|
66
|
-
0.2126 * r + 0.7152 * g + 0.0722 * b < 128 ? dark++ : light++;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
const diff = light / totalPixels - dark / totalPixels;
|
|
70
|
-
let ret = diff > 0 ? "light" : "dark";
|
|
71
|
-
if (transparent) {
|
|
72
|
-
ret += "_transparent";
|
|
73
|
-
}
|
|
74
|
-
return ret;
|
|
75
|
-
}
|
|
76
|
-
return "light";
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
if (typeof window !== "undefined")
|
|
80
|
-
// @ts-expect-error ignore
|
|
81
|
-
window.getImageColorset = getImageColorset;
|
|
File without changes
|
|
File without changes
|
|
File without changes
|