To do that we need to select all the red pixels. But there are a lot of different “reds”. Obviously, a pixel with the color red = 255, green = 0, and blue = 0 is red. But most of the pixels in the scooter aren’t pure red. The red channel might not be 255. Or the blue or green might not be 0. Furthermore, a pixel with color values (255, 255, 255) has the maximum red value… but it is not red, it is white. Here are three examples of colors that might be part of the scooter:
We will say that if a pixel’s red value is at least 10 greater than both the green and the blue values, it is “red”. To express this as a conditional will look like r >= g + 20 and r >= b + 20, where r, g, b are the red, green, and blue color values. To test out our recipe, we are going to make a function isRed. It will accept values for r, g, and b and return True if they meet our criteria and false otherwise.
Now that we have some evidence that the function works, we can use it as the condition for our if statement. Because the function returns True or False, it can serve as the condition for the if: if isRed(r, g, b):.
If the pixel is determined to be “red”, we need to change it to a similar blue. A dark red should become a dark blue and a light red a light blue. The easiest way to do this is to swap the red and blue values. Like this:
A simpler test for “is it red” might just be to see if the red value is 250 or more and not worry about the green or blue. What happens if you change the condition in isRed to read if r >= 250:?