- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
JavaScript - Complementary Colors Builder
We are required to write a JavaScript function that takes in hex color as one and only input.
Our function should then find the complementary color for the color taken in as input.
Here are some input and output pairs −
getComplementaryColor('#142814') = '#ebd7eb'; getComplementaryColor('#ffffff') = '#000000'; getComplementaryColor('#3399ff') = '#cc6600';
Example
The code for this will be −
const str1 = '#142814'; const str2 = '#ffffff'; const str3 = '#3399ff'; const getComplementaryColor = (color = '') => { const colorPart = color.slice(1); const ind = parseInt(colorPart, 16); let iter = ((1 << 4 * colorPart.length) - 1 - ind).toString(16); while (iter.length < colorPart.length) { iter = '0' + iter; }; return '#' + iter; }; console.log(getComplementaryColor(str1)); console.log(getComplementaryColor(str2)); console.log(getComplementaryColor(str3));
Output
And the output in the console will be −
#ebd7eb #000000 #cc6600
- Related Articles
- LongStream builder() method in Java
- DoubleStream builder() method in Java
- IntStream builder() method in Java
- Complementary Code Keying (CCK)
- What are complementary angles?
- How to implement Builder pattern in Kotlin?
- Generate colors between #CCCCCC and #3B5998 for a color meter with JavaScript?
- How to use string Builder class in android?
- What are complementary and supplementary angles ?
- Defining Colors in CSS3
- Sort Colors in Python
- Difference between String buffer and String builder in Java
- Which is best a web designer or a web builder
- What is the Complementary angle of Angle 55°?
- How to select count with Laravel's fluent query builder?

Advertisements