@ray-js/lamp-bead-strip 1.0.1-beta-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.
@@ -0,0 +1,187 @@
1
+ /* eslint-disable radix */
2
+
3
+ // 解析RGB颜色字符串为数组
4
+ function parseRGB(color) {
5
+ const result = color.replace('rgba(', '').replace('rgb(', '').replace(')', '').split(',');
6
+ return [Number(result[0]), Number(result[1]), Number(result[2])];
7
+ }
8
+
9
+ /**
10
+ * 生成渐变颜色数组
11
+ * @param {Array} colors - 长度为2-8的RGB颜色字符串数组,例如['rgb(255, 0, 0)', 'rgb(0, 255, 0)']
12
+ * @param {number} steps - 希望生成的渐变颜色数量
13
+ * @return {Array} 生成的渐变颜色数组
14
+ */
15
+ export function generateGradientColors() {
16
+ let colors = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
17
+ let steps = arguments.length > 1 ? arguments[1] : undefined;
18
+ if (colors.length < 2 || colors.length > 8) {
19
+ throw new Error('颜色数组长度必须为2至8之间');
20
+ }
21
+ if (steps <= 0) {
22
+ throw new Error('步骤数必须大于0');
23
+ }
24
+
25
+ // 生成单个频道的渐变色
26
+ function interpolate(start, end, factor) {
27
+ return Math.round(start + (end - start) * factor);
28
+ }
29
+
30
+ // 生成两个颜色之间的渐变色
31
+ function interpolateColors(color1, color2, factor) {
32
+ const r = interpolate(color1[0], color2[0], factor);
33
+ const g = interpolate(color1[1], color2[1], factor);
34
+ const b = interpolate(color1[2], color2[2], factor);
35
+ return `rgb(${r}, ${g}, ${b})`;
36
+ }
37
+ const parsedColors = colors.map(parseRGB);
38
+ const gradientColors = [];
39
+ for (let i = 0; i < steps; i++) {
40
+ const scaleIndex = i * (parsedColors.length - 1) / (steps - 1);
41
+ const startColorIndex = Math.floor(scaleIndex);
42
+ const endColorIndex = Math.ceil(scaleIndex);
43
+ const color = interpolateColors(parsedColors[startColorIndex], parsedColors[endColorIndex], scaleIndex - startColorIndex);
44
+ gradientColors.push(color);
45
+ }
46
+ return gradientColors;
47
+ }
48
+ export const topList = [0, 2.81, 2.29, 1.76, 1.2, 0.63, 0.06, -0.52, -1.12, -1.72, -2.1, -2.04, -1.58, -0.96, -0.37, 0.2, 0.78, 1.34, 1.9, 2.45];
49
+ export function getListByIndex(list, number) {
50
+ const max = list.length - 1;
51
+ // eslint-disable-next-line no-param-reassign
52
+ if (number > max) number %= list.length;
53
+ return list[number];
54
+ }
55
+ export function splitNumber(number, parts) {
56
+ if (parts < 2 || parts > 8) {
57
+ throw new Error('Parts must be between 2 and 8');
58
+ }
59
+ const base = Math.floor(number / parts); // 基本分割值
60
+
61
+ // 余数,用于均匀分摊
62
+
63
+ const result = Array(parts).fill(base); // 创建一个数组并初始化为基本分割值
64
+
65
+ // 将余数均匀分配到前几个部分中
66
+ for (let i = 0; i < number % parts; i++) {
67
+ result[i]++;
68
+ }
69
+ return result;
70
+ }
71
+ export function arrayFill(number, str) {
72
+ if (number <= 0) return [];
73
+ return new Array(number).fill(str);
74
+ }
75
+ export function splitLight(color) {
76
+ const numberArr = {
77
+ 2: [10, 10],
78
+ // 2
79
+ 3: [6, 7, 7],
80
+ // 3
81
+ 4: [5, 5, 5, 5],
82
+ // 4
83
+ 5: [5, 5, 5, 5],
84
+ // 20
85
+ 6: [5, 5, 5, 5],
86
+ // 12
87
+ 7: [5, 5, 5, 5],
88
+ // 28
89
+ 8: [5, 5, 5, 5] // 8
90
+ }[color.length];
91
+ let colors = color;
92
+ switch (color.length) {
93
+ case 5:
94
+ colors = arrayFill(4, colors).flat(1);
95
+ break;
96
+ case 6:
97
+ colors = arrayFill(2, colors).flat(1);
98
+ break;
99
+ case 7:
100
+ colors = arrayFill(4, colors).flat(1);
101
+ break;
102
+ default:
103
+ break;
104
+ }
105
+ const colorsList = splitArray(colors, numberArr.length);
106
+ return {
107
+ arr: numberArr,
108
+ color: colorsList
109
+ };
110
+ }
111
+
112
+ /**
113
+ * 将数组等分割
114
+ */
115
+ export const splitArray = function (array) {
116
+ let interval = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 4;
117
+ const result = [];
118
+ let temp = [];
119
+ // eslint-disable-next-line no-restricted-syntax
120
+ for (const item of array) {
121
+ temp.push(item);
122
+ if (temp.length === interval) {
123
+ result.push(temp);
124
+ temp = [];
125
+ }
126
+ }
127
+ if (temp.length) {
128
+ result.push(temp);
129
+ }
130
+ return result;
131
+ };
132
+ /**
133
+ * @name: 获取数组内颜色叠加的颜色值
134
+ * @desc:
135
+ * @param {*} rgbArray
136
+ * @return {*} String
137
+ */
138
+ export function averageRGBColors(rgbArray) {
139
+ const {
140
+ length
141
+ } = rgbArray;
142
+ if (length === 0) {
143
+ throw new Error('The input array is empty');
144
+ }
145
+
146
+ // 初始化总和数组
147
+ let total = [0, 0, 0];
148
+
149
+ // 解析并叠加每个颜色
150
+ // eslint-disable-next-line no-restricted-syntax
151
+ for (const rgbString of rgbArray) {
152
+ const [r, g, b] = parseRGB(rgbString);
153
+ total[0] += r;
154
+ total[1] += g;
155
+ total[2] += b;
156
+ }
157
+
158
+ // 计算平均值
159
+ const average = total.map(sum => Math.round(sum / length));
160
+
161
+ // 限制颜色分量在0到255之间
162
+ const clamp = value => Math.max(0, Math.min(255, value));
163
+
164
+ // 合成结果RGB字符串
165
+ const resultRGB = `rgb(${clamp(average[0])}, ${clamp(average[1])}, ${clamp(average[2])})`;
166
+ return resultRGB;
167
+ }
168
+ export function findOverlapIndices(range1, range2) {
169
+ const [start1, end1] = range1;
170
+ const [start2, end2] = range2;
171
+
172
+ // 计算重叠起始和结束索引
173
+ const overlapStart = Math.max(start1 < 0 ? 0 : start1, start2 < 0 ? 0 : start2);
174
+ const overlapEnd = Math.min(end1 < 0 ? 0 : end1, end2 < 0 ? 0 : end2);
175
+
176
+ // 检查是否存在交集
177
+ if (overlapStart > overlapEnd) {
178
+ return []; // 没有交集,返回空数组
179
+ }
180
+
181
+ // 生成重叠部分的索引数组
182
+ const overlapIndices = [];
183
+ for (let i = overlapStart; i <= overlapEnd; i++) {
184
+ overlapIndices.push(i);
185
+ }
186
+ return overlapIndices;
187
+ }
@@ -0,0 +1,11 @@
1
+ export const tuya = {
2
+ backgroundColor: '#f2f4f6'
3
+ };
4
+ export const wechat = {
5
+ backgroundColor: '#f2f4f6'
6
+ };
7
+ export const native = {
8
+ backgroundColor: 'transparent',
9
+ isBleOfflineOverlay: false,
10
+ useSafeAreaView: true
11
+ };
package/lib/index.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ import React from 'react';
2
+ import { IProps } from './props';
3
+ declare const LampBeadStrip: {
4
+ (props: IProps): React.JSX.Element;
5
+ defaultProps: IProps;
6
+ displayName: string;
7
+ };
8
+ export default LampBeadStrip;
package/lib/index.js ADDED
@@ -0,0 +1,20 @@
1
+ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
2
+ import React from 'react';
3
+ import LampBeadStripRjs from './components';
4
+ import { getSystemInfoRes } from './utils';
5
+ import { defaultProps } from './props';
6
+ const {
7
+ windowWidth = 375
8
+ } = getSystemInfoRes();
9
+ const LampBeadStrip = props => {
10
+ // 组件缩放能力
11
+
12
+ return /*#__PURE__*/React.createElement(LampBeadStripRjs, {
13
+ prop: _objectSpread(_objectSpread({}, props), {}, {
14
+ scale: props.scale || windowWidth / 375
15
+ })
16
+ });
17
+ };
18
+ LampBeadStrip.defaultProps = defaultProps;
19
+ LampBeadStrip.displayName = 'LampBeadStrip';
20
+ export default LampBeadStrip;
package/lib/props.d.ts ADDED
@@ -0,0 +1,67 @@
1
+ export interface IProps {
2
+ /**
3
+ * @description.zh 样式类
4
+ * @description.en class Name
5
+ * @default ''
6
+ */
7
+ className?: string;
8
+ /**
9
+ * @description.zh 动画模式 1-16
10
+ * @description.en Animation mode 1-16
11
+ * @default 1
12
+ */
13
+ mode?: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16;
14
+ /**
15
+ * @description.zh canvas唯一标识
16
+ * @description.en canvasId Unique identifier
17
+ */
18
+ canvasId: string | number;
19
+ /**
20
+ * @description.zh canvas父亲盒子样式
21
+ * @description.en canvas father box style
22
+ */
23
+ containerStyle?: string;
24
+ /**
25
+ * @description.zh canvas 盒子样式
26
+ * @description.en canvas Box style
27
+ */
28
+ canvasStyle?: string;
29
+ /**
30
+ * @description.zh 动画颜色列表必须:rgb
31
+ * @description.en Animation Color List must rgb
32
+ */
33
+ colors: string[];
34
+ /**
35
+ * @description.zh 动画速度 1%-100% 对应动画帧刷新间隔 60ms~350ms,速度值越大,间隔越短
36
+ * @description.en The animation speed ranges from 1% to 100%, corresponding to a frame refresh interval of 60ms to 350ms. The larger the speed value, the shorter the interval
37
+ * @default 50
38
+ */
39
+ speed?: number;
40
+ /**
41
+ * @description.zh 是否播放动画
42
+ * @description.en Whether to play the animation.
43
+ * @default true
44
+ */
45
+ ready?: boolean;
46
+ /**
47
+ * @description.zh 缩放比例,适应屏幕
48
+ * @description.en Zoom ratio adapts to the screen
49
+ */
50
+ scale?: number;
51
+ /**
52
+ * @description.zh 详细控制 direction 方向(0/1) segmented 段落(0/1) expand 扩展(0/1/2)
53
+ * @description.en Detailed control of direction (0/1) segmented paragraph (0/1) expand (0/2)
54
+ */
55
+ contentValue?: {
56
+ expand?: 0 | 1 | 2;
57
+ direction?: 0 | 1;
58
+ segmented?: 0 | 1;
59
+ };
60
+ /**
61
+ * @description.zh 关闭灯的颜色
62
+ * @description.en Turn off the color of the light
63
+ * @default 'rgb(58,58,58)'
64
+ */
65
+ closeColor?: string;
66
+ }
67
+ export declare const defaultProps: IProps;
package/lib/props.js ADDED
@@ -0,0 +1,18 @@
1
+ /*
2
+ * @Author: mjh
3
+ * @Date: 2024-09-12 09:55:13
4
+ * @LastEditors: mjh
5
+ * @LastEditTime: 2024-10-21 14:00:58
6
+ * @Description:
7
+ */
8
+
9
+ export const defaultProps = {
10
+ mode: 1,
11
+ canvasId: undefined,
12
+ ready: true,
13
+ speed: 50,
14
+ colors: [],
15
+ contentValue: {},
16
+ closeColor: 'rgb(58,58,58)',
17
+ className: ''
18
+ };
package/lib/utils.d.ts ADDED
@@ -0,0 +1 @@
1
+ export declare const getSystemInfoRes: () => {} | null;
package/lib/utils.js ADDED
@@ -0,0 +1,21 @@
1
+ /*
2
+ * @Author: mjh
3
+ * @Date: 2024-09-02 15:08:35
4
+ * @LastEditors: mjh
5
+ * @LastEditTime: 2024-09-02 15:08:46
6
+ * @Description:
7
+ */
8
+ import { getSystemInfoSync } from '@ray-js/ray';
9
+ let _systemInfo = null;
10
+ export const getSystemInfoRes = () => {
11
+ if (_systemInfo) {
12
+ return _systemInfo;
13
+ }
14
+ try {
15
+ _systemInfo = getSystemInfoSync();
16
+ } catch (error) {
17
+ console.error(error);
18
+ return {};
19
+ }
20
+ return _systemInfo;
21
+ };
package/package.json ADDED
@@ -0,0 +1,69 @@
1
+ {
2
+ "name": "@ray-js/lamp-bead-strip",
3
+ "version": "1.0.1-beta-1",
4
+ "description": "动画灯珠条带",
5
+ "main": "lib/index",
6
+ "files": [
7
+ "lib"
8
+ ],
9
+ "license": "MIT",
10
+ "types": "lib/index.d.ts",
11
+ "maintainers": [
12
+ "tuya_npm",
13
+ {
14
+ "name": "tuyafe",
15
+ "email": "tuyafe@tuya.com"
16
+ }
17
+ ],
18
+ "scripts": {
19
+ "lint": "eslint src --ext .js,.jsx,.ts,.tsx --fix",
20
+ "build": "patch-package && ray build --type=component",
21
+ "watch": "ray start --type=component --output ./example/src/lib",
22
+ "build:tuya": "ray build ./example",
23
+ "build:wechat": "ray build ./example --target=wechat",
24
+ "build:web": "ray build ./example --target=web",
25
+ "build:native": "ray build ./example --target=native",
26
+ "start:native": "ray start ./example -t native --verbose",
27
+ "start:tuya": "ray start ./example",
28
+ "start:wechat": "ray start ./example -t wechat --verbose",
29
+ "start:web": "ray start ./example -t web",
30
+ "prepublishOnly": "yarn build",
31
+ "release-it": "standard-version"
32
+ },
33
+ "peerDependencies": {
34
+ "@ray-js/ray": "^1.4.9"
35
+ },
36
+ "dependencies": {},
37
+ "devDependencies": {
38
+ "@commitlint/cli": "^7.2.1",
39
+ "@commitlint/config-conventional": "^9.0.1",
40
+ "@ray-js/cli": "^1.4.9",
41
+ "@ray-js/panel-sdk": "^1.12.1",
42
+ "@ray-js/ray": "^1.4.9",
43
+ "core-js": "^3.19.1",
44
+ "eslint-config-tuya-panel": "^0.4.2",
45
+ "husky": "^1.2.0",
46
+ "lint-staged": "^10.2.11",
47
+ "patch-package": "^8.0.0",
48
+ "standard-version": "9.3.2"
49
+ },
50
+ "resolutions": {
51
+ "@ray-js/builder-mp": "1.4.15"
52
+ },
53
+ "husky": {
54
+ "hooks": {
55
+ "commit-msg": "commitlint -E HUSKY_GIT_PARAMS --config commitlint.config.js",
56
+ "pre-commit": "lint-staged"
57
+ }
58
+ },
59
+ "lint-staged": {
60
+ "*.{ts,tsx,js,jsx}": [
61
+ "eslint --fix",
62
+ "git add"
63
+ ],
64
+ "*.{json,md,yml,yaml}": [
65
+ "prettier --write",
66
+ "git add"
67
+ ]
68
+ }
69
+ }