I have a CIKernal of a threshold filter in GLSL like this:
let thresholdKernel = CIColorKernel(string:
"kernel vec4 thresholdFilter(__sample pixel, float threshold)" +
"{ " +
" float luma = (pixel.r * 0.2126) + " +
" (pixel.g * 0.7152) + " +
" (pixel.b * 0.0722); " +
" return vec4(step(threshold, luma)); " +
"} "
)”
I want to check if pixel is white. Is there a simple command in GLSL to do this without extra computations?
Update** I want to get rid of luma calculation. So is there a way to check pixel being white without doing above luma calculation?
Th oixel is "white" if each of the the tree color channels is >= 1.0
. This can be checked by testing if the sum of the color channels is 3.0. Of course it has to be ensured that the three color channels are limited to 1.0 first:
bool is_white = dot(vec3(1.0), clamp(lightCol.rgb, 0.0, 1.0)) > 2.999;
or
float white = step(2.999, dot(vec3(1.0), clamp(lightCol.rgb, 0.0, 1.0)));
In ths case min(vec3(1.0), lightCol.rgb)
can be used instead of clamp(lightCol.rgb, 0.0, 1.0)
too.
If it is well known, that each of the three color channels is <= 1.0
, then the expression can be simplified:
dot(vec3(1.0), lightCol.rgb) > 2.999
Note, in this case the dot
product calculates:
1.0*lightCol.r + 1.0*lightCol.g + 1.0*lightCol.b
and luma
can be calculated as follows:
float luma = dot(vec3(0.2126, 0.7152, 0.0722), lightCol.rgb);