Converting hex color codes to hue rotation with a SCSS mixin tutorial

To achieve the color transformation from red to green and blue, I utilized the grayscale filter and determined the appropriate hue-rotate degree by referring to this resource.

Now, I am looking to create a mixin in scss where I can input a hex color code and calculate the hue-rotation degree before returning it. How can I implement this mixin effectively?

.colorRed,.colorBlue, .colorGreen {filter:grayscale(0);}
.colorRed img {filter:hue-rotate(0deg);}
.colorBlue img {filter:hue-rotate(215deg);}
.colorGreen img {filter:hue-rotate(100deg);}
<span class="original"><img src="https://i.imgur.com/ZbOHifU.png" width="100px";></span>
<span class="colorGreen"><img src="https://i.imgur.com/ZbOHifU.png" width="100px";></span>
<span class="colorBlue"><img src="https://i.imgur.com/ZbOHifU.png" width="100px";></span>
<span class="colorRed"><img src="https://i.imgur.com/ZbOHifU.png" width="100px";></span>

p.s

I prefer not to use SVG solutions as indicated here

I also do not want to rely on JavaScript solutions for this task.

Answer №1

Using scss mixins to achieve this task can be challenging, but with the help of Javascript, it becomes easier to filter based on hex color.

To see how filter values can be generated from a hex color, check out this example.

'use strict';

class Color {
  constructor(r, g, b) {
    // Constructor code
  }
  
  toString() {
    // Code for returning RGB string
  }

  // Other color manipulation methods

  clamp(value) {
    // Logic for clamping value within valid range
  }
}

class Solver {
  constructor(target, baseColor) {
    // Solver constructor logic
  }

  solve() {
    // Solve method implementation
  }

  // Other solver methods

  css(filters) {
    // CSS method for applying filters
  }
}

function hexToRgb(hex) {
  // Conversion function from hex to RGB
}

$(document).ready(() => {
  $('button.execute').click(() => {
    // Click event listener function
  });
});
.pixel {
  // CSS for pixel class styling
}
<table>
  <tr valign="top">
    <td width="50%">
      <fieldset>
        <p>
          <label>Target color</label> <input class="target" type="text" placeholder="target hex" value="#00a4d6"/>
        </p>
        <button class="execute">Compute Filters</button>
      </fieldset>
      <p>Real pixel, color applied through CSS <code>background-color</code>:</p>
      <div class="pixel realPixel"></div>

      <p>Filtered pixel, color applied through CSS <code>filter</code>:</p>
      <div class="pixel filterPixel"></div>

      <p class="filterDetail"></p>
      <p class="lossDetail"></p>
    </td>
  </tr>
</table>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

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

Tips on modifying responsive design with jQuery

I have implemented a responsive design approach with the following CSS code: @media all and (max-width:799px){ .bigFont{ font-size: 28px; } } @media all and (min-width:800px){ .bigFont{ font-size: 48px; } } If I want to modify the font size using ...

Font Awesome icons are preventing the navigation bar from collapsing

Just getting started with Bootstrap and facing an issue. I want to include a collapsed user icon that expands when clicked. However, when I add the navbar-toggle class, the icon disappears completely. Without it, the icon appears but doesn't collapse ...

Is it time for a Fluffy FOOTERITIS?

Whenever I add or remove controls within the content div/fieldset, the footer overlaps into the content area. I'm not sure how to fix this issue. In addition, I suspect that my method for extending the left side menu bar down is outdated. Any advic ...

Designing custom gridviews using CSS in an ASP.NET application with Bootstrap

Hey there, I'm currently working on a post system using GRIDVIEW in ASP.NET/BOOTSTRAP. Since GRIDVIEW is basically a table, I am using templatefild and itemtemplate with table td and tr to make it resemble a BLOG! Everything is going well so far, I ha ...

Can passing parameters between nested map functions cause any issues?

While attempting to navigate to a page in reactjs and pass parameters using the useNavigate hook, I encounter an unexpected token error as soon as I include the navigation within the anchor tag. <a onClick={() ={ ...

Every time I attempt to input text into the text area, the page automatically refreshes, preventing me from completing any entries

Currently, I am working on developing a webpage that can dynamically update text inputted by users in a database. However, I have encountered an issue where the page refreshes automatically whenever I click on the textarea. If I type quickly enough, some o ...

Navigate to a list item once Angular has finished rendering the element

I need to make sure the chat box automatically scrolls to the last message displayed. Here is how I am currently attempting this: akiRepair.controller("chatCtrl", ['$scope', function($scope){ ... var size = $scope.messages.length; var t ...

Extracting unique text values from nested div elements within list items using Selenium with Python

Within the HTML code provided, I am looking to extract the text of three variables (descr_confezione, aic_farmaco, and stato_confezione) for each of the four list items: <ul id="ul_lista_confezioni"> <li style="display: list-item;"> &l ...

Assigning websockets to a global variable may cause them to malfunction

I'm utilizing websockets in conjunction with JavaScript and HTML5. Here is the code snippet I am currently working with: <input type="text" onFocus="so = new Websocket('ws://localhost:1234');" onBlur="so.close();" onKeyUp="keyup();"> ...

Unable to align span vertically using font-style "Luckiest Guy" in CSS

I have encountered an issue with vertically centering a span using the font-style "Luckiest Guy". https://i.sstatic.net/Lz8o3.png I attempted to use display: flex;align-items: center; on the span, but it did not work. App.vue <template> <div ...

How can indentation be disabled in asp.net controls?

When utilizing the built-in controls in asp.net, I notice an abundance of tab characters added at the beginning of each line in the resulting html output. Is there a way to disable this? It would be beneficial to eliminate this unnecessary addition. For i ...

React - A guide on accessing the scroll position of a div using JavaScript

Within my side navigation bar, there is a title and other information. The title is fixed in place (sticky) and the sidebar has a height of 100vh. I am looking to add a box-shadow effect to the title div (sticky-title) only when the user scrolls down past ...

Is it possible for the logo in the div and the height of the div to vanish after 10 seconds?

Is it possible to conceal a div along with its logo? I currently have a jpeg logo displayed at the top of my website, but I would like it to vanish after 10 seconds and have the text below it shift upward to fill the space. I've managed to hide the ...

Can we bring flexbox inserts, removes, and item positioning to life through animation?

This question was first raised in 2012, but the responses provided didn't address my specific situation (smooth transition for rearranging wrapped content). "After removing an item from a flexbox, the remaining items snap into their new positions imm ...

Displaying both English and Gujarati text in a single document | Font compatibility glitch

In my application, I have text displayed in both English and Gujarati languages on the same page. For example: Name/નામ The issue arises when I apply a font like Verdana to the body tag through CSS - it affects the appearance of the Gujarati text a ...

Round bottom border button

Is it achievable to design a button that resembles this using only CSS and no extra images? Give me your thoughts on this idea. ...

Alter the HTML tag once the text is encompassed within it

Can you configure your HTML code to display (B) when the text in (A) is wrapped? If it's not wrapped, then just display (A) (A) <div> <span>abcdefgabcdefgabcdefg</span> <span>abcdefgabcdefgabcdefg</span> ...

Prevent unauthorized access to HTML pages with a secure login system

I created a basic login system using JavaScript that requires users to enter the correct username and password. Once verified, they are redirected to log1.html. From there, they can access various subpages such as log1-sub.html. However, I am struggling to ...

Incorporating an external JSX file into an HTML page in a React project

I have a React code in my js/script.js file. My HTML page's head tag is structured like this: <head> <script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="06746367657246373328302834" ...

Outline along a single edge

Is there a way to add an inset border to an HTML element on just one side, without using an image as a background? I've been stretching and positioning images for this purpose in the past, but I recently discovered the outline CSS property. However, i ...