Changing the hue of individual pixels on an HTML canvas to a different color while maintaining their original shading

I have created a function that enables me to draw an image on a hidden canvas temporarily in order to modify colors and change everything except solid black to a specific color while preserving the original white "shade".

However, when attempting to execute this function, the resulting color appears darker than expected.

In my function, I have set it up to replace the color with the new color minus the original color value, which has been subtracted from 255.

    for(var i = 0; i < imageData.data.length; i+=4) {
      if((imageData.data[i] != 0 &&
          imageData.data[i + 1] != 0 &&
          imageData.data[i + 2] != 0)) {

        imageData.data[i] = newColor.r - (255 - imageData.data[i]);
        imageData.data[i + 1] = newColor.g - (255 - imageData.data[i + 1]);
        imageData.data[i + 2] = newColor.b - (255 - imageData.data[i + 2]);

        if(imageData.data[i] > 255)
          imageData.data[i] = 255;

        if(imageData.data[i + 1] > 255)
          imageData.data[i + 1] = 255;

        if(imageData.data[i + 2] > 255)
          imageData.data[i + 2] = 255;
      }
    }

When using the new color #44698B (r:68, g: 105, b:139) on a slightly greyer white #999 (153, 153, 153) section, the result came out as #000325 (0, 3, 36) instead of the expected #2F4860 (47, 72, 96).

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

The paintbrush is whimsically altering its style on the canvas

My checkerboard has a unique feature where hovering over a tile will highlight it in yellow. Surprisingly, this action also turns the game border and the last white checker's border yellow as well. What's puzzling is that I never specified for t ...

Adding a new row to an HTML table using JavaScript

In the code snippet below, I am facing an issue with adding a row to an HTML table and inserting it into a database. I have tried using JavaScript to add the row in order to avoid postback, but so far, I have been unsuccessful. Can someone assist me in t ...

What is the best approach for incorporating remote validation in a KendoUI grid with AngularJS and Web API integration?

Currently, I am facing a challenge of inserting a product into a SQL table. My approach involves using Kendo UI grid and AngularJS technologies. I am seeking guidance on how to properly validate whether the product already exists in the database before exe ...

Retrieving the clicked element in React JS

https://jsbin.com/diyenakife/edit?html,js,output JSX let MY = React.createClass({ sendMsg : function(e){ alert($(e.target).attr('data-id')); //sendMsgButton = ?? }, render: function() { return ( <button is class = ...

Is there a way to seamlessly transition the visibility of the Bootstrap 5.3 Spinner as it loads and unloads?

After delving into some research regarding Bootstrap-compatible spinners, I stumbled upon a piece of code. Take a look: function getData() { var spinner = document.getElementById("spinner"); spinner.style.display ...

How can I utilize match props in React JS with the Context API?

Currently working on a basic application that utilizes context API, fetch, and react hooks for all components except the context API component due to ongoing learning of hooks. The challenge lies in incorporating the match prop within the context API prov ...

An error occurred: [object Object] does not contain the function 'bootstrapDatepicker'

After spending countless hours searching for a solution, I continue to encounter the dreaded 'Uncaught TypeError' without any successful resolutions. The issue seems to stem from a clash between tribe-events-ajax-calendar.js and foundation.min.j ...

Next.js encountered an error while trying to locate the flowbite.min.js file for Tailwindcss and Flowbite, resulting in a

I'm having an issue with integrating the flowbite package with TailwindCSS in my Next.js application. Despite configuring everything correctly, I am encountering an error when adding the flowbite.min.js script: GET http://localhost:3000/node_modules/f ...

What could be causing the Toast message to not show up in react-native-root-toast?

Incorporated react-native-root-toast into my expo project running on expo 51. Please see the code snippet below for reference: const toastColors = { 'error': { color: '#DA5C53', iconName: <WarningIcon size="5 ...

Dispatch in React Redux is not intercepted

I've been grappling with this issue for hours and still can't seem to find a solution. When I click the button, the function "getLocation" is being triggered (Confirmed it) However, the dispatch is not getting passed to the reducer. After r ...

AngularJS views malfunctioning following oauth redirect

I am in the process of creating a web application using AngularJS and Firebase. Recently, I added a second page along with an ng-view to my index file. In order to facilitate login via Facebook or Google, I am utilizing the $firebaseAuth service. However, ...

Tips on navigating the scroller vertically as the user interacts with a selected div by scrolling up and down

On my webpage, I have various div elements displayed. Each div has the options to move it up or down using the buttons labeled "UP" and "Down". When a user selects a div, they can then use these buttons to adjust its position. I am looking for a way to au ...

Tips for displaying filtered items on the MongoDB terminal

I have objects categorized by cost and date of addition, each user having such categories. My goal is to display all categories for the month "08" in the console. How can I achieve this? Currently, my code retrieves the entire object with the user informat ...

Encountering an Issue Retrieving User Orders - Receiving 401 (Unauthorized) Status Code

I am currently working on a React project focused on displaying user orders. I have successfully integrated JSON Web Tokens (JWT) for user authentication. However, I keep encountering a persistent 401 (Unauthorized) error when trying to retrieve the user&a ...

React error: The module "react-router-dom" does not have a member named "useNavigate" available for export

I'm attempting to include useNavigate for use as outlined in the top answer here: react button onClick redirect page import { useNavigate } from "react-router-dom"; However, I am encountering this error: export 'useNavigate' (impo ...

Discover and capture zip codes using javascript

Looking to extract zip codes from strings like "HONOLULU HI 96814-2317 USA" and "HONOLULU HI 96814 USA" using JavaScript. Any suggestions on how to achieve this? ...

I have to make sure not to input any letters on my digipas device

There is a slight issue I am facing. Whenever I input a new transfer of 269 euros with the bank account number BE072750044-35066, a confirmation code is required. The code to be entered is 350269. https://i.stack.imgur.com/YVkPc.png The digits 350 corres ...

The module '../xcode' could not be located. This issue is occurring within React Native and Expo CLI, where the required stack cannot

Trying my hand at creating my first project using React Native in iOS with expo.io, I encountered an error when running the command "expo start": https://ibb.co/f2xsmpN https://i.sstatic.net/Uyxkk.png Despite attempts to reinstall and update Xcode, usin ...

Comparison between websocket implementation in PHP and Node.js

I'm curious to learn about the distinctions between using "websocket php" and node.js for a chat feature. I currently have a chat implemented with websocket php, but I'm wondering if migrating it to node.js would be a better option. Any insights ...

Creating circular patterns with looping on a canvas

My goal is to draw circles in a loop, but when I execute my code, I am encountering an unexpected result: The intention is to simply draw 3 circles in random positions. Here is my current code: for (var i = 0; i < iloscU; i++) { ctx.strokeStyle = ...