Вопрос

i have small problem in my project with getting hex color. I have simple variable with colors:

var colors = {
    "C":0x000000,
    "H":0xffffff,
    "O":0xff0000,
    ...
}

I want to get my color by key in function below: (it is written in typescript)

getAtomColor(element: string) :number{
    for (var key in colors) {
        if (element == key) {
            return colors[key];
        }
    }
}

Problem is with got atom color (below parameter clr), it is in integer form and function in three.js THREE.MeshLambertMaterial({color:clr}) has undefined parameters. How can I fix this problem?

Это было полезно?

Решение

You should be able to adjust your object to hold the hexadecimal strings - this would guarantee that you get back '0x000000' rather than 0.

var colors = {
    'C': '0x000000',
    'H': '0xffffff',
    'O': '0xff0000',
}

Otherwise, you could store them as THREE.color objects...

var colors = {
    'C': new THREE.Color(0x000000),
    'H': new THREE.Color(0xffffff),
    'O': new THREE.Color(0xff0000),
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top