
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Generate colors between #CCCCCC and #3B5998 for a color meter with JavaScript?
We have to write a function that generates a random color between two given colors. Let’s tackle this problem in parts −
First → We write a function that generates a random number between two given numbers.
Second → Instead of using the hex scale for random color generation, we will map the hex to 0 to 15 decimal scale and use that instead.
Lastly → We loop over any of the given color strings and generate a random color.
Example
const randomBetween = (a, b) => { const max = Math.max(a, b); const min = Math.min(a, b); return Math.floor(Math.random() * (max - min) + min); }; const randomColor = (firstColor, secondColor) => { first = firstColor.toUpperCase().substring(1, secondColor.length); second = secondColor.toUpperCase().substring(1, firstColor.length); const scale = '0123456789ABCDEF'; let color = '#'; for(let i = 0; i < first.length && i < second.length; i++ ){ const random = randomBetween(scale.indexOf(first[i]), scale.indexOf(second[i])); color += scale[random]; }; return color; }; console.log(randomColor('#34324a', '#42342c')); console.log(randomColor('#f43250', '#12342c')); console.log(randomColor('#34324a', '#47942c')); console.log(randomColor('#ffffff', '#000000'));
Following is the possible output in the console −
Note − This is just one of the many possible outputs as the output is random everytime.
Output
#33332A #C23328 #36822B #35102A
- Related Articles
- JavaScript to generate random hex codes of color
- How to generate random colors in Matplotlib?
- Difference between Multimeter and Clamp Meter
- Difference between Wattmeter and Energy Meter
- Write the relation between nanometer and meter.
- How can I generate more colors on a pie chart in Matplotlib?
- JavaScript - Complementary Colors Builder
- How to change bullet colors for lists with CSS?
- Generate n random numbers between a range and pick the greatest in JavaScript
- How to set the color of the rule between columns with JavaScript?
- Map the colors of the object to their opposite values in the color spectrum with CSS
- How to set outline color with JavaScript?
- Generate Ranking with combination of strings in JavaScript
- How to show different colors for points and line in a Seaborn regplot?
- Generate a Regular Expression for a Finite Automata with state elimination method

Advertisements