Today I have learned Random color change by using a function in javascript

 Random color change by using a function in javascript:

Introduction

When developing modern websites and web apps with JavaScript, there could be instances where you need to generate random colors, such as:

  • You want to reduce boredom and increase the freshness of your websites
  • To test or debug the appearance and functionality of web elements under different color schemes or contrasts.

This article will show you how to create random RGB colors, random hex (hexadecimal) colors, random HSL colors, and random HTML color names just by using vanilla JavaScript. No third-party libraries are necessary.

Generating random RGB color codes

An RGB color code is three numbers between 0 and 255 that represent the amount of red, green, and blue in a color: rgb(redValue, greenValue, blueValue). They are enclosed in parentheses and separated by commas.

To generate a random RGB color in JavaScript, you need to create three random integers between 0 and 255 using the Math.random() function. And then, you can concatenate them into a string with commas and parentheses.

Generating random hex color codes

A hex color code is a six-digit code that starts with a # and represents the amount of red, green, and blue in a color. Each digit can be from 0 to F (or 15 in decimal).

The solution to creating a random hex color code is to use the Math.random() function to generate random numbers between 0 and 15 and then convert them to hexadecimal using the toString(16) method. You can also use a loop or an array to concatenate the characters into a string.

        color:"red"
 changeButtonColor = () => {
    const newColor = this.getRandomColor();
    this.setState({ color: newColor });
  };
  getRandomColor = () => {
    const letters = '0123456789ABCDEF';
    let color1 = '#';
    for (let i = 0; i < 6; i++) {
      color1 += letters[Math.floor(Math.random() * 16)];
    }
    return color1;
  };
 <div style={{ textAlign: 'center', marginTop: '20px' }}>
        <button
          onClick={this.changeButtonColor}
          style={{ backgroundColor: this.state.color, padding: '10px', cursor: 'pointer' }}
        >
          Click me to change color
        </button>
      </div>
Output:
 num1:0,
        num2:0,
        result:0,
  Num1Change = (e) => {
    this.setState({ num1: parseFloat(e.target.value, 10) || 0 });
  };
  Num2Change = (e) => {
    this.setState({ num2: parseFloat(e.target.value, 10) || 0 });
  };
  addNumbers = () => {
    const { num1, num2 } = this.state;
    const result = num1 / num2;
    this.setState({ result });
  };
<div>
        <label>
          Number 1:
          <input type="text" value={this.state.num1} onChange={this.Num1Change} />
        </label>
        <br />
        <label>
          Number 2:
<input type="text" value={this.state.num2} onChange={this.Num2Change} />
        </label>

<br />
        <button onClick={this.addNumbers}>Division</button>
        <br />
        <div>
          Result: {this.state.result}
        </div>
Output:




Comments

Popular posts from this blog

Building a Full-Stack Student Management System with React.js and NestJS

Today I have solved the problem in javascript

Today I have solved problem in javascript