I added the following to my ~/.Xresources file to change to colors on the fly pressing Ctrl and 7 or 8 or 9.
! change to red background
URxvt.keysym.C-7: command:\033]11;#ff0000\007
! change to light background
URxvt.keysym.C-8: command:\033]11;#ffffff\007
! change to dark gray background
URxvt.keysym.C-9: command:\033]11;#777777\007
If you want to set foreground and background color at the same time, just concatenate the commands (some colors are defined by names):
! change to red background
URxvt.keysym.C-7: command:\033]11;#ff0000\007\033]10;yellow\007
You can test your colors with a simple echo command, like this one:
echo -e '\033]11;#ff0000\007\033]10;yellow\007' # changes to red background and yellow foreground
Attention
I used code 11 for background color and code 10 for foreground color. The definitions for Urxvt cited by Thomas Dickey indicate to use 49 and 39 instead (which I tested and also work).