I don't know of any way to control the setting automatically (sometimes there are special-permission non-standard/undocumented intents for settings though), but as a maybe-close-enough you can turn on grayscale color correction (in accessibility -> color & motion, in my phone) and add a quick-settings tile to toggle it.