Add Category to Body Class

In recent versions of WordPress it’s been possible to add a body class using <body <?php body_class(); ?>>. This provides lots of useful classes for styling, but it doesn’t show the category on a single post. A couple of years ago WordPress forum member eastwoodarts posted an elegant snippet that can be added to functions.php: