THREE.Color getHex() sometimes returns negative values
See original GitHub issueDescribe 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:
- Created 2 years ago
- Comments:12 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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:
There may be other methods where this may make sense but I do not think we should clamp
Color
.We can reconsider if we get any reports.