Why is this if statement failing on a Compute Shader?

I am trying to use a compute shader to change the colour of an image, and if I add no conditions this works fine. However, if I want to add an if statement to only change the pixels of a particular colour to this new colour, no pixels are changed at all.

#pragma kernel CSMain

struct ColorStruct {
    float4 col;

RWTexture2D<float4> Result;
RWStructuredBuffer<ColorStruct> data;

void CSMain (uint3 id : SV_DispatchThreadID)
   // ColorStruct c = data[0];
   // c.col = Result[id.xy];
   // data[id.x] = c;

    if (all(Result[id.xy] == float4(0.937f, 0.110f, 0.145f, 1.000f)))
        Result[id.xy] = float4(0, 0, 0, 1);
   // Result[id.xy] = float4(id.x & id.y, (id.x & 15)/15.0, (id.y & 15)/15.0, 0.0);

I have verified through the RWStructuredBuffer (see the 3 commented lines) that the colour is indeed (0.937f, 0.110f, 0.145f, 1.000f) as this is outputted to me, but for some reason it isn’t picking up this colour within the if statement.

This is the code I use to call the shader:

    public struct ColorStruct
        public Color col;

    private ColorStruct[] data = new ColorStruct[2];

    // Start is called before the first frame update
    void Start()
        tex = new RenderTexture(map.width / 2, map.height / 2, 0);
        ComputeBuffer buf = new ComputeBuffer(map.width * map.height, 4);

        tex.enableRandomWrite = true;
        Graphics.Blit(map, tex);
        shader.SetBuffer(0, "data", buf);
        shader.SetTexture(0, "Result", tex);

        shader.Dispatch(0, tex.width / 8, tex.height / 8, 1);



I can’t for the life of me figure out why the if statement is being ignored when the colour values are identical to what the buffer outputs.

If I’m not mistaken, your red channel (for example) is 239.

239/255 = ~0.93725490196078431372549019607843

(according with Windows calculator)

In other words, the 3-decimal readout you’re basing it on isn’t an EXACT match to the color.

More importantly, however, trying to get an exact floating-point match in a shader is a recipe for disaster. While it’s more expensive to process, it could be worth checking whether the color is simply “within one” of the target:

// 1/255 = 0.0039215686274509803921568627451
if(length(Result[id.xy] - float4(0.937f, 0.110f, 0.145f, 1.000f)) < 0.00391)
	Result[id.xy] = float4(0, 0, 0, 1);

Or, multiply “Result[id.xy]” by 255 and compare with integer-equivalents to the same effect

if(length((Result[id.xy] * 255) - float4(239, 28, 37, 255)) < 0.98)