Make if statements run after each other in a for loop

Hi!

How would i do if i want if-statements to start working after eachother? I am making a script that filters out greyish pixels and replaces them with either black or white pixels, after this procedure i want to color the outline of the black pixels red.

This is a fraction of my code with the if statements:

for (int x = 0; x < image.width; x++){
			for (int y = 0; y < image.height; y++){
				color = pixels[(y * image.height)+x];
				obj = GameObject.CreatePrimitive(PrimitiveType.Cube);
				obj.transform.position = new Vector3 (x, y, 0);
				obj.renderer.material.color = color;

				if (0 < color.b && color.b < Color.gray.b && filter == true)
				{
					s = s + 1;
					obj.renderer.material.color = Color.black;
				}
				if (Color.gray.b < color.b && color.b < 1 && filter == true)
				{
					s = s + 1;
					obj.renderer.material.color = Color.white;
				}
				if (color == Color.black && pixels[(y * image.height)+x-1] == Color.white)
				{
					obj.renderer.material.color = Color.red;
					p = p + 1;
				}
			}
		}

The problem is that the scripts first colors the pixels red, and then filters out the grey pixels. Since the grey pixels arent white or black yet i get some pixels that should be red but arent (the ones that originaly was grey).

Can someone give me some tips on how to run the third if-statement after the two previous ones?

Thank you!

//Taegos

2 Answers

2

Can’t you do your loop 3 times instead of once, each with their own if statement?

Can you make an example please?

Hi Tageos,

I do not fully understand your code, but i think there are some flaws in it.

First:

color = pixels[(y * image.height)+x];

If your pixels-array is organized like this:

Picture: width=500, height=3
Array 0 - 499 = Line 0, 500 - 999 = Line 1, 1000 - 1499 = Line 2

So your line should look like

color = pixels[(y * image.width)+x];

Your code works as long as the picture is a square.

Second:
As far as i see, your code does not deal with the possibility that color.b == Color.gray.b.

Third:
Line 18
(y * image.height)+x-1
can lead to a negative index (x and y are zero), which will throw an exception.

Thanks for this, you found alot of potential bugs in my code. I will address these.