@cloudcome/utils-core 1.1.1 → 1.2.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.
Files changed (291) hide show
  1. package/dist/array.cjs +129 -0
  2. package/dist/array.cjs.map +1 -0
  3. package/dist/array.d.ts +171 -0
  4. package/dist/array.mjs +129 -0
  5. package/dist/array.mjs.map +1 -0
  6. package/dist/async.cjs +219 -0
  7. package/dist/async.cjs.map +1 -0
  8. package/dist/async.d.ts +137 -0
  9. package/dist/async.mjs +219 -0
  10. package/dist/async.mjs.map +1 -0
  11. package/dist/base64.cjs +16 -0
  12. package/dist/base64.cjs.map +1 -0
  13. package/dist/base64.d.ts +7 -0
  14. package/dist/base64.mjs +16 -0
  15. package/dist/base64.mjs.map +1 -0
  16. package/dist/cache.cjs +79 -0
  17. package/dist/cache.cjs.map +1 -0
  18. package/dist/cache.d.ts +90 -0
  19. package/dist/cache.mjs +79 -0
  20. package/dist/cache.mjs.map +1 -0
  21. package/{src/color/contrast.ts → dist/color/contrast.d.ts} +2 -12
  22. package/dist/color/distance.d.ts +8 -0
  23. package/dist/color/helpers.d.ts +2 -0
  24. package/dist/color/hex-hsl.d.ts +3 -0
  25. package/{src/color/hex-hsv.ts → dist/color/hex-hsv.d.ts} +3 -11
  26. package/{src/color/hex-hwb.ts → dist/color/hex-hwb.d.ts} +3 -11
  27. package/dist/color/hex-rgb.d.ts +18 -0
  28. package/{src/color/hsl-lighten.ts → dist/color/hsl-lighten.d.ts} +2 -7
  29. package/{src/color/hsv-brighten.ts → dist/color/hsv-brighten.d.ts} +2 -7
  30. package/{src/color/luminance.ts → dist/color/luminance.d.ts} +2 -9
  31. package/{src/color/mix.ts → dist/color/mix.d.ts} +2 -10
  32. package/dist/color/rgb-hsl.d.ts +23 -0
  33. package/{src/color/rgb-hsv.ts → dist/color/rgb-hsv.d.ts} +3 -30
  34. package/dist/color/rgb-hwb.d.ts +29 -0
  35. package/{src/color/rgb-lab.ts → dist/color/rgb-lab.d.ts} +3 -11
  36. package/dist/color/rgb-whiter.d.ts +12 -0
  37. package/dist/color/rgb-xyz.d.ts +22 -0
  38. package/{src/color/types.ts → dist/color/types.d.ts} +30 -12
  39. package/{src/color/xyz-lab.ts → dist/color/xyz-lab.d.ts} +3 -32
  40. package/dist/color.cjs +250 -0
  41. package/dist/color.cjs.map +1 -0
  42. package/dist/color.mjs +250 -0
  43. package/dist/color.mjs.map +1 -0
  44. package/dist/const.cjs +14 -0
  45. package/dist/const.cjs.map +1 -0
  46. package/dist/const.mjs +15 -0
  47. package/dist/const.mjs.map +1 -0
  48. package/dist/core.cjs +250 -0
  49. package/dist/core.cjs.map +1 -0
  50. package/dist/core.mjs +251 -0
  51. package/dist/core.mjs.map +1 -0
  52. package/dist/crypto/md5.d.mts +1 -0
  53. package/dist/crypto/sha1.d.mts +1 -0
  54. package/dist/crypto/sha256.d.mts +1 -0
  55. package/dist/crypto/sha512.d.mts +1 -0
  56. package/dist/crypto.cjs +812 -0
  57. package/dist/crypto.cjs.map +1 -0
  58. package/{src/crypto.ts → dist/crypto.d.ts} +4 -20
  59. package/dist/crypto.mjs +812 -0
  60. package/dist/crypto.mjs.map +1 -0
  61. package/dist/date/const.d.ts +6 -0
  62. package/dist/date/core.d.ts +52 -0
  63. package/dist/date/days.d.ts +23 -0
  64. package/{src/date/is.ts → dist/date/is.d.ts} +8 -102
  65. package/dist/date/relative.d.ts +44 -0
  66. package/dist/date/start-end.d.ts +73 -0
  67. package/dist/date/timezone.d.ts +67 -0
  68. package/dist/date/weeks.d.ts +72 -0
  69. package/dist/date.cjs +239 -0
  70. package/dist/date.cjs.map +1 -0
  71. package/dist/date.mjs +241 -0
  72. package/dist/date.mjs.map +1 -0
  73. package/dist/dict.cjs +2 -0
  74. package/dist/dict.cjs.map +1 -0
  75. package/dist/dict.mjs +2 -0
  76. package/dist/dict.mjs.map +1 -0
  77. package/dist/each.cjs +18 -0
  78. package/dist/each.cjs.map +1 -0
  79. package/dist/each.mjs +19 -0
  80. package/dist/each.mjs.map +1 -0
  81. package/dist/easing.cjs +151 -0
  82. package/dist/easing.cjs.map +1 -0
  83. package/dist/easing.d.ts +46 -0
  84. package/dist/easing.mjs +151 -0
  85. package/dist/easing.mjs.map +1 -0
  86. package/dist/emitter.cjs +94 -0
  87. package/dist/emitter.cjs.map +1 -0
  88. package/dist/emitter.d.ts +68 -0
  89. package/dist/emitter.mjs +94 -0
  90. package/dist/emitter.mjs.map +1 -0
  91. package/dist/enum.cjs +58 -0
  92. package/dist/enum.cjs.map +1 -0
  93. package/dist/enum.d.ts +68 -0
  94. package/dist/enum.mjs +58 -0
  95. package/dist/enum.mjs.map +1 -0
  96. package/dist/env.cjs +28 -0
  97. package/dist/env.cjs.map +1 -0
  98. package/{src/env.ts → dist/env.d.ts} +6 -30
  99. package/dist/env.mjs +28 -0
  100. package/dist/env.mjs.map +1 -0
  101. package/dist/error.cjs +12 -0
  102. package/dist/error.cjs.map +1 -0
  103. package/{src/error.ts → dist/error.d.ts} +3 -12
  104. package/dist/error.mjs +12 -0
  105. package/dist/error.mjs.map +1 -0
  106. package/dist/exception.cjs +22 -0
  107. package/dist/exception.cjs.map +1 -0
  108. package/dist/exception.d.ts +31 -0
  109. package/dist/exception.mjs +22 -0
  110. package/dist/exception.mjs.map +1 -0
  111. package/dist/fn.cjs +76 -0
  112. package/dist/fn.cjs.map +1 -0
  113. package/dist/fn.d.ts +102 -0
  114. package/dist/fn.mjs +76 -0
  115. package/dist/fn.mjs.map +1 -0
  116. package/dist/index.cjs +5 -0
  117. package/dist/index.cjs.map +1 -0
  118. package/dist/index.d.ts +1 -0
  119. package/dist/index.mjs +5 -0
  120. package/dist/index.mjs.map +1 -0
  121. package/dist/merge.cjs +87 -0
  122. package/dist/merge.cjs.map +1 -0
  123. package/dist/merge.mjs +88 -0
  124. package/dist/merge.mjs.map +1 -0
  125. package/dist/number.cjs +14 -0
  126. package/dist/number.cjs.map +1 -0
  127. package/dist/number.d.ts +153 -0
  128. package/dist/number.mjs +14 -0
  129. package/dist/number.mjs.map +1 -0
  130. package/{src/object/each.ts → dist/object/each.d.ts} +3 -23
  131. package/dist/object/get-set.d.ts +111 -0
  132. package/dist/object/is.d.ts +32 -0
  133. package/dist/object/merge.d.ts +72 -0
  134. package/{src/object/process.ts → dist/object/process.d.ts} +4 -38
  135. package/dist/object.cjs +130 -0
  136. package/dist/object.cjs.map +1 -0
  137. package/dist/object.mjs +130 -0
  138. package/dist/object.mjs.map +1 -0
  139. package/dist/path.cjs +77 -0
  140. package/dist/path.cjs.map +1 -0
  141. package/dist/path.d.ts +82 -0
  142. package/dist/path.mjs +77 -0
  143. package/dist/path.mjs.map +1 -0
  144. package/dist/promise.cjs +62 -0
  145. package/dist/promise.cjs.map +1 -0
  146. package/{src/promise.ts → dist/promise.d.ts} +6 -67
  147. package/dist/promise.mjs +62 -0
  148. package/dist/promise.mjs.map +1 -0
  149. package/dist/qs.cjs +47 -0
  150. package/dist/qs.cjs.map +1 -0
  151. package/{src/qs.ts → dist/qs.d.ts} +3 -60
  152. package/dist/qs.mjs +47 -0
  153. package/dist/qs.mjs.map +1 -0
  154. package/dist/regexp.cjs +66 -0
  155. package/dist/regexp.cjs.map +1 -0
  156. package/dist/regexp.d.ts +65 -0
  157. package/dist/regexp.mjs +66 -0
  158. package/dist/regexp.mjs.map +1 -0
  159. package/dist/string.cjs +16 -0
  160. package/dist/string.cjs.map +1 -0
  161. package/dist/string.d.ts +80 -0
  162. package/dist/string.mjs +16 -0
  163. package/dist/string.mjs.map +1 -0
  164. package/dist/string2.cjs +157 -0
  165. package/dist/string2.cjs.map +1 -0
  166. package/dist/string2.mjs +158 -0
  167. package/dist/string2.mjs.map +1 -0
  168. package/dist/time/from.d.ts +14 -0
  169. package/dist/time/to.d.ts +38 -0
  170. package/dist/time.cjs +82 -0
  171. package/dist/time.cjs.map +1 -0
  172. package/dist/time.mjs +82 -0
  173. package/dist/time.mjs.map +1 -0
  174. package/dist/timer.cjs +119 -0
  175. package/dist/timer.cjs.map +1 -0
  176. package/dist/timer.d.ts +96 -0
  177. package/{src/timer.ts → dist/timer.mjs} +17 -124
  178. package/dist/timer.mjs.map +1 -0
  179. package/dist/tree.cjs +125 -0
  180. package/dist/tree.cjs.map +1 -0
  181. package/{src/tree.ts → dist/tree.d.ts} +41 -225
  182. package/dist/tree.mjs +125 -0
  183. package/dist/tree.mjs.map +1 -0
  184. package/dist/type.cjs +78 -0
  185. package/dist/type.cjs.map +1 -0
  186. package/{src/type.ts → dist/type.d.ts} +20 -96
  187. package/dist/type.mjs +78 -0
  188. package/dist/type.mjs.map +1 -0
  189. package/dist/types.cjs +2 -0
  190. package/dist/types.cjs.map +1 -0
  191. package/{src/types.ts → dist/types.d.ts} +12 -33
  192. package/dist/types.mjs +2 -0
  193. package/dist/types.mjs.map +1 -0
  194. package/dist/unique.cjs +46 -0
  195. package/dist/unique.cjs.map +1 -0
  196. package/dist/unique.d.ts +22 -0
  197. package/dist/unique.mjs +46 -0
  198. package/dist/unique.mjs.map +1 -0
  199. package/dist/url.cjs +37 -0
  200. package/dist/url.cjs.map +1 -0
  201. package/dist/url.d.ts +53 -0
  202. package/dist/url.mjs +37 -0
  203. package/dist/url.mjs.map +1 -0
  204. package/dist/version.cjs +33 -0
  205. package/dist/version.cjs.map +1 -0
  206. package/dist/version.d.ts +32 -0
  207. package/dist/version.mjs +33 -0
  208. package/dist/version.mjs.map +1 -0
  209. package/package.json +8 -2
  210. package/CHANGELOG.md +0 -52
  211. package/src/array.ts +0 -312
  212. package/src/async.ts +0 -379
  213. package/src/base64.ts +0 -20
  214. package/src/cache.ts +0 -146
  215. package/src/color/distance.ts +0 -28
  216. package/src/color/helpers.ts +0 -23
  217. package/src/color/hex-hsl.ts +0 -11
  218. package/src/color/hex-rgb.ts +0 -39
  219. package/src/color/rgb-hsl.ts +0 -53
  220. package/src/color/rgb-hwb.ts +0 -56
  221. package/src/color/rgb-whiter.ts +0 -22
  222. package/src/color/rgb-xyz.ts +0 -62
  223. package/src/crypto/md5.mjs +0 -357
  224. package/src/crypto/sha1.mjs +0 -300
  225. package/src/crypto/sha256.mjs +0 -310
  226. package/src/crypto/sha512.mjs +0 -459
  227. package/src/date/const.ts +0 -6
  228. package/src/date/core.ts +0 -162
  229. package/src/date/days.ts +0 -51
  230. package/src/date/relative.ts +0 -92
  231. package/src/date/start-end.ts +0 -246
  232. package/src/date/timezone.ts +0 -220
  233. package/src/date/weeks.ts +0 -100
  234. package/src/dts/global.d.ts +0 -27
  235. package/src/easing.ts +0 -166
  236. package/src/emitter.ts +0 -117
  237. package/src/enum.ts +0 -171
  238. package/src/exception.ts +0 -68
  239. package/src/fn.ts +0 -197
  240. package/src/index.ts +0 -1
  241. package/src/number.ts +0 -236
  242. package/src/object/get-set.ts +0 -273
  243. package/src/object/is.ts +0 -128
  244. package/src/object/merge.ts +0 -180
  245. package/src/path.ts +0 -188
  246. package/src/regexp.ts +0 -156
  247. package/src/string.ts +0 -146
  248. package/src/time/from.ts +0 -57
  249. package/src/time/to.ts +0 -106
  250. package/src/unique.ts +0 -77
  251. package/src/url.ts +0 -93
  252. package/src/version.ts +0 -71
  253. package/test/array.test.ts +0 -332
  254. package/test/async-real.test.ts +0 -39
  255. package/test/async.test.ts +0 -375
  256. package/test/base64.test.ts +0 -32
  257. package/test/cache.test.ts +0 -83
  258. package/test/color.test.ts +0 -163
  259. package/test/crypto.test.ts +0 -34
  260. package/test/date-tz.test.ts +0 -206
  261. package/test/date.test.ts +0 -353
  262. package/test/easing.test.ts +0 -33
  263. package/test/emitter.test.ts +0 -71
  264. package/test/enum.test.ts +0 -113
  265. package/test/env.test.ts +0 -69
  266. package/test/error.test.ts +0 -58
  267. package/test/exception.test.ts +0 -43
  268. package/test/fn.test.ts +0 -263
  269. package/test/helpers.ts +0 -23
  270. package/test/index.test.ts +0 -6
  271. package/test/number.test.ts +0 -213
  272. package/test/object.test.ts +0 -309
  273. package/test/path.test.ts +0 -156
  274. package/test/promise.test.ts +0 -199
  275. package/test/qs.test.ts +0 -79
  276. package/test/regexp.test.ts +0 -97
  277. package/test/string.test.ts +0 -150
  278. package/test/time.test.ts +0 -214
  279. package/test/timer.test.ts +0 -114
  280. package/test/tree.test.ts +0 -348
  281. package/test/type.test.ts +0 -226
  282. package/test/unique.test.ts +0 -71
  283. package/test/url.test.ts +0 -136
  284. package/test/version.test.ts +0 -52
  285. package/tsconfig.json +0 -31
  286. package/vite.config.mts +0 -114
  287. /package/{src/color.ts → dist/color.d.ts} +0 -0
  288. /package/{src/date.ts → dist/date.d.ts} +0 -0
  289. /package/{src/dict.ts → dist/dict.d.ts} +0 -0
  290. /package/{src/object.ts → dist/object.d.ts} +0 -0
  291. /package/{src/time.ts → dist/time.d.ts} +0 -0
@@ -0,0 +1,16 @@
1
+ function base64toBlob(base64) {
2
+ const byteString = atob(base64.split(",")[1]);
3
+ const mimeString = base64.split(",")[0].split(":")[1].split(";")[0];
4
+ const ab = new ArrayBuffer(byteString.length);
5
+ const ua = new Uint8Array(ab);
6
+ for (let i = 0; i < byteString.length; i++) {
7
+ ua[i] = byteString.charCodeAt(i);
8
+ }
9
+ return new Blob([ab], {
10
+ type: mimeString
11
+ });
12
+ }
13
+ export {
14
+ base64toBlob
15
+ };
16
+ //# sourceMappingURL=base64.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base64.mjs","sources":["../src/base64.ts"],"sourcesContent":["/**\n * base64 转换为 Blob 实例\n * @ref http://stackoverflow.com/q/18253378\n * @param base64 {String} base64 编码\n * @returns {Blob}\n */\nexport function base64toBlob(base64: string): Blob {\n const byteString = atob(base64.split(',')[1]);\n const mimeString = base64.split(',')[0].split(':')[1].split(';')[0];\n const ab = new ArrayBuffer(byteString.length);\n const ua = new Uint8Array(ab);\n\n for (let i = 0; i < byteString.length; i++) {\n ua[i] = byteString.charCodeAt(i);\n }\n\n return new Blob([ab], {\n type: mimeString,\n });\n}\n"],"names":[],"mappings":"AAMO,SAAS,aAAa,QAAsB;AACjD,QAAM,aAAa,KAAK,OAAO,MAAM,GAAG,EAAE,CAAC,CAAC;AAC5C,QAAM,aAAa,OAAO,MAAM,GAAG,EAAE,CAAC,EAAE,MAAM,GAAG,EAAE,CAAC,EAAE,MAAM,GAAG,EAAE,CAAC;AAClE,QAAM,KAAK,IAAI,YAAY,WAAW,MAAM;AACtC,QAAA,KAAK,IAAI,WAAW,EAAE;AAE5B,WAAS,IAAI,GAAG,IAAI,WAAW,QAAQ,KAAK;AAC1C,OAAG,CAAC,IAAI,WAAW,WAAW,CAAC;AAAA,EAAA;AAGjC,SAAO,IAAI,KAAK,CAAC,EAAE,GAAG;AAAA,IACpB,MAAM;AAAA,EAAA,CACP;AACH;"}
package/dist/cache.cjs ADDED
@@ -0,0 +1,79 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
4
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
5
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
6
+ const core = require("./core.cjs");
7
+ class AbstractCache {
8
+ isExpired(cached) {
9
+ return cached.expiredAt > 0 && Date.now() > cached.expiredAt;
10
+ }
11
+ normalizeCached(id, data, options) {
12
+ const { expiredAt = 0, maxAge = 0 } = options || {};
13
+ const now = Date.now();
14
+ return {
15
+ id,
16
+ data,
17
+ createdAt: now,
18
+ expiredAt: expiredAt ? core.dateParse(expiredAt).getTime() : maxAge > 0 ? now + maxAge : 0
19
+ };
20
+ }
21
+ /**
22
+ * 获取缓存项
23
+ * @param id 缓存项的唯一标识
24
+ * @returns 返回缓存项或 null
25
+ */
26
+ get(id) {
27
+ return null;
28
+ }
29
+ /**
30
+ * 设置缓存项
31
+ * @param id 缓存项的唯一标识
32
+ * @param data 要缓存的数据
33
+ * @param options 缓存选项
34
+ */
35
+ set(id, data, options) {
36
+ }
37
+ /**
38
+ * 删除缓存项
39
+ * @param id 缓存项的唯一标识
40
+ */
41
+ del(id) {
42
+ }
43
+ /**
44
+ * 清空缓存
45
+ */
46
+ clear() {
47
+ }
48
+ }
49
+ class MemoryCache extends AbstractCache {
50
+ constructor() {
51
+ super(...arguments);
52
+ __publicField(this, "cache", /* @__PURE__ */ new Map());
53
+ }
54
+ get(id) {
55
+ const cached = this.cache.get(id);
56
+ if (!cached) return null;
57
+ if (this.isExpired(cached)) {
58
+ this.del(id);
59
+ return null;
60
+ }
61
+ return cached;
62
+ }
63
+ set(id, data, options) {
64
+ this.cache.set(id, this.normalizeCached(id, data, options));
65
+ }
66
+ del(id) {
67
+ this.cache.delete(id);
68
+ }
69
+ clear() {
70
+ this.cache.clear();
71
+ }
72
+ }
73
+ function createMemCache() {
74
+ return new MemoryCache();
75
+ }
76
+ exports.AbstractCache = AbstractCache;
77
+ exports.MemoryCache = MemoryCache;
78
+ exports.createMemCache = createMemCache;
79
+ //# sourceMappingURL=cache.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cache.cjs","sources":["../src/cache.ts"],"sourcesContent":["import { type TDateValue, dateParse } from './date';\nimport type { MaybePromise } from './types';\n\n/**\n * 缓存选项\n */\nexport type TCacheOptions = {\n /**\n * 缓存的最大时长(毫秒),为 0 时表示永久缓存\n */\n maxAge?: number;\n\n /**\n * 缓存的过期时间(时间戳、日期字符串、日期对象等)\n * 优先级比 maxAge 更高\n */\n expiredAt?: TDateValue;\n};\n\n/**\n * 缓存项\n * @template T 缓存数据的类型\n */\nexport type TCached<T> = {\n /**\n * 缓存项的唯一标识\n */\n id: string;\n /**\n * 缓存的数据\n */\n data: T;\n /**\n * 缓存项的创建时间(时间戳)\n */\n createdAt: number;\n /**\n * 缓存项的过期时间(时间戳)\n */\n expiredAt: number;\n};\n\nexport type TCache<T> = {\n get(id: string): MaybePromise<TCached<T> | null>;\n\n set(id: string, data: T, options?: TCacheOptions): MaybePromise<void>;\n\n del(id: string): MaybePromise<void>;\n};\n\n/**\n * 缓存抽象类\n * @template T 缓存数据的类型\n */\nexport class AbstractCache<T> implements TCache<T> {\n isExpired(cached: TCached<T>) {\n return cached.expiredAt > 0 && Date.now() > cached.expiredAt;\n }\n\n normalizeCached(id: string, data: T, options?: TCacheOptions): TCached<T> {\n const { expiredAt = 0, maxAge = 0 } = options || {};\n const now = Date.now();\n return {\n id,\n data,\n createdAt: now,\n expiredAt: expiredAt ? dateParse(expiredAt).getTime() : maxAge > 0 ? now + maxAge : 0,\n };\n }\n\n /**\n * 获取缓存项\n * @param id 缓存项的唯一标识\n * @returns 返回缓存项或 null\n */\n get(id: string): MaybePromise<TCached<T> | null> {\n return null;\n }\n\n /**\n * 设置缓存项\n * @param id 缓存项的唯一标识\n * @param data 要缓存的数据\n * @param options 缓存选项\n */\n set(id: string, data: T, options?: TCacheOptions): MaybePromise<void> {\n //\n }\n\n /**\n * 删除缓存项\n * @param id 缓存项的唯一标识\n */\n del(id: string): MaybePromise<void> {\n //\n }\n\n /**\n * 清空缓存\n */\n clear(): MaybePromise<void> {\n //\n }\n}\n\n/**\n * 内存缓存实现类\n * @template T 缓存数据的类型\n */\nexport class MemoryCache<T> extends AbstractCache<T> {\n private cache: Map<string, TCached<T>> = new Map();\n\n get(id: string) {\n const cached = this.cache.get(id);\n\n if (!cached) return null;\n\n if (this.isExpired(cached)) {\n this.del(id);\n return null;\n }\n\n return cached;\n }\n\n set(id: string, data: T, options?: TCacheOptions) {\n this.cache.set(id, this.normalizeCached(id, data, options));\n }\n\n del(id: string) {\n this.cache.delete(id);\n }\n\n clear() {\n this.cache.clear();\n }\n}\n\n/**\n * 创建一个新的内存缓存实例\n * @template T 缓存数据的类型\n * @returns 返回一个新的 MemoryCache 实例\n */\nexport function createMemCache<T>() {\n return new MemoryCache<T>();\n}\n"],"names":["dateParse"],"mappings":";;;;;;AAsDO,MAAM,cAAsC;AAAA,EACjD,UAAU,QAAoB;AAC5B,WAAO,OAAO,YAAY,KAAK,KAAK,IAAA,IAAQ,OAAO;AAAA,EAAA;AAAA,EAGrD,gBAAgB,IAAY,MAAS,SAAqC;AACxE,UAAM,EAAE,YAAY,GAAG,SAAS,EAAE,IAAI,WAAW,CAAC;AAC5C,UAAA,MAAM,KAAK,IAAI;AACd,WAAA;AAAA,MACL;AAAA,MACA;AAAA,MACA,WAAW;AAAA,MACX,WAAW,YAAYA,KAAA,UAAU,SAAS,EAAE,QAAY,IAAA,SAAS,IAAI,MAAM,SAAS;AAAA,IACtF;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQF,IAAI,IAA6C;AACxC,WAAA;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAST,IAAI,IAAY,MAAS,SAA6C;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQtE,IAAI,IAAgC;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAOpC,QAA4B;AAAA,EAAA;AAG9B;AAMO,MAAM,oBAAuB,cAAiB;AAAA,EAA9C;AAAA;AACG,qDAAqC,IAAI;AAAA;AAAA,EAEjD,IAAI,IAAY;AACd,UAAM,SAAS,KAAK,MAAM,IAAI,EAAE;AAE5B,QAAA,CAAC,OAAe,QAAA;AAEhB,QAAA,KAAK,UAAU,MAAM,GAAG;AAC1B,WAAK,IAAI,EAAE;AACJ,aAAA;AAAA,IAAA;AAGF,WAAA;AAAA,EAAA;AAAA,EAGT,IAAI,IAAY,MAAS,SAAyB;AAC3C,SAAA,MAAM,IAAI,IAAI,KAAK,gBAAgB,IAAI,MAAM,OAAO,CAAC;AAAA,EAAA;AAAA,EAG5D,IAAI,IAAY;AACT,SAAA,MAAM,OAAO,EAAE;AAAA,EAAA;AAAA,EAGtB,QAAQ;AACN,SAAK,MAAM,MAAM;AAAA,EAAA;AAErB;AAOO,SAAS,iBAAoB;AAClC,SAAO,IAAI,YAAe;AAC5B;;;;"}
@@ -0,0 +1,90 @@
1
+ import { TDateValue } from './date';
2
+ import { MaybePromise } from './types';
3
+ /**
4
+ * 缓存选项
5
+ */
6
+ export type TCacheOptions = {
7
+ /**
8
+ * 缓存的最大时长(毫秒),为 0 时表示永久缓存
9
+ */
10
+ maxAge?: number;
11
+ /**
12
+ * 缓存的过期时间(时间戳、日期字符串、日期对象等)
13
+ * 优先级比 maxAge 更高
14
+ */
15
+ expiredAt?: TDateValue;
16
+ };
17
+ /**
18
+ * 缓存项
19
+ * @template T 缓存数据的类型
20
+ */
21
+ export type TCached<T> = {
22
+ /**
23
+ * 缓存项的唯一标识
24
+ */
25
+ id: string;
26
+ /**
27
+ * 缓存的数据
28
+ */
29
+ data: T;
30
+ /**
31
+ * 缓存项的创建时间(时间戳)
32
+ */
33
+ createdAt: number;
34
+ /**
35
+ * 缓存项的过期时间(时间戳)
36
+ */
37
+ expiredAt: number;
38
+ };
39
+ export type TCache<T> = {
40
+ get(id: string): MaybePromise<TCached<T> | null>;
41
+ set(id: string, data: T, options?: TCacheOptions): MaybePromise<void>;
42
+ del(id: string): MaybePromise<void>;
43
+ };
44
+ /**
45
+ * 缓存抽象类
46
+ * @template T 缓存数据的类型
47
+ */
48
+ export declare class AbstractCache<T> implements TCache<T> {
49
+ isExpired(cached: TCached<T>): boolean;
50
+ normalizeCached(id: string, data: T, options?: TCacheOptions): TCached<T>;
51
+ /**
52
+ * 获取缓存项
53
+ * @param id 缓存项的唯一标识
54
+ * @returns 返回缓存项或 null
55
+ */
56
+ get(id: string): MaybePromise<TCached<T> | null>;
57
+ /**
58
+ * 设置缓存项
59
+ * @param id 缓存项的唯一标识
60
+ * @param data 要缓存的数据
61
+ * @param options 缓存选项
62
+ */
63
+ set(id: string, data: T, options?: TCacheOptions): MaybePromise<void>;
64
+ /**
65
+ * 删除缓存项
66
+ * @param id 缓存项的唯一标识
67
+ */
68
+ del(id: string): MaybePromise<void>;
69
+ /**
70
+ * 清空缓存
71
+ */
72
+ clear(): MaybePromise<void>;
73
+ }
74
+ /**
75
+ * 内存缓存实现类
76
+ * @template T 缓存数据的类型
77
+ */
78
+ export declare class MemoryCache<T> extends AbstractCache<T> {
79
+ private cache;
80
+ get(id: string): TCached<T> | null;
81
+ set(id: string, data: T, options?: TCacheOptions): void;
82
+ del(id: string): void;
83
+ clear(): void;
84
+ }
85
+ /**
86
+ * 创建一个新的内存缓存实例
87
+ * @template T 缓存数据的类型
88
+ * @returns 返回一个新的 MemoryCache 实例
89
+ */
90
+ export declare function createMemCache<T>(): MemoryCache<T>;
package/dist/cache.mjs ADDED
@@ -0,0 +1,79 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
4
+ import { d as dateParse } from "./core.mjs";
5
+ class AbstractCache {
6
+ isExpired(cached) {
7
+ return cached.expiredAt > 0 && Date.now() > cached.expiredAt;
8
+ }
9
+ normalizeCached(id, data, options) {
10
+ const { expiredAt = 0, maxAge = 0 } = options || {};
11
+ const now = Date.now();
12
+ return {
13
+ id,
14
+ data,
15
+ createdAt: now,
16
+ expiredAt: expiredAt ? dateParse(expiredAt).getTime() : maxAge > 0 ? now + maxAge : 0
17
+ };
18
+ }
19
+ /**
20
+ * 获取缓存项
21
+ * @param id 缓存项的唯一标识
22
+ * @returns 返回缓存项或 null
23
+ */
24
+ get(id) {
25
+ return null;
26
+ }
27
+ /**
28
+ * 设置缓存项
29
+ * @param id 缓存项的唯一标识
30
+ * @param data 要缓存的数据
31
+ * @param options 缓存选项
32
+ */
33
+ set(id, data, options) {
34
+ }
35
+ /**
36
+ * 删除缓存项
37
+ * @param id 缓存项的唯一标识
38
+ */
39
+ del(id) {
40
+ }
41
+ /**
42
+ * 清空缓存
43
+ */
44
+ clear() {
45
+ }
46
+ }
47
+ class MemoryCache extends AbstractCache {
48
+ constructor() {
49
+ super(...arguments);
50
+ __publicField(this, "cache", /* @__PURE__ */ new Map());
51
+ }
52
+ get(id) {
53
+ const cached = this.cache.get(id);
54
+ if (!cached) return null;
55
+ if (this.isExpired(cached)) {
56
+ this.del(id);
57
+ return null;
58
+ }
59
+ return cached;
60
+ }
61
+ set(id, data, options) {
62
+ this.cache.set(id, this.normalizeCached(id, data, options));
63
+ }
64
+ del(id) {
65
+ this.cache.delete(id);
66
+ }
67
+ clear() {
68
+ this.cache.clear();
69
+ }
70
+ }
71
+ function createMemCache() {
72
+ return new MemoryCache();
73
+ }
74
+ export {
75
+ AbstractCache,
76
+ MemoryCache,
77
+ createMemCache
78
+ };
79
+ //# sourceMappingURL=cache.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cache.mjs","sources":["../src/cache.ts"],"sourcesContent":["import { type TDateValue, dateParse } from './date';\nimport type { MaybePromise } from './types';\n\n/**\n * 缓存选项\n */\nexport type TCacheOptions = {\n /**\n * 缓存的最大时长(毫秒),为 0 时表示永久缓存\n */\n maxAge?: number;\n\n /**\n * 缓存的过期时间(时间戳、日期字符串、日期对象等)\n * 优先级比 maxAge 更高\n */\n expiredAt?: TDateValue;\n};\n\n/**\n * 缓存项\n * @template T 缓存数据的类型\n */\nexport type TCached<T> = {\n /**\n * 缓存项的唯一标识\n */\n id: string;\n /**\n * 缓存的数据\n */\n data: T;\n /**\n * 缓存项的创建时间(时间戳)\n */\n createdAt: number;\n /**\n * 缓存项的过期时间(时间戳)\n */\n expiredAt: number;\n};\n\nexport type TCache<T> = {\n get(id: string): MaybePromise<TCached<T> | null>;\n\n set(id: string, data: T, options?: TCacheOptions): MaybePromise<void>;\n\n del(id: string): MaybePromise<void>;\n};\n\n/**\n * 缓存抽象类\n * @template T 缓存数据的类型\n */\nexport class AbstractCache<T> implements TCache<T> {\n isExpired(cached: TCached<T>) {\n return cached.expiredAt > 0 && Date.now() > cached.expiredAt;\n }\n\n normalizeCached(id: string, data: T, options?: TCacheOptions): TCached<T> {\n const { expiredAt = 0, maxAge = 0 } = options || {};\n const now = Date.now();\n return {\n id,\n data,\n createdAt: now,\n expiredAt: expiredAt ? dateParse(expiredAt).getTime() : maxAge > 0 ? now + maxAge : 0,\n };\n }\n\n /**\n * 获取缓存项\n * @param id 缓存项的唯一标识\n * @returns 返回缓存项或 null\n */\n get(id: string): MaybePromise<TCached<T> | null> {\n return null;\n }\n\n /**\n * 设置缓存项\n * @param id 缓存项的唯一标识\n * @param data 要缓存的数据\n * @param options 缓存选项\n */\n set(id: string, data: T, options?: TCacheOptions): MaybePromise<void> {\n //\n }\n\n /**\n * 删除缓存项\n * @param id 缓存项的唯一标识\n */\n del(id: string): MaybePromise<void> {\n //\n }\n\n /**\n * 清空缓存\n */\n clear(): MaybePromise<void> {\n //\n }\n}\n\n/**\n * 内存缓存实现类\n * @template T 缓存数据的类型\n */\nexport class MemoryCache<T> extends AbstractCache<T> {\n private cache: Map<string, TCached<T>> = new Map();\n\n get(id: string) {\n const cached = this.cache.get(id);\n\n if (!cached) return null;\n\n if (this.isExpired(cached)) {\n this.del(id);\n return null;\n }\n\n return cached;\n }\n\n set(id: string, data: T, options?: TCacheOptions) {\n this.cache.set(id, this.normalizeCached(id, data, options));\n }\n\n del(id: string) {\n this.cache.delete(id);\n }\n\n clear() {\n this.cache.clear();\n }\n}\n\n/**\n * 创建一个新的内存缓存实例\n * @template T 缓存数据的类型\n * @returns 返回一个新的 MemoryCache 实例\n */\nexport function createMemCache<T>() {\n return new MemoryCache<T>();\n}\n"],"names":[],"mappings":";;;;AAsDO,MAAM,cAAsC;AAAA,EACjD,UAAU,QAAoB;AAC5B,WAAO,OAAO,YAAY,KAAK,KAAK,IAAA,IAAQ,OAAO;AAAA,EAAA;AAAA,EAGrD,gBAAgB,IAAY,MAAS,SAAqC;AACxE,UAAM,EAAE,YAAY,GAAG,SAAS,EAAE,IAAI,WAAW,CAAC;AAC5C,UAAA,MAAM,KAAK,IAAI;AACd,WAAA;AAAA,MACL;AAAA,MACA;AAAA,MACA,WAAW;AAAA,MACX,WAAW,YAAY,UAAU,SAAS,EAAE,QAAY,IAAA,SAAS,IAAI,MAAM,SAAS;AAAA,IACtF;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQF,IAAI,IAA6C;AACxC,WAAA;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAST,IAAI,IAAY,MAAS,SAA6C;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQtE,IAAI,IAAgC;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAOpC,QAA4B;AAAA,EAAA;AAG9B;AAMO,MAAM,oBAAuB,cAAiB;AAAA,EAA9C;AAAA;AACG,qDAAqC,IAAI;AAAA;AAAA,EAEjD,IAAI,IAAY;AACd,UAAM,SAAS,KAAK,MAAM,IAAI,EAAE;AAE5B,QAAA,CAAC,OAAe,QAAA;AAEhB,QAAA,KAAK,UAAU,MAAM,GAAG;AAC1B,WAAK,IAAI,EAAE;AACJ,aAAA;AAAA,IAAA;AAGF,WAAA;AAAA,EAAA;AAAA,EAGT,IAAI,IAAY,MAAS,SAAyB;AAC3C,SAAA,MAAM,IAAI,IAAI,KAAK,gBAAgB,IAAI,MAAM,OAAO,CAAC;AAAA,EAAA;AAAA,EAG5D,IAAI,IAAY;AACT,SAAA,MAAM,OAAO,EAAE;AAAA,EAAA;AAAA,EAGtB,QAAQ;AACN,SAAK,MAAM,MAAM;AAAA,EAAA;AAErB;AAOO,SAAS,iBAAoB;AAClC,SAAO,IAAI,YAAe;AAC5B;"}
@@ -1,6 +1,4 @@
1
- import { luminance } from './luminance';
2
- import type { RGB } from './types';
3
-
1
+ import { RGB } from './types';
4
2
  /**
5
3
  * 计算两个颜色之间的对比度比率(符合WCAG 2.1标准)
6
4
  * @param rgb1 第一个RGB颜色对象(分量范围0-255)
@@ -9,12 +7,4 @@ import type { RGB } from './types';
9
7
  * @example
10
8
  * contrast({r: 0, g: 0, b: 0}, {r: 255, g: 255, b: 255}) // 返回21
11
9
  */
12
- export function contrast(rgb1: RGB, rgb2: RGB) {
13
- const lum1 = luminance(rgb1);
14
- const lum2 = luminance(rgb2);
15
-
16
- const brightest = Math.max(lum1, lum2);
17
- const darkest = Math.min(lum1, lum2);
18
-
19
- return (brightest + 0.05) / (darkest + 0.05);
20
- }
10
+ export declare function contrast(rgb1: RGB, rgb2: RGB): number;
@@ -0,0 +1,8 @@
1
+ import { LAB } from './types';
2
+ /**
3
+ * 计算两个颜色之间的距离(距离越小相似度越高),值范围 [0, 1]
4
+ * @param {LAB} labA
5
+ * @param {LAB} labB
6
+ * @returns {number}
7
+ */
8
+ export declare function distance(labA: LAB, labB: LAB): number;
@@ -0,0 +1,2 @@
1
+ import { RGB } from './types';
2
+ export declare function rgbToHue({ r, g, b }: RGB): [number, number, number, number];
@@ -0,0 +1,3 @@
1
+ import { HEX, HSL } from './types';
2
+ export declare function hslToHex(hsl: HSL): HEX;
3
+ export declare function hexToHsl(hex: HEX): HSL;
@@ -1,7 +1,4 @@
1
- import { hexToRgb, rgbToHex } from './hex-rgb';
2
- import { hsvToRgb, rgbToHsv } from './rgb-hsv';
3
- import type { HEX, HSV } from './types';
4
-
1
+ import { HEX, HSV } from './types';
5
2
  /**
6
3
  * 将HEX颜色转换为HSV颜色空间
7
4
  * @param hex HEX颜色字符串(支持3位或6位格式)
@@ -12,10 +9,7 @@ import type { HEX, HSV } from './types';
12
9
  * @example
13
10
  * hexToHsv('#ff0000') // returns {h: 0, s: 100, v: 100}
14
11
  */
15
- export function hexToHsv(hex: HEX): HSV {
16
- return rgbToHsv(hexToRgb(hex));
17
- }
18
-
12
+ export declare function hexToHsv(hex: HEX): HSV;
19
13
  /**
20
14
  * 将HSV颜色转换回HEX字符串
21
15
  * @param hsv HSV颜色对象
@@ -23,6 +17,4 @@ export function hexToHsv(hex: HEX): HSV {
23
17
  * @example
24
18
  * hsvToHex({h: 0, s: 100, v: 100}) // returns '#ff0000'
25
19
  */
26
- export function hsvToHex(hsv: HSV): HEX {
27
- return rgbToHex(hsvToRgb(hsv));
28
- }
20
+ export declare function hsvToHex(hsv: HSV): HEX;
@@ -1,7 +1,4 @@
1
- import { hexToRgb, rgbToHex } from './hex-rgb';
2
- import { hwbToRgb, rgbToHwb } from './rgb-hwb';
3
- import type { HEX, HWB } from './types';
4
-
1
+ import { HEX, HWB } from './types';
5
2
  /**
6
3
  * 将HWB颜色转换为HEX字符串
7
4
  * @param hwb HWB颜色对象
@@ -12,10 +9,7 @@ import type { HEX, HWB } from './types';
12
9
  * @example
13
10
  * hwbToHex({h: 0, w: 0, b: 0}) // returns '#ff0000'
14
11
  */
15
- export function hwbToHex(hwb: HWB): HEX {
16
- return rgbToHex(hwbToRgb(hwb));
17
- }
18
-
12
+ export declare function hwbToHex(hwb: HWB): HEX;
19
13
  /**
20
14
  * 将HEX颜色转换为HWB颜色空间
21
15
  * @param hex HEX颜色字符串(支持3位或6位格式)
@@ -26,6 +20,4 @@ export function hwbToHex(hwb: HWB): HEX {
26
20
  * @example
27
21
  * hexToHwb('#ff0000') // returns {h: 0, w: 0, b: 0}
28
22
  */
29
- export function hexToHwb(hex: HEX): HWB {
30
- return rgbToHwb(hexToRgb(hex));
31
- }
23
+ export declare function hexToHwb(hex: HEX): HWB;
@@ -0,0 +1,18 @@
1
+ import { HEX, RGB } from './types';
2
+ /**
3
+ * 将HEX颜色字符串转换为RGB颜色对象
4
+ * @param hex HEX颜色字符串,支持3位或6位格式(例如#f00或#ff0000)
5
+ * @returns {RGB} 对应的RGB颜色对象
6
+ * @throws {SyntaxError} 当颜色格式不符合规范时抛出
7
+ * @example
8
+ * hexToRgb('#f00') // returns {r: 255, g: 0, b: 0}
9
+ */
10
+ export declare function hexToRgb(hex: HEX): RGB;
11
+ /**
12
+ * 将RGB颜色对象转换为HEX颜色字符串
13
+ * @param rgb RGB颜色对象
14
+ * @returns {HEX} 6位HEX颜色字符串(带#前缀)
15
+ * @example
16
+ * rgbToHex({r: 255, g: 0, b: 0}) // returns '#ff0000'
17
+ */
18
+ export declare function rgbToHex(rgb: RGB): HEX;
@@ -1,5 +1,4 @@
1
- import type { HSL } from './types';
2
-
1
+ import { HSL } from './types';
3
2
  /**
4
3
  * 通过HSL颜色空间调整颜色亮度
5
4
  * @param hsl 原始 HSL 颜色
@@ -8,8 +7,4 @@ import type { HSL } from './types';
8
7
  * @example
9
8
  * hslLighten({h: 300, s: 33, l: 44}, 0.2) // 返回亮度增加20%后的颜色
10
9
  */
11
- export function hslLighten(hsl: HSL, value: number): HSL {
12
- const hslFinal = { ...hsl };
13
- hslFinal.l = hslFinal.l * (1 + value);
14
- return hslFinal;
15
- }
10
+ export declare function hslLighten(hsl: HSL, value: number): HSL;
@@ -1,5 +1,4 @@
1
- import type { HSV } from './types';
2
-
1
+ import { HSV } from './types';
3
2
  /**
4
3
  * 通过HSV颜色空间调整颜色明度
5
4
  * @param hsv 原始HSV颜色对象
@@ -10,8 +9,4 @@ import type { HSV } from './types';
10
9
  * @example
11
10
  * hsvBrighten({h: 0, s: 100, v: 50}, 0.3) // 返回{h: 0, s: 100, v: 65}
12
11
  */
13
- export function hsvBrighten(hsv: HSV, value: number): HSV {
14
- const hsvFinal = { ...hsv };
15
- hsvFinal.v = hsvFinal.v * (1 + value);
16
- return hsvFinal;
17
- }
12
+ export declare function hsvBrighten(hsv: HSV, value: number): HSV;
@@ -1,5 +1,4 @@
1
- import type { RGB } from './types';
2
-
1
+ import { RGB } from './types';
3
2
  /**
4
3
  * 计算RGB颜色的相对亮度(符合WCAG 2.1标准)
5
4
  * @param rgb RGB颜色对象(分量范围0-255)
@@ -8,10 +7,4 @@ import type { RGB } from './types';
8
7
  * @example
9
8
  * luminance({r: 255, g: 255, b: 255}) // 返回1
10
9
  */
11
- export function luminance({ r, g, b }: RGB) {
12
- const a = [r, g, b].map((v) => {
13
- const vFinal = v / 255;
14
- return vFinal <= 0.03928 ? vFinal / 12.92 : ((vFinal + 0.055) / 1.055) ** 2.4;
15
- });
16
- return a[0] * 0.2126 + a[1] * 0.7152 + a[2] * 0.0722;
17
- }
10
+ export declare function luminance({ r, g, b }: RGB): number;
@@ -1,5 +1,4 @@
1
- import type { HSL, HSV, RGB } from './types';
2
-
1
+ import { HSL, HSV, RGB } from './types';
3
2
  /**
4
3
  * 颜色混合函数(支持RGB/HSL/HSV色彩模型)
5
4
  * @template T 颜色类型,支持RGB、HSL或HSV对象
@@ -16,11 +15,4 @@ import type { HSL, HSV, RGB } from './types';
16
15
  * // HSL混合示例
17
16
  * mix({h: 0, s: 100, l: 50}, {h: 120, s: 100, l: 50}, 0.3)
18
17
  */
19
- export function mix<T extends RGB | HSV | HSL>(a: T, b: T, weight = 0.5): T {
20
- return Object.keys(a).reduce((acc, key) => {
21
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
22
- // @ts-ignore
23
- acc[key] = (b[key] - a[key]) * weight + a[key];
24
- return acc;
25
- }, {} as T);
26
- }
18
+ export declare function mix<T extends RGB | HSV | HSL>(a: T, b: T, weight?: number): T;
@@ -0,0 +1,23 @@
1
+ import { HSL, RGB } from './types';
2
+ /**
3
+ * 将RGB颜色转换为HSL颜色空间
4
+ * @param rgb RGB颜色对象(分量范围0-255)
5
+ * @returns {HSL} HSL颜色对象,其中:
6
+ * h: 色相(0-360度)
7
+ * s: 饱和度(0-100%)
8
+ * l: 亮度(0-100%)
9
+ * @example
10
+ * rgbToHsl({r: 255, g: 0, b: 0}) // returns {h: 0, s: 100, l: 50}
11
+ */
12
+ export declare function rgbToHsl(rgb: RGB): HSL;
13
+ /**
14
+ * 将HSL颜色转换回RGB颜色空间
15
+ * @param hsl HSL颜色对象
16
+ * @param hsl.h 色相(0-360度)
17
+ * @param hsl.s 饱和度(0-100%)
18
+ * @param hsl.l 亮度(0-100%)
19
+ * @returns {RGB} RGB颜色对象(分量范围0-255)
20
+ * @example
21
+ * hslToRgb({h: 0, s: 100, l: 50}) // returns {r: 255, g: 0, b: 0}
22
+ */
23
+ export declare function hslToRgb({ h, s, l }: HSL): RGB;
@@ -1,9 +1,4 @@
1
- import { rgbToHue } from './helpers';
2
- import type { HSV, RGB } from './types';
3
-
4
- const { abs, min, max, round } = Math;
5
-
6
- // @ref https://www.30secondsofcode.org/js/s/rgb-to-hsb/
1
+ import { HSV, RGB } from './types';
7
2
  /**
8
3
  * 将RGB颜色转换为HSV颜色空间
9
4
  * @param rgb RGB颜色对象(分量范围0-255)
@@ -15,17 +10,7 @@ const { abs, min, max, round } = Math;
15
10
  * @example
16
11
  * rgbToHsv({r: 255, g: 0, b: 0}) // {h: 0, s: 100, v: 100}
17
12
  */
18
- export function rgbToHsv(rgb: RGB): HSV {
19
- const [hue, max, min, diff] = rgbToHue(rgb);
20
-
21
- return {
22
- h: hue,
23
- s: max && (diff / max) * 100,
24
- v: max * 100,
25
- };
26
- }
27
-
28
- // @ref https://www.30secondsofcode.org/js/s/hsb-to-rgb/
13
+ export declare function rgbToHsv(rgb: RGB): HSV;
29
14
  /**
30
15
  * 将HSV颜色转换回RGB颜色空间
31
16
  * @param hsv HSV颜色对象
@@ -37,16 +22,4 @@ export function rgbToHsv(rgb: RGB): HSV {
37
22
  * @example
38
23
  * hsvToRgb({h: 0, s: 100, v: 100}) // {r: 255, g: 0, b: 0}
39
24
  */
40
- export function hsvToRgb({ h, s, v }: HSV): RGB {
41
- s /= 100;
42
- v /= 100;
43
-
44
- const k = (n: number) => (n + h / 60) % 6;
45
- const f = (n: number) => v * (1 - s * max(0, min(k(n), 4 - k(n), 1)));
46
-
47
- return {
48
- r: 255 * f(5),
49
- g: 255 * f(3),
50
- b: 255 * f(1),
51
- };
52
- }
25
+ export declare function hsvToRgb({ h, s, v }: HSV): RGB;
@@ -0,0 +1,29 @@
1
+ import { HWB, RGB } from './types';
2
+ /**
3
+ * 将RGB颜色转换为HWB颜色空间
4
+ * @param rgb RGB颜色对象(分量范围0-255)
5
+ * @returns {HWB} HWB颜色对象:
6
+ * h: 色相(0-360度)
7
+ * w: 白度(0-100%)
8
+ * b: 黑度(0-100%)
9
+ * @see https://www.w3.org/TR/css-color-4/#hwb-to-rgb
10
+ * @example
11
+ * rgbToHwb({r: 255, g: 0, b: 0}) // {h: 0, w: 0, b: 0}
12
+ */
13
+ export declare function rgbToHwb(rgb: RGB): HWB;
14
+ /**
15
+ * 将HWB颜色转换回RGB颜色空间
16
+ * @param hwb HWB颜色对象
17
+ * @param hwb.h 色相(0-360度)
18
+ * @param hwb.w 白度(0-100%)
19
+ * @param hwb.b 黑度(0-100%)
20
+ * @returns {RGB} RGB颜色对象(分量范围0-255)
21
+ * @see https://en.wikipedia.org/wiki/HWB_color_model
22
+ * @example
23
+ * hwbToRgb({h: 0, w: 0, b: 0}) // {r: 255, g: 0, b: 0}
24
+ */
25
+ export declare function hwbToRgb({ h, w: white, b: black }: HWB): {
26
+ r: number;
27
+ g: number;
28
+ b: number;
29
+ };
@@ -1,7 +1,4 @@
1
- import { rgbToXyz, xyzToRgb } from './rgb-xyz';
2
- import type { LAB, RGB } from './types';
3
- import { labToXyz, xyzToLab } from './xyz-lab';
4
-
1
+ import { LAB, RGB } from './types';
5
2
  /**
6
3
  * 将RGB颜色转换为Lab颜色空间(CIE 1976标准)
7
4
  * @param rgb RGB颜色对象(分量范围0-255)
@@ -13,10 +10,7 @@ import { labToXyz, xyzToLab } from './xyz-lab';
13
10
  * @example
14
11
  * rgbToLab({r: 255, g: 0, b: 0}) // {l: 53.24, a: 80.09, b: 67.20}
15
12
  */
16
- export function rgbToLab(rgb: RGB): LAB {
17
- return xyzToLab(rgbToXyz(rgb));
18
- }
19
-
13
+ export declare function rgbToLab(rgb: RGB): LAB;
20
14
  /**
21
15
  * 将Lab颜色转换回RGB颜色空间
22
16
  * @param lab Lab颜色对象
@@ -28,6 +22,4 @@ export function rgbToLab(rgb: RGB): LAB {
28
22
  * @example
29
23
  * labToRgb({l: 53.24, a: 80.09, b: 67.20}) // {r: 255, g: 0, b: 0}
30
24
  */
31
- export function labToRgb(lab: LAB): RGB {
32
- return xyzToRgb(labToXyz(lab));
33
- }
25
+ export declare function labToRgb(lab: LAB): RGB;
@@ -0,0 +1,12 @@
1
+ import { RGB } from './types';
2
+ /**
3
+ * 通过混合颜色调整明暗度
4
+ * @param rgb 原始RGB颜色对象
5
+ * @param value 调整强度(-1到1之间):
6
+ * - 正值时与黑色混合(增加暗度)
7
+ * - 负值时与白色混合(增加亮度)
8
+ * @returns {RGB} 调整后的RGB颜色对象
9
+ * @example
10
+ * rgbDarken({r: 100, g: 150, b: 200}, 0.2) // 变暗20%
11
+ */
12
+ export declare function rgbWhiter(rgb: RGB, value: number): RGB;
@@ -0,0 +1,22 @@
1
+ import { RGB, XYZ } from './types';
2
+ /**
3
+ * 将RGB颜色转换为CIE 1931 XYZ颜色空间(D65白点,2°观察者)
4
+ * @param rgb RGB颜色对象(分量范围0-255)
5
+ * @returns {XYZ} XYZ颜色对象:
6
+ * x: 约0-95.047
7
+ * y: 约0-100.0
8
+ * z: 约0-108.883
9
+ * @see https://en.wikipedia.org/wiki/CIE_1931_color_space
10
+ * @example
11
+ * rgbToXyz({r: 255, g: 255, b: 255}) // {x: 95.047, y: 100.0, z: 108.883}
12
+ */
13
+ export declare function rgbToXyz(rgb: RGB): XYZ;
14
+ /**
15
+ * 将XYZ颜色转换回RGB颜色空间(可能超出常规范围)
16
+ * @param xyz XYZ颜色对象
17
+ * @returns {RGB} RGB颜色对象(分量可能超出0-255范围)
18
+ * @see https://www.brucelindbloom.com/index.html?Eqn_XYZ_to_RGB.html
19
+ * @example
20
+ * xyzToRgb({x: 95.047, y: 100.0, z: 108.883}) // {r: 255, g: 255, b: 255}
21
+ */
22
+ export declare function xyzToRgb(xyz: XYZ): RGB;