cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
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, hue, luminance, saturation},
	backgroundColor = Arg( Arg( Get Preference( BackgroundColor ), 1 ), 1 );
	{hue, luminance, saturation} = Color To HLS( backgroundColor );
	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
            )
        )
    )
);

 

JSL Cookbook

If you’re looking for a code snippet or design pattern that performs a common task for your JSL project, the JSL Cookbook is for you.

This knowledge base contains building blocks of JSL code that you can use to reduce the amount of coding you have to do yourself.

It's also a great place to learn from the experts how to use JSL in new ways, with best practices.