Tips on how to prevent certain classes from being impacted by a hue-rotate filter applied to all elements on a webpage

I am currently in the process of adding a feature that allows users to choose between a dark or light theme, as well as select a specific theme color for the app. The implementation involves using CSS filters such as invert(1) for the dark theme and hue-rotate for the theme color. I have successfully managed to exclude certain classes from the invert() filter effect, like images and svgs, by setting invert(0) for those classes. However, I encountered an issue with the theme color selector, as the degree value for the hue-rotate() filter changes based on user selection. While it is technically possible to calculate and apply the opposite hue dynamically to those classes, my limited knowledge in JavaScript has hindered my progress.

In attempting to address this issue, I developed the following code:

HTML

<!DOCTYPE html>
<html lang="en">
<!-- rest of HTML code here -->

JS

// Theme Chooser and Theme Color Chooser scripts
<!-- JavaScript code here -->

LIGHT.CSS

/* Light theme CSS styling */
<!-- Styling rules for light theme -->

DARK.CSS

Dark css is the same as light but adding this at the bottom

/* Dark theme CSS with additional styling */
<!-- Additional styling for dark theme -->

When toggling between light and dark themes, the svg icon and image are not affected. However, changing the theme color impacts both elements. Therefore, the challenge lies in dynamically applying the calculated opposite hue values to these classes through JavaScript.

I attempted some calculations manually using the dev tools, determining suitable opposite hue values for different degrees within the selector range. While this approach worked momentarily, finding a dynamic solution eludes me due to my limited JavaScript proficiency.

You may refer to this code snippet found here for transforming black into a specified color using CSS filters.

HUE-ROTATE CALCULATION

function hueRotate(angle = 0) {
// Function for calculating hue rotation
}

If someone with more expertise in JavaScript can leverage this function to address the issue, it would be greatly appreciated.

Answer №1

It seems like you're looking to switch from a dark theme to a light theme and colorize UI elements when users select a color. I've revamped the design for a more polished appearance, building upon a previous solution and enhancing it.

const value = document.getElementById('value');
const body = document.getElementById('usrhue');
const switcher = document.getElementById('switcher');
const hueRotate = document.getElementById('hue-rotate');

const LIGHT_THEME = 'light';
const DARK_THEME = 'dark';

value.textContent = `Color: Tomato`;
body.classList.add(DARK_THEME);
body.classList.add('tomato');

// Rest of JavaScript code omitted for brevity

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: serif;
}

// Custom CSS styling continues...

<body id="usrhue">
  // HTML structure example
</body>

With localStorage

// Enhanced functionality with localStorage implementation
// Complete JavaScript code with localStorage integration

Answer №2

Learn how to utilize the :not() selector - MDN mentions that the following syntax is considered experimental

body:not(.webp, .png, .jpg) { filter: invert(0) contrast(1) saturate(1);}

Alternatively, as stated in this resource, you can also chain multiple :not() selectors

body:not(.webp):not(.png):not(.jpg) { filter: invert(0) contrast(1) saturate(1);}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Updating an array of objects within an array using another array of objects

There are two JavaScript Arrays: let x = [ { id: 'Abc', children: [ { id: 12, name: 'john' }, { id: 13, name: 'dow' ...

Which is the best framework to transform a NextJS website into a desktop application: Electron, React-Native, or React-

After recently mastering ReactJS, I am currently utilizing it to create an application with NextJS. My goal is to make this application available as a desktop version as well. I have been introduced to Electron, React-Native, and React-Desktop, but I am u ...

Having trouble debugging a website remotely? Dealing with a JSON problem specifically in IE8

Currently, the website is functioning flawlessly on various machines except for those used by the client in Africa. Unfortunately, I have no way of accessing his specific machine due to geographical constraints. The client has limited IT knowledge, and we ...

Safari does not display disabled input fields correctly

I have designed a simple angular-material form with various inputs that are organized using angular flex-layout. The form displays correctly in all browsers except for Safari on iOS devices. The problem in Safari arises when loading a form that contains d ...

Triggering the 'Next' function with a key press event

Currently, I am utilizing the following code to manage the keydown event for the tab and enter keys within an input field. $(document).on('keydown', ".className" , function(e) { var keyCode = e.keyCode || e.which; alert(keyCode) / ...

Error: Attempting to assign a value to the non-existent property 'user' <br>    at [file path] on sqlite3

I encounter an unspecified error when I try to establish a session for the user after validating their credentials during login. I'm utilizing express-session to create the session, but it's not directly imported into the file as instructed by my ...

Tips for validating a form with the assistance of jQuery

While there are multiple answers out there for this particular question, I am specifically interested in validating an entire form with dynamic input fields sourced from a database similar to Google Forms. HTML <div class="rs_card"><spa ...

What is the best way to automatically scroll to the bottom of a modal after making a request and

I've been faced with a challenge regarding an online chat widget. My goal is to automatically scroll the widget down to display the latest message when a specific chat is opened. Despite using an async function, I encountered difficulties in achieving ...

Slow loading time when switching between items in the drop-down menu from the Main Menu

Visiting my website at europebathroom.com, you will notice a horizontal main menu with dropdown functionality. When hovering over a menu item, the corresponding dropdown appears seamlessly. Yet, sometimes quickly touching another menu item unintentionally ...

What steps can I take to optimize my code and eliminate any redundancies?

In a current project, I need to make calls to three different endpoints. The functions handling these requests are identical except for the URLs being called. Below is an example of the code: const handleSubmitRequest = (e, next, endpoint, requestData) =&g ...

showcasing a map on an HTML webpage

Seeking advice on integrating a map feature in HTML to mark store locations. Working on an app for a business community and need help with this specific functionality. Your assistance would be greatly appreciated! ...

Create an interactive JSON tree structure

I am looking to create a JSON tree from an array. My array is structured like this: var arraySource = []; arraySource.push({key : "fr", value: "france"}); arraySource.push({key : "es", value: "spain"}); //... console.debug(arraySource); The desired JSON ...

How come I'm facing difficulties when trying to send a post request to my WebApi using AngularJS's POST method?

I've uploaded "mytest.html" to the IIS server, and this project consists of WebApi and AngularJS. However, I am unable to make a correct request to my WebApi. I'm not sure why? 【HTML Code Snippets】 <!DOCTYPE html> <html> ...

The path specified as "react-native/scripts/libraries" does not exist in the file

I've been troubleshooting an error on Github - https://github.com/callstack/react-native-fbads/issues/286. After cloning the repository and running it, I discovered that the error persisted. I am currently updating packages to investigate why this rec ...

Variations in functionality within the Google Chrome browser

This is the content of my HTML document. <BODY> <button id="b1" onclick="load()">Play</button> <canvas id="c1" onclick="f_clicked(this.id,1)"> Your browser does not support the HTML5 canvas tag. </canvas> ...

Using regular expressions to modify parameter values in a command-line argument between nodes and npm scripts

While experimenting with node.js, I encountered a perplexing behavior related to command line arguments: I have a program that utilizes a regex pattern to identify test files. This regex is passed as a command line argument: node index.js --require src/** ...

What is the process for attaching an on-click event listener by utilizing an argument that is passed to a function in JavaScript?

These are the functions I have created: function type(message) { document.getElementById("body").innerHTML="<div id=\"textbox\" class=\"textbox\"></div><div id=\"oc\&q ...

Connect the mileage tracker to a Datalist or grid view component

I recently downloaded the Odometer Sample from , however, I am facing an issue where only the first element in the Datalist is getting the Odometer display, while others are not displaying it. Here is the snippet of code: <script type="text/javascript" ...

Unable to use npm module "csv-db" as it is currently experiencing functionality issues

Looking to implement a "lightweight offline database" that stores data in .csv files. The module I am using can be found in the documentation: https://www.npmjs.com/package/csv-db Unfortunately, I've been encountering issues with this module and have ...

Next.js 13's App Router experiences a 404 error when refreshing due to the parallel route modal configuration

I am attempting to create a modal using parallel routes in Next.js version 13.4. Everything functions properly when I am on the homepage (/) and open the modal by navigating to /login. However, if I refresh the /login page, instead of displaying the home ...