【问题标题】:Threejs Geometry colorThreejs几何颜色
【发布时间】:2015-04-23 16:29:50
【问题描述】:

我用十六进制值设置几何颜色

RichGeometry.prototype = new THREE.Geometry();

RichGeometry.prototype.constructor = RichGeometry;
function RichGeometry(c) {
c = typeof c !== 'undefined' ? c : 0x00C020;     // color

THREE.Geometry.call(this);

this.color = c;

}

但是当我从创建的对象中获取值时,它会返回一个像 16711680 这样的 RGB 值。为什么?如何获得 HEX 值?

var geometry0 = new RichGeometry(0xff0000);
console.log(geometry0.color);// it returns rgb value like this 16711680

【问题讨论】:

    标签: javascript three.js


    【解决方案1】:

    您可以将颜色存储为THREE.Color 的实例:

    function RichGeometry(c) {
    
        c = (typeof c !== 'undefined') ? c : 0x00C020;
    
        THREE.Geometry.call(this);
    
        this.color = new THREE.Color(c);
    
    }
    

    然后您可以使用THREE.Color 上的方法检索颜色的不同表示。对于您的示例,请尝试:

    var geometry0 = new RichGeometry(0xff0000);
    console.log('0x' + geometry0.color.getHexString());
    

    【讨论】:

      【解决方案2】:
      > foo = 16711680
      16711680
      > '0x'+foo.toString(16)
      '0xff0000'
      

      将数字转换为十六进制字符串并在前面加上'0x'

      你可能想把它包装在像这样的 getter 中:

         this.__defineGetter__('color', function(){
              return '0x'+this.color.toString(16)
          });
      

      【讨论】:

        【解决方案3】:

        当您使用 0xff0000 时,它表示一个整数(即 16711680),这与您使用字符串“0xff0000”时不同。所以要看你想做什么。

        【讨论】:

        • 我需要获取我放入构造函数的初始值。如果我把 0xff0000 我想检索相同的而不是像现在这样的 RGB。
        • 把它作为一个字符串而不是一个整数。
        猜你喜欢
        • 2018-03-24
        • 2015-08-31
        • 1970-01-01
        • 2023-03-19
        • 1970-01-01
        • 2013-11-06
        • 2020-02-24
        • 1970-01-01
        • 2014-02-14
        相关资源
        最近更新 更多