Our World Statistics Day conversations have been a great reminder of how much statistics can inform our lives. Do you have an example of how statistics has made a difference in your life? Share your story with the Community!
Choose Language Hide Translation Bar

Detecting macOS Dark Mode and Windows High Contrast Mode

Problem

When creating user interfaces in JMP, you may want to add some background or foreground colors to your displays. However, with JMP 15's new support for macOS Dark Mode and Windows High Contrast Mode, you need a way to detect if the user is in a dark or light appearance mode in order to have legible content. For example, you wouldn't want to set a light color as a background color or a dark foreground color in dark mode, as it would be hard to read.

Solution

To detect whether the user is in a dark or light mode, we can check the Background Color preference. By calculating the luminance of the background color, we can generally figure out if the current OS is running in a dark appearance mode.


/*	Function: getCurrentAppearance
		Returns the current appearance based on the background 
		color for reports, either 'Light' or 'Dark'.
*/
getCurrentAppearance = Function({},
	{backgroundColor, r, g, b, luminance},
	backgroundColor = Arg( Arg( Get Preference( BackgroundColor ), 1 ), 1 );
	{r, g, b} = Color To RGB( backgroundColor );
	luminance = r * 0.30 + g * 0.59 + b * 0.11;
	If( luminance < 0.5, "Dark", "Light" );
);
In practice, you can call this function at the beginning of your script (or call it each time), and use it when needed.
isLightMode = getCurrentAppearance() == "Light";
New Window("Appearance Mode",
    Text Box("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
        <<Background Color( 
            If( isLightMode, 
                RGB Color( 219, 235, 251 ), // light blue in light mode
                RGB color( 15, 40, 60 )     // dark blue in dark mode
            )
        )
    )
);

 

Article Labels

    There are no labels assigned to this post.

Article Tags
Contributors