We discuss the use of a Langevin equation with a colored (correlated) noise to perform constant-temperature molecular dynamics simulations. Since the equations of motion are linear in nature, it is easy to predict the response of a Hamiltonian system to such a thermostat and to tune at will the relaxation time of modes of different frequency. This allows one to optimize the time needed to thermalize the system and generate independent configurations. We show how this frequency-dependent response can be exploited to control the temperature of Car-Parrinello-like dynamics, keeping at low temperature the electronic degrees of freedom, without affecting the adiabatic separation from the vibrations of the ions.