Theory of contact electrification: Optical transitions in two-level systems