Build a Hexadecimal Color Clock in JavaScript

A fun way to progress while learning JavaScript

Sylvain Saurel
Jan 5, 2020 · 5 min read
Image for post
Image for post

Learning a new programming language is not always a piece of cake. Studying the basics of a language is often quite daunting. It’s a necessary evil, but from time to time, in the course of your learning, I think you can give it a little fun break.

This story offers you just that little bit of fun in your learning of the JavaScript programming language. Thus, I will teach you how to create a hexadecimal color clock.

This will be an opportunity to put into practice the basics of the JavaScript language as you have already learned to make a fun application.

What Is a Hexadecimal Color Clock?

A hexadecimal color clock represents the time as a hexadecimal color and applies this color to the background. Note also that a hexadecimal color is a six digit representation of a color.

It is well-known to web developers who use this representation every day in HTML pages with CSS.

Concretely, you will retrieve the time in hour:minute:second format and then convert each of the components of the time into a component of the RGB hexadecimal color format.

In order to have a greater diversity of colors, we will put the hours, minutes, and seconds back into the value set [0;255].

Currently it is 12:06:56. Applying the calculation I have just detailed to this time, we will obtain the following hexadecimal color: #851AF2.

I will come back in detail about this calculation when I will implement it in JavaScript later in this piece.

Building the HTML Page

The first step is to create an HTML page to represent our hexadecimal color clock. The background of the HTML page will take the hexadecimal color returned by our hexadecimal color clock.

I will also display the hexadecimal value returned by the hexadecimal color clock on the page by adapting the color of the text depending on whether the hexadecimal hour is dark or light.

The content of the basic HTML page for our hexadecimal color clock is as follows:

Hexadecimal Color Time Conversion

As I explained earlier, our hexadecimal color clock will represent the time as an RGB color in hexadecimal format.

You must, therefore, write a function capable of transforming the time to RGB color in hexadecimal format.

The prerequisite for this function is to be able to transform a decimal value into a hexadecimal value. To do this, I create a toHex function that takes an integer variable as input and returns its hexadecimal value.

In order to be sure that the hexadecimal value is returned on two digits, I add an initial zero :

I will then create a hexaTime function returning the current time in the format expected by our hexadecimal color clock.

I retrieve the current date using the Date object in JavaScript. I then call the functions getSeconds, getMinutes, and getHours to get respectively the seconds, minutes, and hours of the current date.

I then put these values back into the value set [0;255] with a simple rule of three.

Then, for each decimal value obtained, I call the toHex function so that I can output the time as an RGB color in hexadecimal format.

All this gives the following code:

Changing the Color of the Text According to the Darkness

In order to be able to visualize the time well in the hexadecimal format of our clock, it would be good to be able to change its color according to whether the background color is more or less dark.

If the color is dark, we write the text in white, and if it is light, we write the text in black.

To do this, I will create a hexislight function that will return true if the input color is light and false if the input color is dark.

After retrieving the R, G, and B components of the input RGB color, I will apply the following formula to calculate the brightness of the color:

brightness = ((red * 299) + (green * 587) + (blue * 114)) / 1000;

From this brightness value, it can be deduced that the color is light if the value is higher than 155. Otherwise, it is dark.

All this gives the following code for the hexislight function:

Continuous Application of Hexadecimal Color in the Background

The last part of the construction of our hexadecimal color clock will consist of applying the obtained RGB color continuously in the background of the previously detailed HTML page.

The time value in hexadecimal format will be updated second by second, as well as the display color of this time.

So, I create a bgColor function that first retrieves the current time in hexadecimal format. I apply this color in the background of the HTML page body.

Then, I display the hexadecimal color within the hexatime div while taking care to change its color depending on whether the hexadecimal color is dark or light.

It then remains to ensure that this function is constantly called-up so that the background color is continuously updated.

This last feature is implemented via the standard JavaScript setTimeout function in which it is specified that the bgColor function must be called in 100ms.

Finally, we finish the development of our hexadecimal color clock by making a first call to the bgColor function once the DOM of the page has been successfully loaded.

All this gives the following complete code for the hexadecimal color clock we just developed:

Hexadecimal Color Clock in Action

The test of the hexadecimal color clock requires loading its HTML page in a modern browser such as Brave browser.

Once the HTML page is launched, you should get the following result:

Image for post
Image for post

As time goes by, you should then see the displayed color change:

Image for post
Image for post

The hexadecimal color clock that we have just developed is therefore perfectly functional.

To Go Further

You can get inspiration from the code written for this hexadecimal color clock to make a desktop application that displays this clock in the desktop background.

In order to reuse the JavaScript code used in this piece as much as possible, I recommend you use the Electron framework that allows you to develop multi-platform desktop applications with JavaScript, HTML, and CSS.

Finally, you can discover this tutorial in video on YouTube:

Better Programming

Advice for programmers.

Sign up for The Best of Better Programming

By Better Programming

A weekly newsletter sent every Friday with the best articles we published that week. Code tutorials, advice, career opportunities, and more! Take a look

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Sylvain Saurel

Written by

Entrepreneur / Developer / Blogger / Author. In Bitcoin We Trust: https://www.inbitcoinwetrust.net

Better Programming

Advice for programmers.

Sylvain Saurel

Written by

Entrepreneur / Developer / Blogger / Author. In Bitcoin We Trust: https://www.inbitcoinwetrust.net

Better Programming

Advice for programmers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store