c - Hexadecimal to RGB values in WebGL Shader -
i'm working on application take single integer input (basically color) and, using webgl shaders, color box given input. planned, originally, combination of shifts & mask 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 int 0xff33cc, red=1.0, green=0.2, blue=0.8
however, ran problem , found out webgl shaders can't perform bitwise shifts.
i'm wondering how able efficiently produce proper fragcolor given integer, if that's possible.
edit: after bit of trial , error, , @jongware, i've come solution
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 clean up, code perfect job, i'd interested in other methods different 1 mentioned above.
to add own answer, consider staying integer math until 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(rint / 255.0f, gintvalue / 255.0f, bintvalue / 255.0f, 1.0);
Comments
Post a Comment