In two, three and even four spatial dimensions, the transverse responses experienced by a charged particle on a lattice in a uniform magnetic field are fully controlled by topological invariants called Chern numbers, which characterize the energy bands of the underlying Hofstadter Hamiltonian. These remarkable features, solely arising from the magnetic translational symmetry, are captured by Diophantine equations which relate the fraction of occupied states, the magnetic flux and the Chern numbers of the system bands. Here we investigate the close analogy between the topological properties of Hofstadter Hamiltonians and the diffraction figures resulting from optical gratings. In particular, we show that there is a one-to-one relation between the above mentioned Diophantine equation and the Bragg condition determining the far-field positions of the optical diffraction peaks. As an interesting consequence of this mapping, we discuss how the robustness of diffraction figures to structural disorder in the grating is a direct analogy of the robustness of transverse conductance in the Quantum Hall effect.