Day Night Auto Display Modes in VB.NET

Introduction

This article describes an easy approach to implementing day and night modes of display operation; the example described addresses an approach to swapping between a custom color scheme and the default color scheme applied to the operating system and for that reason it could be applied to a number of different designs. The concept of creating alternative night modes of operations for displays is not new; it has been around in aircraft and automobiles for many years. Night modes of display operation offer a way in which displays may be used in darkness and when interference with the user's night vision is an issue.

Maintaining night vision requires the avoidance of light; any source of light can bleach out vision purple and even a dark accommodated display can cause this reaction quite rapidly. Once destroyed, it takes about thirty minutes for the human eye to become dark adapted again. While it is not possible to make a backlit display such as the LCD displays typically found on laptops and tablets entirely safe for night use, it is possible to reduce the effect of the emission by darkening the display to the maximum extent practical.

Altering the color scheme is beneficial but to make the display safer for night use, it may also be advisable to reduce the brightness of the illumination source. Military displays intended for use in environments where night vision imaging systems (NVIS) are to be used with the equipment will typically offer a mode of operation where the brightness is reduced to eliminate interference with the use of the NVIS device; this is also typical in automobiles where turning on the headlights also reduces the brightness of the illuminated displays in the instrument panel.

The demonstration project only alters the color scheme. When night mode is selected, the field areas of the controls are converted to black and most of the foreground color is converted to green. Without regard to contrast, the human eye is tuned to see the color green better than other colors and specifically green in about the 450 nanometers of wavelength range. It is no accident that early display monitors used phosphors that emit light in this range. As you shift from green to blue-green (e.g., about 480 nanometers of wavelength) the color becomes more saturated and the contrast ratio declines which makes characters rendered in this color less legible on a black background. It is a common misconception that red makes a good color for rendering text on a black background but red is a highly saturated color and it too is difficult to read when displayed on a black background due to the contrast ratio. Naturally white on black or black on white offers the highest contrast ratio but white is a worst case color for this sort of mode of operation and should be used sparingly.

DayNight1.gif

Figure 1. Day Use of Sample Application

DayNight2.gif

Figure 2. Night Use of Sample Application

Getting Started

In order to get started, unzip the included project and open the solution in the Visual Studio 2005 environment. In the solution explorer, you should note the following:

DayNight3.gif

Figure 3. Solution Explorer

As you can see, there is only a single form contained in this Windows application project (Form1.vb). The only additional reference added to the project was System.Drawing.Color; all else is per the default configuration for a form class.
There is not much to the design of the form; it merely contains a set of controls used to demonstrate the transition from day to night modes of operation. These controls do not do anything (unless you are interested in learning the phonetic alphabet):

DayNight4.gif

Figure 4. The Main Form Designer

The Code: Main Form (frmMain.vb)

The main form class includes one import which is necessary to support the sample application:

Imports System.Drawing.Color

The color class import is used to allow the application to manipulate the control colors.

The first block of code in the application is used to terminate the demonstration application whenever the user clicks the form's "Exit" button:

Public Class Form1

 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As

    System.EventArgs) Handles Button1.Click

        Application.Exit()

    End Sub

Following the exit button click event handler, the next block of code is used to handle the click event of the Day mode menu option; there are a total of three modes of operation (day, night, and auto):

Private Sub tspDay_Click(ByVal sender As System.Object, ByVal e As

System.EventArgs) Handles tspDay.Click

 

    Me.tspAuto.Checked = False

    Me.tspNight.Checked = False

    Timer1.Enabled = False

 

    Dim obj As Object

 

    For Each obj In Me.Controls

        obj.ForeColor = Control.DefaultForeColor

        obj.BackColor = Control.DefaultBackColor

 

        If TypeOf obj Is TextBox Then

            obj.BackColor = Color.White

        End If

 

        If TypeOf obj Is ComboBox Then

            obj.BackColor = Color.White

        End If

 

        If TypeOf obj Is ListBox Then

            obj.BackColor = Color.White

        End If

    Next

 

    Me.BackColor = Control.DefaultBackColor

    Me.ForeColor = Control.DefaultForeColor

 

End Sub

The code is simple enough, the first part of the block is used to update the check marks in the menu and to disable a timer control used to poll for the time whenever the Auto mode of operation is selected. Next, the control collection is evaluated and the control has its foreground and background color properties set to the default control foreground and background colors. Since the text box, combo box, and list box controls are normally white, the current control is evaluated to see if it falls into one of these three categories and if it does, the background color is set to white. Lastly, the form background and foreground colors are also set to the default options. If you employ other controls on your form with similar requirements, you'd want to perform similar checks on those controls as well.

The next block of code is used to handle the night menu option click event:

Private Sub tspNight_Click(ByVal sender As System.Object, ByVal e As

System.EventArgs) Handles tspNight.Click

 

    Me.tspAuto.Checked = False

    Me.tspDay.Checked = False

    Timer1.Enabled = False

 

    Dim obj As Object

 

    For Each obj In Me.Controls

        obj.ForeColor = Color.Lime

        obj.BackColor = Color.Black

    Next

 

    Me.BackColor = Color.Black

    Me.ForeColor = Color.Lime

 

End Sub

This event handler works pretty much the same as the day option does, however, all of the controls are set to black backgrounds with lime green foreground colors.

The last bit of code in the demo is used to implement the Auto mode of operation. Since I don't have photo sensors installed on my machine, I have to use some other mechanism to determine whether or not the display should be set to day or night. I opted to use a timer control for this purpose; when enabled the timer evaluates the current time to determine if it should display the form in the night or day mode of operation; the menu click event handler merely updates the menu and enables the timer; the timer (shown after this first block) actually does the work of checking the time on interval and updating the display according to the time of day. In the example shown, the current hour is evaluated to determine whether or not the time is between 5 PM and 5 AM:

Private Sub tspAuto_Click(ByVal sender As System.Object, ByVal e As

System.EventArgs) Handles tspAuto.Click

 

    Me.tspNight.Checked = False

    Me.tspDay.Checked = False

 

    Timer1.Enabled = True

 

End Sub

 

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As

System.EventArgs) Handles Timer1.Tick

 

    If Now.Hour > 17 Or Now.Hour < 5 Then

 

        'night rules

        Dim obj As Object

 

        For Each obj In Me.Controls

            obj.ForeColor = Color.Lime

            obj.BackColor = Color.Black

        Next

 

        Me.BackColor = Color.Black

        Me.ForeColor = Color.Lime

 

    Else

        'day rules

        Dim obj As Object

 

        For Each obj In Me.Controls

            obj.ForeColor = Control.DefaultForeColor

            obj.BackColor = Control.DefaultBackColor

 

            If TypeOf obj Is TextBox Then

                obj.BackColor = Color.White

            End If

 

            If TypeOf obj Is ComboBox Then

                obj.BackColor = Color.White

            End If

 

            If TypeOf obj Is ListBox Then

                obj.BackColor = Color.White

            End If

        Next

 

        Me.BackColor = Control.DefaultBackColor

        Me.ForeColor = Control.DefaultForeColor

 

    End If

 

End Sub

With this mechanization, the display is set to day mode between 5 AM and 5PM or set to night mode when the time is between 5 PM and 5 AM.

Summary

This article describes an approach to implementing an alternative color scheme that can quickly be converted back to the current operating system color settings. The approach demonstrated was described in terms of providing a night mode of operation for displays used by mobile users in conditions of darkness. The approach will not eliminate the potential for the loss of dark adaptation but will reduce the impact of using displays where this is an issue.


Similar Articles