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

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


相关推荐

  • Keil中代码的颜色设置 ( 很 全 )[通俗易懂]

    Keil中代码的颜色设置 ( 很 全 )[通俗易懂]因为长时间要编程,对于keil上的黑字白底,如果看久了会让人眼睛产生疲倦感,所以最好将代码颜色和底色都改变一下,让自己看起来舒服一点。下面是文字图片教程(如果自己没空去设置,我后面也有两套模板可以直接用,教程也在后面)1、点击keil右上角edit——Configuration——Color&Fonts(上面标签)2、3、(注意事…

    2022年5月10日
    220
  • ucosii操作系统内核源码学习第一篇

    ucosii操作系统内核源码学习第一篇待会就开始学习

    2022年5月6日
    34
  • Wappalyzer 网站技术分析软件「建议收藏」

    Wappalyzer 网站技术分析软件「建议收藏」Wappalyzer工具支持分析目标网站所采用的平台构架、网站环境、服务器配置环境、JavaScript框架、编程语言等参数,同时还可以显示目标站点使用该技术的网站比例,例如有多少网站使用的是Wordpress、有多少网站使用AddThis第三方服务,其他还有网页服务器、分析工具、CDN、留言系统、控制台、网络空间等等,可以让你从使用比例中得出目前最流行的技术。使用方法:1.打开

    2022年5月12日
    77
  • datetime.date()_datenum函数使用

    datetime.date()_datenum函数使用比如在windowscmd命令行窗口执行date命令后这个环境变量的值为当前日期:2014-03-01 星期六那么如下的各个操作的意义如下:%date:~0,4% 表示从左向右指针向右偏0位,然后从指针偏移到的位置开始提取4位字符,结果是2014(年的值)%date:~5,2% 表示指针从左向右偏移5位,然后从偏移处开始提取2位字符,结果是03(月的值)%date:~8,

    2022年9月20日
    0
  • Asp.net Web.Config – 配置元素customErrors

    Asp.net Web.Config – 配置元素customErrorsAsp.netWeb.Config-配置元素customErrors Asp.net配置文件的配置方式,其实在MSDN里面是写得最清楚的了。可惜之前一直未曾了解到MSDN的强大。  先贴个地址:http://msdn.microsoft.com/zh-cn/library/dayb112d(v=vs.80).aspx,然后逐个分析。我希望自己能够从头到尾认真学完这系列东西。为了不至于让自己太早放弃,我决定从自己用过的配置文件学起,然后逐渐接触那些比较生疏,和少用的。一、customErro

    2022年7月16日
    12
  • flash怎么强制gc_Adode Flash初级教程

    flash怎么强制gc_Adode Flash初级教程AdodeFlash初级教程(湖南信息职业技术学院xxgc.hniu.cn教务处,湖南长沙410200)第一章入门一、概述1、定义:矢量二维动画2、适用范围:1.针对与网页2.动漫3、特点:a矢量动画制作模式,文件容量小b支持多类型文件导入(图片、视频、音频)c支持流媒体技术d交互动画4、网页四剑客简介adobedreamweaver:是一个网页排版软件,不是设计软件,唯一体现设计软件的地方是c…

    2022年9月2日
    2

发表回复

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

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