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

微信小程序—图片色彩分析(拾取图片的配色方案)「建议收藏」这是一款图分析图片配色方案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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • dw自动滚动图片_DW图片无缝滚动代码

    dw自动滚动图片_DW图片无缝滚动代码DIV的图片无缝滚动,DIV图片上无缝滚动,DIV图片下无缝滚动,DIV图片左无缝滚动,DIV图片右无缝滚动1.先了解一下对象的几个的属性:innerHTML:设置或获取位于对象起始和结束标签内的HTMLscrollHeight:获取对象的滚动高度。scrollLeft:设置或获取位于对象左边界和窗口中目前可见内容的最左端之间的距离scrollTop:设置或获取位于对象最顶端和窗口中可见内容的最顶…

    2022年7月18日
    12
  • 移掉 K 位数字

    移掉 K 位数字

    2020年11月19日
    204
  • adminLTE的介绍

    adminLTE的介绍一.adminLTE的介绍adminLTE的官方网站:adminLTE官方网站和github:adminLTE的github演示地址:adminLTE演示地址adminLTE是基于bootstrap3的前端框架,并且将bootstrap3进行修改来适应自身的样式。adminLTE除了可以使用bootstrap3的大多数样式之外,自身也提供了一些非常实用的样式包装,并且在样式演

    2022年7月27日
    0
  • Attempt to invoke virtual method on a null object reference 完美解决

    Attempt to invoke virtual method on a null object reference 完美解决

    2021年10月2日
    69
  • DOS下的Tree命令

    DOS下的Tree命令    有时候我们想了解一个文件夹或者驱动器根目录下的所有文件,并希望它以资源管理器的树形视图方式显示文件结构。如果你使用的是Windows 2000,可以在“命令提示符”窗口中输入“tree D:Inetpub”命令(这里的 D:Inetpub可以根据需要改成要你查阅的文件路径),就会看到D:Inetpub下的所有文件夹以树形结构清楚地显示出来。    此外,你还可以加入以下参数以增强“tree

    2022年7月24日
    8
  • Thread.IsBackground 属性

    Thread.IsBackground 属性net提供了Thread类用于线程的操作。当初始化一个线程,把Thread.IsBackground=true的时候,指示该线程为后台线程。后台线程将会随着主线程的推出而退出。后台线程不妨碍程序的终止,只要所有前台线程都终止后,CLR就会对每一个活在的后台线程调用Abort()来彻底终止应用程序。【注意】当在主线程中创建了一个线程,那么该线程的IsBackground默认是设置为FALS…

    2022年8月31日
    0

发表回复

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

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