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

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


相关推荐

  • 签名设计手写简单_怎么写自己的签名

    签名设计手写简单_怎么写自己的签名首语:大家好,很多人都想设计一个简单的艺术签名,所以就有人开始找【签名设计手写简单】,这其实对于设计师来说的很简单的事情。写字的时候也有简单的时候,也有复杂的时候。真的需要怎么去取舍,这就要看你是如何去选择了。签名设计手写简单首先说一下,三秒艺术签名网的所有签名都是手写完成的,大家这一点不要质疑。其次是说到简单,其实写签名就是要让名字变得简单,好写,这样才会让人感觉,舒畅。但是不是什么字都能被写的简单的,这需要考虑一个文字的笔画多少程度,举个例子“饕餮”这两个字就不好去做简化。像,明啊,等啊,峰啊

    2025年9月22日
    7
  • 一、智能车舵机控制

    一、智能车舵机控制前言:本文章主要是近期有关舵机知识的总结,将分别从舵机的控制原理,控制流程和代码实现流程几个方面作简要介绍,由于时间紧急,难免有疏漏错误之处,欢迎留言指正,QQ:2046890259一、多级的控制原理:我们本次智能车使用的舵机是通过PWM进行控制。而PWM几个重要的参数就是最大值,最小值和占空比。其中占空比决定了舵机的旋转角度,如下图所示:不同的占空比控制不同的角度。而我们的目的就是通过…

    2022年6月20日
    39
  • TFS2010安装部署

    TFS2010安装部署TFS2010安装过程TFS疑难问题解答TFS文档不能查看TFS报表不能查看

    2022年9月24日
    2
  • CSS绘画三角形

    CSS绘画三角形本案例使用CSS绘画了一个三角形的图案。预览演示地址:https://tympanus.net/codrops-playground/SaraSoueidan/kFTn9yJk/editor将下面的代码复制到演示地址对应位置即可查看效果&lt;!–HTML–&gt;&lt;divclass="container"&gt;&lt;divclass="eleme…

    2022年6月26日
    28
  • MySQL常见的数据类型[通俗易懂]

       不多说,直接上干货!       MySQL常见的数据类型一、数据类型是什么?  数据类型是指列、存储过程参数、表达式和局部变量的数据特征,它决定了数据的存储格式,代表了不同的信息类型。  有一些数据是要存储为数字的,数字当中有些是要存储为整数、小数、日期型等… 二、MYSQL常见数据类型  MySQL支持多种类型,大致可以…

    2022年4月5日
    44
  • python pycharm教程_Pycharm简单使用教程(入门小结)

    python pycharm教程_Pycharm简单使用教程(入门小结)1、下载pycharmpycharm是一种PythonIDE,能够帮助我们在编写代码时提高效率。网上提供的有专业版和教育版之分。专业版是收费的,功能更全面点。教育版或社区版是阉割版本,但它是免费的。2、pycharm的安装比较简单,官网下载备注:刚下载好的pycharm无法运行程序“Cannotstartprocess,theworkingdirectory…”,两种解决方法1.选择…

    2022年8月29日
    3

发表回复

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

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