0

Hello right now I have a task to do but I am kind of stuck with the color bit. I am working with three.js (a javascript library). In the task at a certain point, I need to convert decimal color code (e.g 12615680 into something like this: #FF0000 or 0xFF0000. The solution should be javascript code (function) which can convert the decimal color to hex.

WestLangley
  • 102,557
  • 10
  • 276
  • 276
  • https://www.npmjs.com/package/hex-to-rgba – Jonathan Dion Jan 06 '18 at 20:43
  • Well, I have made few function trying to convert RGB into hex but the thing is I am not sure what I am doing wrong and if this is even the possible solution, that's why I am here –  Jan 06 '18 at 20:44
  • So the number `12615680` corresponds to `C08000` in hex, is that the color you want from that input? – user3483203 Jan 06 '18 at 20:45
  • I tried that but it didn't work that's the problem, please look at the example above and try getting a color using that, if it works please submit your code! –  Jan 06 '18 at 20:45
  • Yes that is the color I want :D –  Jan 06 '18 at 20:45
  • Problem is I can't make it work... –  Jan 06 '18 at 20:46

1 Answers1

2

In three.js, you can use this pattern to convert a decimal color to a hex string:

var c = new THREE.Color(); // create once and reuse

c.set( 12615680 );

c.getHexString(); // "c08000"

three.js r.89

WestLangley
  • 102,557
  • 10
  • 276
  • 276
  • Please finish it off. var example = c.getHexString(); or like how do I get the value so I can put it in the parameters of the sphere? –  Jan 07 '18 at 16:40
  • There is no 'sphere' in your question. Please do not modify your question after is has been answered. Instead, make a new post and include the code you need help with. – WestLangley Jan 07 '18 at 21:05
  • THREE.Color: Unknown color 0xc08000 –  Jan 15 '18 at 17:24
  • This is the error I get if I use your method I get the same but like this: THREE.Color: Unknown color c08000 –  Jan 15 '18 at 17:24
  • From your profile, you appear to be a beginner. Consider posting your questions on https://discourse.threejs.org. It may be a more beginner-friendly site. Always show your code when you post a question. – WestLangley Jan 15 '18 at 19:18
  • I found the solution my self. Yes, I am a beginner but only to the library. I am not that bad at js, html, and css brother :D –  Jan 15 '18 at 21:42
  • Solution was: after using your, my or any other solution to simply convert the string I get a result into a number which I did like this: Number(stringVar); –  Jan 15 '18 at 21:43