Gnome Dynamic Theme

Coding All Hours? Conquer Eye Strain with Gnome Dynamic Theme

Spread the love

Having a dynamic theme for your code editor sounds great. However, it does not fully solve the eye transition problem.

Working with the computer has more than just writing code. You also browse the web, write documents, or run some commands in the terminal.

All those activities require looking at your screen.

If your code editor uses a dark color scheme, but the other tools shine as bright as the sun, your eyes will want to punch you in the face for making them suffer.

In this article, we will make your entire computer environment dynamic.

Before tinkering with the system

This article explains the way to make the Gnome environment change theme automatically based on the time of the day.

If you are using Windows, this article is not for you.

If you are using Linux with another environment other than Gnome, this article acts as a guide to show what you need to do to have the same result.

Let’s begin the journey.

Get started with the Gnome color scheme

Using the Gnome desktop environment, you can configure your favorite theme on Settings > Appearance.

Selecting light or dark mode will change all system tools like Files, Settings, and Firefox to prefer light or dark mode.

Do you know you can set the mode from the command line too?

You can change the color scheme of the theme by setting the value to the `color-scheme` key.

To see the list of all color schemes your theme has, run the command below.

$ gsettings describe org.gnome.desktop.interface color-scheme

The preferred color scheme for the user interface. Valid values are “default”, “prefer-dark”, “prefer-light”.

You can change the color scheme to dark mode by using the `set` command.

$ gsettings set org.gnome.desktop.interface color-scheme 'prefer-dark'

You can do the same with ’prefer-light’ and ’default’.

Please play around to see how things change.

Why change the color scheme using the command line?

Why bother changing the color scheme using the command line while we do have UI?

Changing the color scheme using the UI is easy. However, if we want to automate it, we must run the command.

We can put the command above to a bash script file.

Using the current time value to determine the color scheme

To reduce eye strain while transitioning, we want the color scheme to match the outside environment as much as possible.

Light mode for the day. Dark mode for the night.

We can have a simple if/else logic like this.

if (is_daytime) {
	use_light_mode()
} else {
	use_dark_mode()
}

With the command line above, we can use the bash script to change the color scheme according to the time of the day.

# dynamic_theme.sh

function change_color_scheme() {
	local color_scheme=$1
	gsettings set org.gnome.desktop.interface color-scheme $color_scheme
}

# Get the current hour
hour=$(date +%H)

if [ $hour -ge 6 ] && [ $hour -lt 18 ]; then
   	# Morning
   	change_color_scheme 'default'
else
   	# Evening
   	change_color_scheme 'prefer-dark'
fi

In the file dynamic_theme.sh above, I want to have light mode from 6:00 to 18:00. Otherwise, dark mode is set.

Change the color scheme automatically

Having to run the file `dynamic_theme.sh` every time you want to change the theme is bad. It is the same as manually changing the theme in the UI.

How to change the theme automatically?

The answer is cron job.

The cron utility is used for running scripts and commands at regular intervals, and at specific times and dates.

We want to run the script dynamic_theme.sh every hour to update the theme.

First, you need to install crontab.

sudo dnf install crontab

Then, specify the job you want to run.

sudo dnf install crontab

I want to run dynamic_theme.sh every time the computer turns on and every hour, so the theme is applied correctly from the start of the day and dynamically changed during the day.

@reboot /home/ngoc/dynamic_theme.sh
0 * * * * /home/ngoc/dynamic_theme.sh

You can use crontab.guru to validate your expressions before adding to crontab.

Working in a dynamic environment

Your computer has become an all-day eye-comfort environment.

Although, looking at the screen feels better now, don’t just sit there all day long. Take short breaks for eye strain relief. Enjoy the change of the environment, both on-screen and outside.

Hit the share button if you find this article helpful.


Spread the love

Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *