/** 获取图片主题色
 * @param path * 图片的路径，可以是相对路径，临时文件路径，存储文件路径，网络图片路径
 * @param canvasId * 画布表示
 * @param callback * 回调函数，返回图片主题色的 RGB 颜色值
*/
export function getImageThemeColor(path, canvasId, callback) {
    uni.getImageInfo({
        src: path,
        success(img) {
            // 创建一个 Canvas 对象 
            const ctx = uni.createCanvasContext(canvasId); // 将图片绘制到 Canvas 上
            const imgWidth = 300; const imgHeight = 150;
            ctx.drawImage(img.path, 0, 0, imgWidth, imgHeight);
            ctx.save();
            ctx.draw(true, () => {
                uni.canvasGetImageData({
                    canvasId: canvasId, x: 0, y: 0, width: imgWidth, height: imgHeight, success(res) {
                        // let data = res.data; let arr = []; let r = 1, g = 1, b = 1;
                        // // 取所有像素的平均值 
                        // for (let row = 0; row < imgHeight; row++) {
                        //     for (let col = 0; col < imgWidth; col++) {
                        //         if (row == 0) {
                        //             r += data[imgWidth * row + col];
                        //             g += data[imgWidth * row + col + 1];
                        //             b += data[imgWidth * row + col + 2];
                        //             arr.push([r, g, b]);
                        //         } else {
                        //             r += data[(imgWidth * row + col) * 4];
                        //             g += data[(imgWidth * row + col) * 4 + 1];
                        //             b += data[(imgWidth * row + col) * 4 + 2];
                        //             arr.push([r, g, b]);
                        //         }
                        //     }
                        // }
                        // // 求取平均值
                        // r /= imgWidth * imgHeight; g /= imgWidth * imgHeight; b /= imgWidth * imgHeight;
                        // // 将最终的值取整
                        // r = Math.round(r); g = Math.round(g); b = Math.round(b);
                        // // 返回图片主题色的 RGB 颜色值
                        // if (!!callback) {
                        //     callback(`${r},${g},${b}`);
                        // }

                        let imgData = res.data;
                        var r = 1, g = 1, b = 1, a = 1;
                        const arr = Array.prototype.slice.call(imgData, 0)
                        for (var i = 0; i < arr.length; i += 4) {
                            r += arr[i];
                            g += arr[i + 1];
                            b += arr[i + 2];
                            a += arr[i + 3];
                        }
                        // 求取平均值
                        r /= (imgWidth * imgHeight);
                        g /= (imgWidth * imgHeight);
                        b /= (imgWidth * imgHeight);
                        a /= (imgWidth * imgHeight);
                        // 将最终的值取整
                        r = Math.round(r);
                        g = Math.round(g);
                        b = Math.round(b);
                        a = Math.round(a);

                        let obj = { r, g, b, a }
                        console.log(obj)

                        const linearGradientColor1 = `rgba(${r},${g},${b},${a})`
                        const linearGradientColor2 = `rgba(${r},${g},${b},${a * .2})`
                        let bgColorStr = `linear-gradient(${linearGradientColor1},${linearGradientColor2})`;
                        console.log(bgColorStr);
                        
                        if (!!callback) {
                            callback(bgColorStr);
                        }
                    },
                });
            });
        },
    });
}