如何在 A-Frame 中进行颜色解析以从颜色字符串中查找 r/g/b 的数量

  • 本文关键字:颜色 查找 字符串 A-Frame aframe
  • 更新时间 :
  • 英文 :


如果我试图找到绿色或红色等的数量。 我应该对此进行字符串解析吗:

document.getElementById("targetElement").getAttribute("material").color

输出类似于"rgb(0,0,0)",因此我可以使用正则表达式匹配来查找绿色。有没有更好的方法可以做到这一点?

您可以使用

THREE.Color实用程序 https://threejs.org/docs/api/math/Color.html

THREE.Color('rgb(10, 20, 30)').r // 10

试试那个:

var greenElements = document.querySelectorAll("*").filter(function () { 
    return this.getAttribute("material") == 'rgb(0,255,0)' 
});
console.log(greenElements.length);

如果您了解有关要搜索的元素的更多信息,则可以更改选择器。我使用了所有匹配的选择器,如果您有很多 html 要解析,可能会"慢"。

最新更新