微信小程序—图片色彩分析(拾取图片的配色方案)「建议收藏」

微信小程序—图片色彩分析(拾取图片的配色方案)「建议收藏」这是一款图分析图片配色方案demo,图片色彩分析或许可以应用在智能分析色彩领域,比如穿衣搭配、家装等设计或生活领域,但需要大量数据的支持,希望技术能够更好的被应用

大家好,又见面了,我是你们的朋友全栈君。

摘要

这是一款图分析图片配色方案demo,图片色彩分析或许可以应用在智能分析色彩领域,比如穿衣搭配、家装等设计或生活领域,但需要大量数据的支持,希望技术能够更好的被应用

效果

在这里插入图片描述

体验

体验途径:画布系列>色彩分析
在这里插入图片描述

代码

js

import ColorThief from '/utils/color-thief.js'
import { 
   
  rgbToHex,
  uuid,
  colorsEqual,
  saveBlendent
} from '/utils/util.js'
const app = getApp()
Page({ 
   
  data: { 
   
    colorThief: '',
    imgPath: 'cloud://nopc.6e6f-norm6pc-1300924598/meinv/00010.jpg',
    colors: [
    ],
    imgInfo: { 
   },
    colorCount: 5,
    screenWidth: 0
  },
  chooseImg: function () { 
   
    var that = this
    wx.chooseImage({ 
   
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: (res) => { 
   
        that.setData({ 
   
          imgPath: res.tempFilePaths[0],
        })
        wx.getImageInfo({ 
   
          src: 'that.data.imgPath',
          success: function (imgInfo) { 
   

          }
        })
        wx.getImageInfo({ 
   
          src: that.data.imgPath,
          success: (imgInfo) => { 
   
            let { 
   
              width,
              height,
              imgPath
            } = imgInfo;
            let scale = 0.8 * that.data.screenWidth / Math.max(width, height);
            let canvasWidth = Math.floor(scale * width);
            let canvasHeight = Math.floor(scale * height);
            that.setData({ 
   
              imgInfo,
              canvasScale: scale,
              canvasWidth,
              canvasHeight
            });
            let quality = 1;
            that.data.colorThief.getPalette({ 
   
              width: canvasWidth,
              height: canvasHeight,
              imgPath: res.tempFilePaths[0],
              colorCount: that.data.colorCount,
              quality
            }, (colors) => { 
   
              if (colors) { 
   
                colors = colors.map((color) => { 
   
                  return ('#' + rgbToHex(color[0], color[1], color[2]))
                })
                that.setData({ 
   
                  colors,
                })
              }
            });
          }
        })
      }
    })
  },
  onLoad: function () { 
   
    var that = this
    that.data.colorThief = new ColorThief('imageHandler');
    wx.getSystemInfo({ 
   
      success: function (res) { 
   
        that.setData({ 
   
          screenWidth: res.windowWidth
        })
        wx.getImageInfo({ 
   
          src: that.data.imgPath,
          success: (imgInfo) => { 
   
            let width = imgInfo.width
            let height = imgInfo.height
            let imgPath = imgInfo.path
            let scale = 0.8 * that.data.screenWidth / Math.max(width, height);
            let canvasWidth = Math.floor(scale * width);
            let canvasHeight = Math.floor(scale * height);
            that.setData({ 
   
              imgInfo,
              canvasScale: scale,
              canvasWidth,
              canvasHeight,
              imgPath: imgPath
            });
            let quality = 1;
            that.data.colorThief.getPalette({ 
   
              width: canvasWidth,
              height: canvasHeight,
              imgPath: that.data.imgPath,
              colorCount: that.data.colorCount,
              quality
            }, (colors) => { 
   
              if (colors) { 
   
                colors = colors.map((color) => { 
   
                  return ('#' + rgbToHex(color[0], color[1], color[2]))
                })
                that.setData({ 
   
                  colors,
                })
              }
            });
          }
        })
      },
    })
  }
})

wxml


<view class="container">
  <canvas wx:if="{ 
   {imgPath}}" id='image-handler' style='{ 
   {"width:"+canvasWidth+"px;height:"+canvasHeight+"px;"}}' canvas-id="imageHandler" bindtap='chooseImg'></canvas>
  <view class='colors-container' style='{ 
   {"width:"+colors.length * 40 + "px;"}}'>
    <view wx:for="{ 
   {colors}}" wx:key="{ 
   {item}}" class='color' style='{ 
   {"background-color:"+item}};'></view>
  </view>
  <button bindtap="chooseImg" style="margin:20rpx">更换图片</button>
</view>

wxss

.colors-container { 
   
  max-width: 80%;
  height: 80px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: 40rpx;
  box-shadow: 0 4px 12px rgba(0,0,0,.25);
  border-radius: 6px;
  overflow: hidden;
}

.colors-container .color { 
   
  flex: 1;
  max-width: 40px;
  height: 100%;
}
#image-handler { 
   
  box-shadow: 0 4px 12px rgba(0,0,0,.25);
  border-radius: 6px;
  margin-top: 40rpx;
}

所使用到的工具类免积分下载点击下载
所使用到的工具类免积分下载点击下载
所使用到的工具类免积分下载点击下载

程序员不易,来个鼓励师

在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/148113.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • phpspreadsheet使用实例_php获取html中文本框内容

    phpspreadsheet使用实例_php获取html中文本框内容目录安装引用导入Excel获取日期格式安装composerrequirephpoffice/phpspreadsheet引用usePhpOffice\PhpSpreadsheet\Reader\Xlsx;usePhpOffice\PhpSpreadsheet\Reader\Xls;usePhpOffice\PhpSpreadsheet\IOFactory;usePhpOffice\PhpSpreadsheet\Cell\Coordinate;usePhpOffice\PhpS

    2022年9月17日
    2
  • datagrip2021.11.4激活码-激活码分享[通俗易懂]

    (datagrip2021.11.4激活码)最近有小伙伴私信我,问我这边有没有免费的intellijIdea的激活码,然后我将全栈君台教程分享给他了。激活成功之后他一直表示感谢,哈哈~IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html…

    2022年3月29日
    156
  • [ACM] POJ 1442 Black Box (堆,优先队列)

    [ACM] POJ 1442 Black Box (堆,优先队列)

    2022年1月31日
    37
  • 无类路由计算方法_actin

    无类路由计算方法_actin给定一个包含 n 个点 m 条边的有向图,并给定每条边的容量和费用,边的容量非负。图中可能存在重边和自环,保证费用不会存在负环。求从 S 到 T 的最大流,以及在流量最大时的最小费用。输入格式第一行包含四个整数 n,m,S,T。接下来 m 行,每行三个整数 u,v,c,w,表示从点 u 到点 v 存在一条有向边,容量为 c,费用为 w。点的编号从 1 到 n。输出格式输出点 S 到点 T 的最大流和流量最大时的最小费用。如果从点 S 无法到达点 T 则输出 0 0。数据范围2≤n≤50

    2022年8月9日
    9
  • java山寨qq账号密码验证_java swing实现QQ账号密码输入框

    java山寨qq账号密码验证_java swing实现QQ账号密码输入框本文实例为大家分享了Javaswing仿QQ账号密码输入框,供大家参考,具体内容如下主要思路是自己定义AccountPanel和PasswordPanel类,继承JPanel,通过paintComponent(Graphicsg)方法重绘面板,实现目标样式。先上效果图正常显示效果:鼠标放在控件上时的效果:腾讯QQ登录界面的账号密码输入部分效果比对:下面直接给出代码吧Account…

    2022年7月20日
    13
  • 分享测试自动化的19个教训

    分享测试自动化的19个教训

    2021年9月7日
    54

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号