最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JS 两个值之间的色差相似度%

SEO心得admin73浏览0评论
本文介绍了JS 两个值之间的色差/相似度%的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要计算两个十六进制颜色值之间的差异,因此输出是一个百分比值.我放弃的第一件事是将十六进制值转换为十进制值,因为第一个的权重会比最后一个高得多.

I need to compute the difference between two hex color values so the output is a percentage value. The first thing I discarted was converting the hex value into decimal, as the first one will have much higher weight than the last.

第二个选项是计算每个 RGB 值之间的差异,然后将它们全部相加.但是,0, 0, 0 和 30, 30, 30 之间的差异远小于 0, 0, 0 和90, 0, 0.

The second option is to compute the difference between each of the RGB values and then add them all. However, the difference between 0, 0, 0 and 30, 30, 30 is much lower than the one between 0, 0, 0 and 90, 0, 0.

这个问题 建议使用 YUV,但我不知道如何使用它来建立区别.

This question recommends using YUV, but I can't figure out how to use it to establish the difference.

另外,另一个问题有一个很好的公式来计算差异并输出一个 RGB 值,但它并不完全存在.

Also, this other question has a nice formula to compute the difference and output a RGB value, but it's not quite there.

推荐答案

只需计算一个 欧几里得距离:

var c1 = [0, 0, 0], c2 = [30, 30, 30], c3 = [90, 0, 0], distance = function(v1, v2){ var i, d = 0; for (i = 0; i < v1.length; i++) { d += (v1[i] - v2[i])*(v1[i] - v2[i]); } return Math.sqrt(d); }; console.log( distance(c1, c2), distance(c1, c3), distance(c2, c3) ); //will give you 51.96152422706632 90 73.48469228349535

发布评论

评论列表(0)

  1. 暂无评论