简体   繁体   English

使用来自three.js的颜色选择器的十六进制值作为字符串

[英]Using a HEX value from a color picker for three.js as a string

So I have a color picker, which outputs colors in this format: FFA6A6 .所以我有一个颜色选择器,它以这种格式输出颜色: FFA6A6 It is a plugin from jscolor.com.它是来自 jscolor.com 的插件。 I am using three.js to change the color of something live when I change it with the color picker.当我使用颜色选择器更改某些内容时,我正在使用three.js 来更改它的颜色。 However, three.js takes 0xFFA6A6, not just that.但是,three.js 需要 0xFFA6A6,而不仅仅是这样。 On top of that, I can not concatenate 0x to FFA6A6, which would make it a string.最重要的是,我无法将 0x 连接到 FFA6A6,这会使其成为字符串。 Also, it outputs colors as a string, and I cannot seem to remove it from being a string此外,它将颜色输出为字符串,我似乎无法将其从字符串中删除

ex:"FFA6A6" to FFA6A6例如:“FFA6A6”到 FFA6A6

How would I change FFA6A6 to something that can be recognized as a three.js color?我如何将FFA6A6更改为可以识别为 Three.js 颜色的颜色? This is what I have:这就是我所拥有的:

function updateNoseColor(){
    scene.remove(nose);
    var geometry = new THREE.ConeGeometry( .4, 1, 32 );

    var material = new THREE.MeshBasicMaterial( {color: document.getElementById("nosecolor").value} );
    //three.js cannot take a string as a value, so im not sure what to do
    var nose = new THREE.Mesh( geometry, material );
    scene.add(nose);
}

Thank you for your time, there is probably an extremely simple solution that I do not know of.感谢您抽出宝贵时间,可能有一个我不知道的极其简单的解决方案。 Again, the problem is removing the quotes, and adding it to 0x Thanks!同样,问题是删除引号,并将其添加到0x谢谢!

EDIT : I have tried converting to a decimal with ParseInt, it is not accepted by three.js编辑:我尝试使用 ParseInt 转换为小数,但three.js 不接受

You can use the Color class ( reference ) from three.js.您可以使用来自three.js 的Color类(参考)。 For example:例如:

var color = new THREE.Color("#FFA6A6"); // "FFA6A6" won't work!
color.getHex(); // 0xFFA6A6

That said, replace your following code with the given code:也就是说,给定的代码替换您的以下代码:

var material = new THREE.MeshBasicMaterial( {color: document.getElementById("nosecolor").value} );

with

var material = new THREE.MeshBasicMaterial( {color: new Color(document.getElementById("nosecolor").value)} );

if document.getElementById("nosecolor").value = "#FFA6A6" if document.getElementById("nosecolor").value = "#FFA6A6"

else with否则与

var material = new THREE.MeshBasicMaterial( {color: new Color("#" + document.getElementById("nosecolor").value)} );

if document.getElementById("nosecolor").value = "FFA6A6" (no # in the start) if document.getElementById("nosecolor").value = "FFA6A6" (开头没有#)

For example , run the below snippet (inspired from a three.js example ).例如,运行下面的代码片段(灵感来自一个 Three.js 示例)。

 function createSquare() { var scene = new THREE.Scene(); var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000); var renderer = new THREE.WebGLRenderer(); renderer.setSize(window.innerWidth, window.innerHeight); document.body.appendChild(renderer.domElement); var geometry = new THREE.BoxGeometry(1, 1, 1); /* START - EXACT ANS */ var a = "#"+document.getElementById("nosecolor").value; var material = new THREE.MeshBasicMaterial({ color: (new THREE.Color(a)) }); /* STOP - EXACT ANS */ var cube = new THREE.Mesh(geometry, material); scene.add(cube); camera.position.z = 5; function animate() { requestAnimationFrame(animate); renderer.render(scene, camera); } animate(); } createSquare();
 <input type="text" id="nosecolor" value="FFA6A6"> <script src="http://threejs.org/build/three.min.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM