What gives a porter or a stout its dark colour? I imagine it's probably just that the barley has been roasted darker, and that comes out.
If you follow the information trail through Wikipedia's page about Stout beer, you find that what was known as Stout became a strong, dark beer in the 1800s:


  In the 19th century, the beer gained its customary black colour through the use of black patent malt, and became stronger in flavour.


You can see from the article about mash ingredients that black patent malt is "barley malt that has been kilned to the point of carbonizing, around 200 °C." It is apparently called "patent" malt because the inventor was awarded a patent for it. 

Sometimes, this malt is used to darken the colour of other beers.