Check whether OS X is in Dark/Light theme

Recently, I was forced to check whether application is in Dark/Light theme (status icon must be either white or black).

I have found two solutions. First one

boolean isDark = [NSAppearance currentAppearance] name] 
containsString: NSAppearanceNameVibrantDark]

and second one

boolean isDark = [[
   [NSUserDefaults standardUserDefaults] 
     stringForKey: "AppleInterfaceStyle"] isEqual: "Dark"]

To my surprise, both can give different results.

First one, seems to fail when you open new window from the status bar icon. In that case, after closing the window you get the value @”NSAppearanceNameVibrantLight”, which is wrong. It seems that for some reason window thinks it is executed in Light mode instead of Dark.