Contrasting Randomly Generated Background colors with foreground colors

The javascript code below was used as a prototype, I wanted to see if I could find when it would be best to use a light or dark font, given an arbitrary background color.

The ultimate usage of this was for a Small Clock (github) I wanted to use on my second screen.

See the Pen ContrastIngBackgroundAndForeground by Bruce Burge (@bruceburge) on CodePen.