
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
RGB color to hexadecimal color JavaScript
We are required to write a JavaScript function that takes in a RGB color and returns its hexadecimal representation.
The function should take in an object containing three numbers representing the respective values of red green and blue color.
For example:
rgbToHex(0, 128, 192) should return '#0080C0'
The code for this will be −
const rgbColor = { red: 0, green: 51, blue: 155 } function rgbToHex({ red: r, green: g, blue: b }) { const prefix = '#'; const hex = prefix + ((1 << 24) + (r << 16) + (g << 8) + b) .toString(16) .slice(1); return hex; }; console.log(rgbToHex(rgbColor));
Following is the output on console −
#00339b
- Related Questions & Answers
- Hexadecimal color to RGB color JavaScript
- C Program to Change RGB color model to HSV color model
- How to use rgb color codes in tkinter?
- How can RGB color space be converted to a different color space in Python?
- How to pass RGB color values to Python's Matplotlib eventplot?
- Random color generator in JavaScript
- Changing color randomly in JavaScript
- How to set outline color with JavaScript?
- Change Cursor Color with CSS caret-color
- Randomize color by number in JavaScript
- Generating random hex color in JavaScript
- JavaScript to generate random hex codes of color
- How to Change Link Underline Color using text-decoration-color CSS
- HTML Color Styles
- Add hover color to background color of the table row in Bootstrap
Advertisements