Yes, reading the manual would help. There is a paint mode called formula and you can define your own shit in it in the settings dialogue under Mode
Here, this will make the fg colour paint ONLY over the bg one:
if( a==bg, fg, a )
and this the other way round which you put in the 2nd dialoge when adding a new formula
if( a==fg, bg, a )
That was not hard, now was it. Learn to read before you bitch around, seriously.