question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

THREE.Color getHex() sometimes returns negative values

See original GitHub issue

Describe the bug

Sometimes, when creating a Threejs Color from 3 separate rgb values the color gets created in a strange way that all the color manipluation related to getting hex values returns wrong values.

Code to Reproduce

Steps to reproduce the behavior:

var r = [Math.random()*256, Math.random()*256, Math.random()*256]; var rand_color = new THREE.Color(r[0], r[1], r[2]);

console.log(r);
console.log(rand_color);
console.log(rand_color.getHex());//this is sometimes negative
console.log(rand_color.getHexString()); //this string is then affected too 

For example, these are values that create such negative color:

var c = new THREE.Color(172.10908897579378, 171.64128648818325, 101.05378373805644);
console.log(c.getHex()); //returns -1413112664
console.log(c.getHexString()); //returns '3a6358', which is different color than what THREE.js internally represents

edit: This does not always happen with decimals only as i previously thought, it happens with integers too, for example

var c = new THREE.Color(241, 52, 177);
console.log(c.getHex()) // -264471473

Expected behavior

Getting the same hex value as the one that THREE.js represents internally too.

Platform:

  • Device: Desktop
  • OS: Windows
  • Browser: Chrome, Firefox
  • Three.js version: r124

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
mrdoobcommented, Feb 25, 2022

Both answers are incorrect.

But I think returning black when the color is below 0, and white when the color is above 1 is easier to understand than returning a negative hex.

That’s also how the web behaves:

// Makes the background white and doesn't warn the user that colors go from 0 to 255
document.body.style.backgroundColor = 'rgb(512, 512, 512)'

// Makes the background black and doesn't warn the user that colors go from 0 to 255
document.body.style.backgroundColor = 'rgb(-512, -512, -512)'

There may be other methods where this may make sense but I do not think we should clamp Color.

1reaction
mrdoobcommented, Feb 25, 2022

We can reconsider if we get any reports.

Read more comments on GitHub >

github_iconTop Results From Across the Web

return string on color.getHex()? #2520 - mrdoob/three.js - GitHub
So I think getHex() doesn't quite do what I was expecting as it returns a decimal value. For instance after using setHex(0xFFFFFF) getHex ......
Read more >
Issue in Coloring mesh in Three,js - javascript - Stack Overflow
var color = new THREE.Color( "#ff0080" ); var hex = color.getHex(); var material = new THREE.MeshBasicMaterial( { color: hex } );. three.js r.58....
Read more >
Color – three.js docs
Sets this color's components from the attribute. # .getHex ( colorSpace : string = SRGBColorSpace ) : Integer. Returns the hexadecimal value of...
Read more >
hexdec - Manual - PHP
Returns the decimal equivalent of the hexadecimal number represented by the hex_string argument. hexdec() converts a hexadecimal string to a decimal number.
Read more >
Google Sheets: Mark Positive Number Green and Negative ...
Learn how to turn positive numbers green and negative numbers red in Google Sheets, an important technique for showing gains and losses.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found