How to compare colors in Shaders?

How do I compare two colors inside the shader fragment function?

I want to make a simple shader to swap a pixel color so I’m using this line:

if (all(c.rgb == _InColor.rgb)) c = _OutColor; 

Both c and _InColor are typed as half4. The colors are not swapping at all.

I known this type of comparation is bad for performance but idk how to make it better.
Suggestions?


Full shader:
Shader "Sprites/Palette 01"
{
	Properties
	{
		[PerRendererData] _MainTex("Sprite Texture", 2D) = "white" {}
		_Color("Tint", Color) = (1,1,1,1)    
		_InColor("In Color", Color) = (0,0,0,0)    
		_OutColor("Out Color", Color) = (0,0,0,0) 
		[MaterialToggle] PixelSnap("Pixel snap", Float) = 0
		[HideInInspector] _RendererColor("RendererColor", Color) = (1,1,1,1)
		[HideInInspector] _Flip("Flip", Vector) = (1,1,1,1)
		[PerRendererData] _AlphaTex("External Alpha", 2D) = "white" {}
		[PerRendererData] _EnableExternalAlpha("Enable External Alpha", Float) = 0
	}

		SubShader
		{
			Tags
			{
				"Queue" = "Transparent"
				"IgnoreProjector" = "True"
				"RenderType" = "Transparent"
				"PreviewType" = "Plane"
				"CanUseSpriteAtlas" = "True"
			}

			Cull Off
			Lighting Off
			ZWrite Off
			Blend One OneMinusSrcAlpha

			Pass
			{
			CGPROGRAM
				#pragma vertex SpriteVert
				#pragma fragment PaletteSwapFrag
				#pragma target 2.0
				#pragma multi_compile_instancing
				#pragma multi_compile_local _ PIXELSNAP_ON
				#pragma multi_compile _ ETC1_EXTERNAL_ALPHA
				#include "UnitySprites.cginc"

				half4  _InColor;
				half4  _OutColor;

				half4 PaletteSwapFrag(v2f IN) : SV_Target
				{
					half4 c = tex2D(_MainTex, IN.texcoord);    
					if (all(c.rgb == _InColor.rgb)) c = _OutColor;    
					c.rgb *= c.a * IN.color;
					return c;
				}
			ENDCG
		}
		}
}

could this work (note that I made up the variable names, 0.1 is the tolerance value; you can add it as a parameter as well)

half3 delta = abs(texColor.rgb - colorKey.rgb);
half3 rgb =length(delta) < 0.1 ? replace_color.rgb : IN.color.rgb;
result.color = half4(rgb, IN.color.a);

with something like this you usualy need some sort of tollerance.

//instead of doing this

if(color1==color2){do stuff}

// try something like this instead:


if(Mathf.Abs (color1.r-color2.r)<.1f){
if(Mathf.Abs (color1.b-color2.b)<.1f){
if(Mathf.Abs (color1.g-color2.g)<.1f){
do stuff
}}}

actual color channel numbers can slightly change because of compression.

when you save a photo. its standard in most formats that color channels are rounded off and averaged. so pixels color numbers may not be EXACTLY what they started with. this reduces size.

checking colors the the == operator is not going to do you any good. unless nothing is compressed

also unity has default texture import settings that also can effect these numbers when unity loads them.

an alternate answer whould be to make sure you are saving your pics in a lossless format and go through unitys texture import settings and make sure that is lossless too. but these things are in place for proficiency reasons. so unless you have very small textures i would recommend the “tollerance” approach to solve your problem