You only get a yellow if you are attempting to play the ball (ie. slide tackle). If you deny a goal scoring opportunity by fouling the player without attempting to play the ball then you get a red (ie. grabbing his jersey from behind).
But if you are outside the box and make a last man foul with a slide tackle, it's a red. Attempting to play the ball doesn't matter in this case.