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

微信小程序—图片色彩分析(拾取图片的配色方案)「建议收藏」这是一款图分析图片配色方案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)
上一篇 2022年6月29日 下午2:00
下一篇 2022年6月29日 下午2:16


相关推荐

  • 使用LinkedHashMap实现LRU算法

    使用LinkedHashMap实现LRU算法

    2021年4月10日
    123
  • Hadoop生态系统全面介绍

    Hadoop生态系统全面介绍Hadoop 作为大数据的分布式计算框架 发展到今天已经建立起了很完善的生态 本文将一一介绍基于 Hadoop 生态的一系列框架和组件 Flume 简介 Flume 是一个分布式 高可用的服务 用于高效收集 聚合和移动大量日志数据 作用 Flume 主要承载的作用是收集各个数据源的事件或日志数据 然后将其 Sink 到数据库架构 Flume 的实现架构原理也非常简单 通过 Agent 代理来实现数据的收集 一个 Agent 包含了 Source channel Sink 三个组件 Source 采集的数据来源

    2025年8月22日
    5
  • 利用nginx搭建RTMP视频点播、直播、HLS服务器

    利用nginx搭建RTMP视频点播、直播、HLS服务器开发环境Ubuntu14.04servernginx-1.8.1nginx-rtmp-modulenginx的服务器的搭建安装nginx的依赖库sudoapt-getupdatesudoapt-getinstalllibpcre3libpcre3-devsudoapt-getinstallopenssllibssl-dev配置并编译nginx使用nginx的默

    2022年6月13日
    27
  • Red5 流媒体 学习(一)

    Red5 流媒体 学习(一)由于公司项目需要 要去研究流媒体服务器

    2026年3月16日
    2
  • 爱比较病毒测试(不是震网病毒特点的是)

    CIH、爱虫、冲击波、熊猫烧香,对这4种网络病毒你了解多少?现在的网络病毒能够分为三类,第一类是“蠕虫”病毒,这类型的病毒是在感染电脑后经过不停地复制自己,将电脑原来的运行和储存资源耗费掉,最终电脑由于资源耗费干净而无法运行直至死机;第二类是“进犯型”病毒,这类病毒和蠕虫病毒不一样,进犯型病毒一旦感染就会对电脑软件甚至是硬件做出破坏性的进犯,威力和破坏性极端强大。最终第三类是“木马”类病毒,这类病…

    2022年4月14日
    267
  • ISSU简介

    ISSU简介1 ISSU 简介在线业务软件升级 ISSU In ServiceSoftw 是一种简便 快速升级设备系统软件的方式 可以有效减少系统软件升级过程中的流量中断时间 提高业务可靠性 在大多数网络中 系统软件的升级都需要重启设备 设备的重启过程中断了业务正常运行和流量转发 解决系统软件升级给业务造成影响的一个有效方法是建立多条等价负载路径 在系统软件升级时可以将业务切换到备

    2026年3月19日
    2

发表回复

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

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