Adjusting linear gradient when the color picker is modified

My website has a default linear gradient over an image, which is controlled by the following HTML and TypeScript code:

<header [style.background-image]="cv2HeaderStyle('0, 0, 0, 0.1', '0, 0, 0, 0.8')"></header>

cv2HeaderStyle(rgba1: string, rgba2: string) {
    return this.sanitizer.bypassSecurityTrustStyle(
      `linear-gradient(to bottom, rgba(${rgba1}), rgba(${rgba2})),
        url(${'../../../assets/imgs/woman-girl-silhouette-jogger-40751.jpg'})`
    );
  }

Within a form on my site, I have integrated a dropdown feature for the ngx-color-picker, enabling users to modify the RGBA values of the linear gradient.

 <input 
              (colorPickerChange)="updateFirestoreColor($event)" 
              id="filmColorPicker" 
              [(colorPicker)]="color" 
              [style.background]="color"
              [cpOutputFormat]='auto'/>

I'm using a function in my TypeScript file to convert hex codes to RGBA values, and then attempting to update the cv2HeaderStyle with these new values. However, the updated code isn't functioning as expected:

updateFirestoreColor(color) {
    var setColor1WithOpacity = this.hexToRgb(color) + ', 0.1)';
    var setColor2WithOpacity = this.hexToRgb(color) + ', 0.8)';
    this.cv2HeaderStyle(setColor1WithOpacity, setColor2WithOpacity);
  }

hexToRgb(hex){
    var c;
    if(/^#([A-Fa-f0-9]{3}){1,2}$/.test(hex)){
        c= hex.substring(1).split('');
        if(c.length== 3){
            c= [c[0], c[0], c[1], c[1], c[2], c[2]];
        }
        c= '0x'+c.join('');
        return 'rgba('+[(c>>16)&255, (c>>8)&255, c&255].join(',');
    }
    throw new Error('Bad Hex');
  }

Despite correctly obtaining the new RGBA values in the console, there are no errors displayed when running the code.

Answer №1

Make sure to include the "argument" in your .html file or use a variable instead

<header [style.background-image]="cv2HeaderStyle(color1, color2)">
//or
<header [style.background-image]="myBackground">

When updating Firestore color:

updateFirestoreColor(color) {
    this.color1 = setColor1WithOpacity = this.hexToRgb(color) + ', 0.1)';
    this.color2 = setColor2WithOpacity = this.hexToRgb(color) + ', 0.8)';
     //or 
    const setColor1WithOpacity = this.hexToRgb(color) + ', 0.1)';
    const setColor2WithOpacity = this.hexToRgb(color) + ', 0.8)';
    myBackground = this.cv2HeaderStyle(setColor1WithOpacity, setColor2WithOpacity);
  }

Remember: Avoid using "var" for temporary variables, opt for "let" if the value may change or "const" if it remains constant within the function

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

Encountering an issue originating from passport-local-mongoose, a node package while executing my app.js script coded in node js

const mongoose = require('mongoose'); const passportLocalMongoose = require('passport-local-mongoose'); const newUserSchema = new mongoose.Schema({ username: String, name: String, password: String, phoneNumber: Number } ...

"Error: Vue prop is not defined when passed to computed functions during initial call

I am encountering an issue with my Vue component. Here is the code for reference: Vue.component('result', { props: ['stuff'], data: () => ({}), template: "<img :src='tag' class='result'></img>", ...

Exploring the possibilities of wildcards in npm scripts on Windows

Currently, I am attempting to use jshint to lint all of my javascript files by utilizing an npm script command. Even though I am working on a Windows system, I am facing an issue where I cannot successfully lint more than one file regardless of the wildca ...

Is there a way to pause and await the completion of an axios post request within a different axios interceptor?

Here are my axios interceptors: instance.interceptors.request.use( (config) => { const accessToken = localStorage.getItem("access_token"); const auth = jwt_decode(accessToken); const expireTime = auth.exp * 1000; co ...

Guidance on editing list items individually using jQuery from separate input fields

Working with li presents some challenges for me. On my webpage, I have an input field that dynamically adds values to a list. However, the function to edit these values is not working properly. After editing an li element, it deletes all classes and span ...

Trouble getting proper alignment displayed with JavaScript and CSS

If anyone has a solution for printing content in a div using JavaScript and CSS, please help. The main div with the id 'preview' contains content taken from a database using PHP and MySQL. However, the data on the print page appears vertically an ...

What is the best method to loop through this object with JavaScript?

Suppose I have the following data: let testData = { 'numGroup1': [[(1, 2, 3, 4, 5), (5, 6, 7, 8, 9)]], 'numGroup2': [[(10, 11, 12, 13, 14), (15, 16, 17, 18, 19)]] }; What is the best approach to iterate through this data using Jav ...

Creating a design with CSS that features three main content columns of equal height, all of which are scrollable and have fluid heights, along with a sticky

After extensive research through various online resources, I have yet to find a solution to my specific CSS layout inquiry regarding "3 equal-height columns with a 'really' sticky footer". The desired layout includes the following components: ...

Exporting ExpressJS from a TypeScript wrapper in NodeJS

I've developed a custom ExpressJS wrapper on a private npm repository and I'm looking to export both my library and ExpressJS itself. Here's an example: index.ts export { myExpress } from './my-express'; // my custom express wrap ...

Issues arise when attempting to make a SOAP request in NodeJS, as opposed to PHP where it functions seamlessly

As I work on integrating a SOAP-API to access data, I encounter an error when trying to implement it in my ExpressJS-Service using NodeJS. The API documentation provides examples in PHP, which is not my preferred language. My PHP implementation works flawl ...

"Customizing the gaps between cluster bars on AmCharts JS for a clean and professional

Previously, I utilized the AmCharts flash version where creating clustered column/bar charts like the image below was a breeze. In that setup, the clustered bars were tightly packed with no spacing in between. However, transitioning to the JS version of Am ...

Stop Wordpress from automatically preloading html5 videos

I have a WordPress page containing numerous videos and I am looking to prevent them from preloading using HTML5. The challenge is that I am inserting the videos through the featured media options, which means I cannot directly add the preload="none" attrib ...

Explore the styling options for SVG using CSS to manipulate different properties of the

After saving my SVG image in Illustrator, the code appears like this: <?xml version="1.0" encoding="utf-8"?> <svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBo ...

Is there a way to eliminate the gap beneath each row of my Tic-Tac-Toe grid in Next.js with CSS styling?

What could be causing the space under every row in my CSS? I am currently developing a tic-tac-toe application using Next.js to enhance my skills. However, I have encountered an issue with the CSS where there appears to be a small space underneath each bo ...

Avoiding repetitive rendering of child components in ReactJS

In my React project, I have a parent component that contains 3 children components, structured like this: var Parent = React.createClass({ render: function() { return (<div> <C1/> <C2/> <C3/> ...

What is the best way to shorten text in Angular?

I am looking to display smaller text on my website. I have considered creating a custom pipe to truncate strings, but in my situation it's not applicable. Here's what I'm dealing with: <p [innerHTML]="aboutUs"></p> Due to t ...

Avoiding type errors in d3 v5 axis by using Typescript

I am new to TypeScript and I have some code that is functioning perfectly. I believe if I define a type somewhere, d3's generics will come into play? Within my code, I have an xAxis and a yAxis. Both are the same, but D3 seems to have an issue with t ...

Exploring the use of single character alternation in regex with Webstorm's Javascript Regex functionality

I've been attempting to divide the string using two different separators, like so: "some-str_to_split".split(/-|_/) It successfully splits the string based on both "-" and "_". However, Webstorm is issuing a warning: Single character alternation ...

Controlling Node.js application with Electron app: initiating and terminating

I'm currently working on the functionality to control the start and stop of another node.js application within my electron app. So far, I've managed to successfully start the node application from bot.js by running npm start to launch the electr ...

What is the best method for storing numerical data for a Next.js/React website? Should you use a CSV file, make a backend API call, or download

I'm working on a nextjs website and I want to integrate a chart. Where would be the best place to store the data for this chart? Here are some options I've considered: Save a csv file in the public folder and retrieve it from there Store a csv f ...