微信小程序的图片色彩分析,窃取主色调,调色板

1、在微信小程序中创建包管理器 package.json

npm init -y

2、安装 Mini App Color Thief 包

npm i --save miniapp-color-thief

3、构建 npm

4、wxml

html 复制代码
<canvas canvas-id="myCanvas"></canvas>
<button bindtap="chooseImage">chooseImage</button>
<view wx:for="{{ palette }}" wx:key="index">
  <view style="width:32px;height:32px;background-color: {{ item }}"></view>
  <text>{{ item }}</text>
</view>

5、js

javascript 复制代码
data: {
    palette: []
  },
  /*
  获取图片的色板
  colorThief(data).palette(count, quality).get(); // [[0,0,0],[0,0,0],[0,0,0]...]
    {Number} count 返回色板的颜色数量 ( 1 < count < 256 )
    {Number} quality 计算颜色的精度,默认为 10
  */
  chooseImage() {
    const ctx = wx.createCanvasContext('myCanvas')
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: res => {
        ctx.drawImage(res.tempFilePaths[0], 0, 0, 300, 150)
        ctx.draw(true, () => {
          wx.canvasGetImageData({
            canvasId: "myCanvas",
            x: 0,
            y: 0,
            width: 100,
            height: 100,
            success: res => {
              let palette = colorThief(res.data)
                .palette(3, 10)
                .getHex();
              this.setData({ palette })
            }
          });
        })
      }
    })
  },

6、结果

相关推荐
衍生星球20 小时前
SpringBoot3 + Vue3 + 微信小程序如何学习,以及学哪些技术,组件
sql·微信小程序·uni-app·vue·springboot
weixin_451431561 天前
落地到一个真实项目里——一个 AI驱动的冰箱食材管理小程序
人工智能·ai·小程序
hnxaoli1 天前
统信小程序(十六)xls转xlsx
开发语言·python·小程序
卓怡学长1 天前
w272基于springboot便民医疗服务小程序
java·spring boot·spring·小程序·intellij-idea
新华财经频道1 天前
2026微信小程序搭建平台实测测评,优缺点解析
微信小程序·小程序
kidding7232 天前
旋转大转盘小程序
前端·css·微信小程序·小程序·前端框架
码农客栈2 天前
小程序学习(二十九)之项目打包
小程序
凡科建站2 天前
2026年艺术类教育小程序开发平台有哪些?艺术类教育小程序开发平台推荐
小程序
LT10157974443 天前
2026年在线兼容性测试工具推荐|零部署网页 / APP / 小程序实测对比
测试工具·小程序
码农客栈3 天前
小程序学习(二十八)之“订单列表”
小程序