Given three colors as R, G and B, convert this RGB color to hex color code. If the conversion is not possible, print -1.
Input: R = 0, G = 0, B = 0
Input: R = 255, G = 255, B = 256
256 color code is not possible as only 0-255 range is available for a color.
- First check if each of the given colors is in the range 0-255 or not.
- If not, then print -1 and exit the program as no conversion is possible in this case.
- If they are in range, then for each color, convert the given color code into its equivalent hexadecimal number.
- If the hexadecimal value is of 1 digit, add 0 to the left to make it 2 digit.
- Then in the final answer, add ‘#’ at the starting followed by the hexadecimal values of R, G, and B respectively.
Below is the implementation of the above approach.
#000000 #FFFFFF #9183B7 #020304 -1
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Program to Change RGB color model to HSV color model
- Program to calculate Resistance using given color code in circuits
- Color N boxes using M colors such that K boxes have different color from the box on its left
- Color a grid such that all same color cells are connected either horizontally or vertically
- How to validate Hexadecimal Color Code using Regular Expression
- Minimum given operations required to convert a given binary string to all 1's
- Convert a given temperature to another system based on given boiling and freezing points
- Minimize cost to convert given two integers to zero using given operations
- Minimum steps to color the tree with given colors
- Number of ways to color N-K blocks using given operation
- Check if quantities of 3 distinct colors can be converted to a single color by given merge-pair operations
- Find the missing end tag in the given HTML Code
- Program to convert a given number to words
- Program to convert a given number to words | Set 2
- Convert a number m to n using minimum number of given operations
- Minimum number of given operation required to convert n to m
- Convert given integer X to the form 2^N - 1
- Minimum operations required to convert X to Y by multiplying X with the given co-primes
- Convert N to M with given operations using dynamic programming
- Count the numbers which can convert N to 1 using given operation
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.