{"id":1169,"date":"2018-10-01T01:40:28","date_gmt":"2018-10-01T00:40:28","guid":{"rendered":"http:\/\/www.malcolmhardie.com\/weblogs\/angus\/?p=1169"},"modified":"2025-02-01T03:04:14","modified_gmt":"2025-02-01T03:04:14","slug":"nscolor-highlightcolor-differs-in-dark-mode","status":"publish","type":"post","link":"https:\/\/www.malcolmhardie.com\/weblogs\/angus\/2018\/10\/01\/nscolor-highlightcolor-differs-in-dark-mode\/","title":{"rendered":"[NSColor highlightColor] differs in dark mode"},"content":{"rendered":"<p>I noticed a point today while working on dark mode for Mojave 10.14.<\/p>\n<p>The value of <code>-(NSColor*)highlightColor<\/code> differs depending on whether you&#8217;re in light mode or dark mode.<\/p>\n<p>This particularly affects <code>-(NSColor *)highlightWithLevel:(CGFloat)val;<\/code><\/p>\n<p>By calling:<\/p>\n<p><code> [[NSColor highlightColor] colorUsingColorSpace:[NSColorSpace genericRGBColorSpace]];<\/code><\/p>\n<p>I was able to grab the highlight colors in light mode and dark mode. I converted the values to hex and they are displayed below:<\/p>\n<p>Light Mode:<\/p>\n<div style=\"background-color: #ffffff; height: 50px; width: 50px; border: 1px solid black;\"><\/div>\n<p>(White)<\/p>\n<p>Dark Mode:<\/p>\n<div style=\"background-color: #a5a5a5; height: 50px; width: 50px; border: 1px solid black;\"><\/div>\n<p>(Gray-ish)<\/p>\n<p>&nbsp;<\/p>\n<p>Highlight color is noted as for &#8220;The virtual light source onscreen&#8221;, but it is occasionally suggested for providing slight variations on a drawing color.<\/p>\n<p>Which is great, except that because the output differs between light mode and dark mode, you can&#8217;t now use it anywhere within a drawn document unless you want things to look different between the two display appearances.<\/p>\n<p>In the end I replaced the method call with this new code:<\/p>\n<pre>NSColor* color = [NSColor redColor];\nCGFloat fraction = 0.7;\nNSColor* newColor = [aColor blendedColorWithFraction:fraction \n   ofColor:[NSColor colorWithCalibratedWhite:1 alpha:1]];<\/pre>\n<p>I also produced a roughly equivalent swift version:<\/p>\n<pre>import Cocoa\n\nlet color = NSColor.red;\nlet fraction = 0.7 as CGFloat;\nlet newColor = color.blended(withFraction: fraction, of: NSColor.init(calibratedWhite: 1, alpha: 1));\n\nprint(newColor?.description ?? \"invalid color\");\n<\/pre>\n<p>&nbsp;<\/p>\n<p>See\u00c2\u00a0<a href=\"https:\/\/developer.apple.com\/design\/human-interface-guidelines\/macos\/visual-design\/color\/\">https:\/\/developer.apple.com\/design\/human-interface-guidelines\/macos\/visual-design\/color\/<\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I noticed a point today while working on dark mode for Mojave 10.14. The value of -(NSColor*)highlightColor differs depending on whether you&#8217;re in light mode or dark mode. This particularly affects -(NSColor *)highlightWithLevel:(CGFloat)val; By calling: [[NSColor highlightColor] colorUsingColorSpace:[NSColorSpace genericRGBColorSpace]]; I was able to grab the highlight colors in light mode and dark mode. I converted [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,14],"tags":[],"class_list":["post-1169","post","type-post","status-publish","format-standard","hentry","category-macintosh","category-writing-software"],"_links":{"self":[{"href":"https:\/\/www.malcolmhardie.com\/weblogs\/angus\/wp-json\/wp\/v2\/posts\/1169","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.malcolmhardie.com\/weblogs\/angus\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.malcolmhardie.com\/weblogs\/angus\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.malcolmhardie.com\/weblogs\/angus\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.malcolmhardie.com\/weblogs\/angus\/wp-json\/wp\/v2\/comments?post=1169"}],"version-history":[{"count":1,"href":"https:\/\/www.malcolmhardie.com\/weblogs\/angus\/wp-json\/wp\/v2\/posts\/1169\/revisions"}],"predecessor-version":[{"id":1281,"href":"https:\/\/www.malcolmhardie.com\/weblogs\/angus\/wp-json\/wp\/v2\/posts\/1169\/revisions\/1281"}],"wp:attachment":[{"href":"https:\/\/www.malcolmhardie.com\/weblogs\/angus\/wp-json\/wp\/v2\/media?parent=1169"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.malcolmhardie.com\/weblogs\/angus\/wp-json\/wp\/v2\/categories?post=1169"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.malcolmhardie.com\/weblogs\/angus\/wp-json\/wp\/v2\/tags?post=1169"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}