javascript提取图片主要颜色

3 篇文章 0 订阅
2 篇文章 0 订阅

本文主要整理前人的文章,并增强色彩饱和度,在此记录留用

基础html结构

<span style='display: inline-block;width:200px;height: 200px;' id='span'>预览</span>
<img id="imgs" src="http://127.0.0.1:8081/rgb1.jpg" alt="" width="300" height="400">

借助 canvas 提取图片主要 rgb 色值


	var img = document.getElementById('imgs');
    var span = document.getElementById('span');

    function getImageColor(img) {
      var canvas = document.createElement('canvas')
      canvas.width = img.width;
      canvas.height = img.height;

      var context = canvas.getContext("2d");
      img.crossOrigin = "Anonymous"
      context.drawImage(img, 0, 0, canvas.width, canvas.height);

      // 获取像素数据
      var data = context.getImageData(0, 0, img.width, img.height).data;
      console.log(data)
      var r = 1,
        g = 1,
        b = 1;
      // 取所有像素的平均值
      for (var row = 0; row < img.height; row++) {
        for (var col = 0; col < img.width; col++) {
          // console.log(data[((img.width * row) + col) * 4])
          if (row == 0) {
            r += data[((img.width * row) + col)];
            g += data[((img.width * row) + col) + 1];
            b += data[((img.width * row) + col) + 2];
          } else {
            r += data[((img.width * row) + col) * 4];
            g += data[((img.width * row) + col) * 4 + 1];
            b += data[((img.width * row) + col) * 4 + 2];
          }
        }
      }

      console.log(r, g, b)
      // 求取平均值
      r /= (img.width * img.height);
      g /= (img.width * img.height);
      b /= (img.width * img.height);

      // 将最终的值取整
      r = Math.round(r);
      g = Math.round(g);
      b = Math.round(b);
      console.log(r, g, b)

      return [r, g, b]
    }

将rgb色转位hsl色并增强 s,l值

   function rgbtohsl(rgb) {
      r = rgb[0] / 255;
      g = rgb[1] / 255;
      b = rgb[2] / 255;

      var min = Math.min(r, g, b);
      var max = Math.max(r, g, b);
      var l = (min + max) / 2;
      var difference = max - min;
      var h, s, l;
      if (max == min) {
        h = 0;
        s = 0;
      } else {
        s = l > 0.5 ? difference / (2.0 - max - min) : difference / (max + min);
        switch (max) {
          case r:
            h = (g - b) / difference + (g < b ? 6 : 0);
            break;
          case g:
            h = 2.0 + (b - r) / difference;
            break;
          case b:
            h = 4.0 + (r - g) / difference;
            break;
        }
        h = Math.round(h * 60);
      }
      s = Math.round(s * 100 * 1.5)+ '%'; //转换成百分比的形式
      l = Math.round(l * 100 * 0.8)+ '%';
      const str = "hsl(" + h + "," + s + "," + l + ")";
      console.log('str', str)
      return str
    }

调用方法

   img.onload = function () {
      const rgb = getImageColor(img)
      const hsl = rgbtohsl(rgb)
      span.style.background = hsl
    }
  
  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Vue3 + TypeScript 中提取图片颜色代码和解析 CSS 提取图片主题色的方法与 Vue2 + JavaScript 基本一致,只是需要在 Vue3 的 Composition API 中使用。 1. 如何提取图片颜色代码 可以使用 Vue3 的生命周期函数 onMounted 来实现提取图片颜色代码的功能。具体步骤如下: 1) 在 template 中添加一个 image 元素。 ``` <template> <div> <img ref="image" src="图片链接" /> </div> </template> ``` 2) 在 setup 函数中获取 image 元素,并将图片绘制到 Canvas 上。 ``` import { onMounted } from 'vue'; export default { setup() { const imageRef = ref(null); onMounted(() => { const canvas = document.createElement('canvas'); const ctx = canvas.getContext('2d'); const img = imageRef.value; img.onload = () => { canvas.width = img.width; canvas.height = img.height; ctx.drawImage(img, 0, 0, img.width, img.height); }; }); return { imageRef, }; }, }; ``` 3) 获取 Canvas 上某个像素的颜色代码。 ``` import { ref } from 'vue'; const pixelData = ctx.getImageData(x, y, 1, 1).data; const colorCode = '#' + ((1 << 24) + (pixelData[0] << 16) + (pixelData[1] << 8) + pixelData[2]).toString(16).slice(1); ``` 其中 x 和 y 表示像素的坐标,pixelData 是一个长度为 4 的数组,分别表示 RGBA 四个通道的值。将 RGB 三个通道的值拼接成一个 16 进制数,并加上前缀“#”就是颜色代码了。 2. 解析 CSS 提取图片主题色功能 可以使用第三方库 color-thief 来实现解析 CSS 提取图片主题色的功能。具体步骤如下: 1) 安装 color-thief 库。 ``` npm install color-thief --save ``` 2) 在 Vue 组件中引入 color-thief 库。 ``` import ColorThief from 'color-thief'; ``` 3) 在 setup 函数中创建一个 Image 元素,并将图片加载到 Image 上。 ``` const colorThief = new ColorThief(); const img = new Image(); img.src = '图片链接'; img.onload = () => { const color = colorThief.getColor(img); console.log(color); }; ``` colorThief.getColor(img) 函数的返回值是一个长度为 3 的数组,分别表示 RGB 三个通道的值。将 RGB 三个通道的值拼接成一个 16 进制数,并加上前缀“#”就是图片的主题色了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值