简体   繁体   English

WebGL着色器中的十六进制到RGB值

[英]Hexadecimal to RGB values in WebGL Shader

I'm working on an application where I would like to take a single integer input (basically a color) and, using WebGL shaders, color a box with the given input. 我正在开发一个应用程序,我想在其中使用单个整数输入(基本上是一种颜色),并使用WebGL着色器为具有给定输入的框着色。 I planned, originally, to do this with a combination of shifts & a mask as demonstrated below: 我最初计划用变速和面罩的组合做到这一点,如下所示:

uniform int u_color;

float rValue = (((u_color) >> 16) & 0xFF) / 255.0;
float gValue = (((u_color) >> 8) & 0xFF) / 255.0;
float bValue = ((u_color) & 0xFF) / 255.0;
gl_FragColor = vec4(rValue, gValue, bValue, 1.0);

so given the int 0xFF33CC, red=1.0, green=0.2, blue=0.8 所以给定int 0xFF33CC,红色= 1.0,绿色= 0.2,蓝色= 0.8

However, I ran into a problem and found out that WebGL shaders can't perform bitwise shifts. 但是,我遇到了一个问题,发现WebGL着色器无法执行按位移位。

I'm wondering how would I be able to efficiently produce the proper FragColor from the given integer, if that's even possible. 我想知道如何能够从给定的整数中有效地生成适当的FragColor,如果可能的话。

EDIT: After a bit of trial and error, and thanks to @Jongware, I've come up with a solution 编辑:经过一些试验和错误,并感谢@Jongware,我想出了一个解决方案

uniform int u_color;
float rValue = float(u_color / 256 / 256);
float gValue = float(u_color / 256 - int(rValue * 256.0));
float bValue = float(u_color - int(rValue * 256.0 * 256.0) - int(gValue * 256.0));
gl_FragColor = vec4(rValue / 255.0, gValue / 255.0, bValue / 255.0, 1.0);

Other than a clean up, this code is perfect for the job, however I'd always be interested in any other methods different from the one mentioned above. 除了清理之外,这段代码非常适合这项工作,但是我总是对其他任何与上述方法不同的方法感兴趣。

To add to your own answer, consider staying with integer math until the end. 要添加到您自己的答案,请考虑使用整数数学直到结束。

uniform int u_color;
unsigned rIntValue = (u_color / 256 / 256) % 256;
unsigned gIntValue = (u_color / 256      ) % 256;
unsigned bIntValue = (u_color            ) % 256;
gl_FragColor = vec4(rIntValue / 255.0f, gIntValue / 255.0f, bIntValue / 255.0f, 1.0);

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

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