0

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?

1 Answer 1

1

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),
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.