# Recipe 43: Chromakey: Replace all green and brown with the new background

def chromakey(source, bg):
  # Source should have something in front of blue, bg is the new background
  # Both pictures have to be of the same size!

  for x in range(1, getWidth(source)+1):
    for y in range(1, getHeight(source)+1):
      p = getPixel(source,x,y)

      # Define background: If the redness < greenness
      if (getRed(p)) < (getGreen(p)):
        # Then grab the pixel from the new background
        setColor(p, getColor(getPixel(bg,x,y)))
      # Define background: If the theres alot of brown
      elif ((getBlue(p) + getGreen(p)) > (getRed(p))):
        # Then grab the pixel from the new background
        setColor(p, getColor(getPixel(bg,x,y)))

  return source