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

Activate the class only on the current element

I need help with a specific functionality on my website. I have multiple sections with content and buttons, and when a button is clicked, I want to add an active class to the corresponding content section within the same row. <div id="1" class="row"> ...

Ensuring consistent placement and scrollability of two divs across all screen sizes

I am in need of a solution to fix the top and bottom divs in the given image. The scroll should only occur when there is overflow. <!DOCTYPE html> <html> <head> <script src="//code.jquery.com/jquery-1.9.1.min.js"></script> ...

How to use Ionic 3 to automatically scroll ion-scroll content all the way to the bottom

My ion-scroll component is experiencing some challenges <ion-scroll scrollY="true" style="height: 52vh;"> {{ text }} </ion-scroll> The content inside the ion-scroll keeps expanding, exceeding the designated height. Users can manually scroll ...

Having trouble with updating AngularJS?

I am facing an issue while updating the guitar object in my AngularJS application. The operation is performed using the updateGuitar controller with ngMock backend. After updating the guitar object, the put request is processed by the carService. However, ...

The input field cannot accommodate the lengthy value in the Mat Select option

When a user selects a value in my mat select, it doesn't display well in the selection box. The text wraps when the selection is opened, but once a choice is made, it gets cut off without proper spacing between the ellipses and the dropdown arrow. Th ...

Issue with Angular 2 NgFor Pattern Error Message Display Absence

I am attempting to incorporate inputs with a regex requirement within an ngFor loop, but I am not receiving the expected error message when entering something that does not match the required pattern. Even when I input an incorrect pattern, "Test" remains ...

Node.js: App crashed, standing by for file changes to initiate restart

While I was in the middle of a nodejs course on Udemy, I encountered an issue where the code suddenly started breaking and throwing errors. Even after attempting to replicate the instructor's code, the problem persisted. I sought guidance from this a ...

What is the best way to get process.argv to display only the arguments and exclude the node command and path?

As the title suggests, I am working on a substantial project that involves AppleScript and iMessage. After testing the script, it successfully opens Terminal and executes: node ~/Desktop/chatbot [argument]. However, at the moment, all it returns is: [ &apo ...

Tips for inserting an item into the parent window: Chrome-specific (compatible with all other browsers)

In my HTML file, I have an iFrame element like this: <iframe src="frame.html" width="2000" height="1000"></iframe> When trying to use jQuery's append function from within the iFrame to add a DIV to the parent window, it works fine in Fir ...

Guide on how to showcase the template by leveraging the roomList information with ngTemplateOutlet in Angular

TS roomList = [{ name: 'Room2' }] HTML <div class="Layout-body"> <ng-container *ngFor="let dt of roomList; index as i" [ngTemplateOutlet]="Room1" [ngTemplateOutletContext]="{ data: dt, i: i }&qu ...

What is the best way to clear an array?

Yesterday I had a query regarding JSON Check out this link for details: How to return an array from jQuery ajax success function and use it in a loop? One of the suggested answers included this script: setInterval(updateTimestamps,30000); var ids = new ...

Rendering on the server with React: Utilizing server-side rendering to display product information with passed-in :productId parameters

Having an issue with my React app that is rendered on both the client and server (Node and Express). When someone types in the URL directly, the application crashes because it cannot parse the '1' in the URL to fetch the correct product. If a l ...

What sets apart the <img> and <Image> layout attributes in NextJS?

While working on custom checkboxes, I came across a unique situation - the only way the positioning of the checkboxes aligns properly is if the classes '.init' and '.hover' are assigned to images, and the class '.done' is disp ...

What is the process for updating the background image in Ionic?

Currently facing an issue with setting an image for background in Ionic. The code provided doesn't seem to be working as expected. It appears that the style sheet is not being applied correctly. Not sure where exactly to add the style sheet or how to ...

Setting a specific time zone as the default in Flatpickr, rather than relying on the system's time zone, can be

Flatpickr relies on the Date object internally, which defaults to using the local time of the computer. I am currently using Flatpickr version 4.6.6 Is there a method to specify a specific time zone for flatpickr? ...

Extracting Text from a Span Element Using XPath in Selenium

Here is the HTML code snippet: <div class="a-row a-spacing-small a-size-small"> <div class="a-row"> <a class="a-link-normal a-declarative g-visible-js reviewStarsPopoverLink" href="#" data-action="a-popover" data-a-popover="{"closeButton":" ...

JavaScript has received an event on Server XHR

Situation: There is a scenario where the target API is external and cannot be altered. A JS client initiates sending data to the API upon clicking a button. The code snippet resembles something like this: $.ajax({ type: "POST", url: &quo ...

The journey of data starting from a PHP file, moving through JavaScript, and circling back to PHP

I've encountered an interesting challenge with my PHP file and Wordpress shortcode. It all starts when the shortcode is embedded in a webpage, triggering a php function from within the file. This function executes an SQL query to extract data, then s ...

Adding Bootstrap to container-specific styling in SCSS

I am currently in the process of upgrading to the most recent version of reactstrap & Bootstrap. Previously, I had reactstrap in my package.json file and downloaded Bootstrap SCSS in my client/src/styles/bootstrap directory. Now, my updated package.json c ...

Learn how to establish a state using an array and effectively utilize the setState() method in React

For my latest project, which is API based, I am working with arrays that contain sub-arrays. How can I define a state with an array and utilize the setState() method to work with the entire array? ...