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.
- How to validate Hexadecimal Color Code using Regular Expression
- Program to calculate Resistance using given color code in circuits
- Color a grid such that all same color cells are connected either horizontally or vertically
- Color N boxes using M colors such that K boxes have different color from the box on its left
- Program to Change RGB color model to HSV color model
- Find the missing end tag in the given HTML Code
- Minimum steps to color the tree with given colors
- Number of ways to color N-K blocks using given operation
- Decimal Equivalent of Gray Code and its Inverse
- How to validate pin code of India using Regular Expression
- How to validate IFSC Code using Regular Expression
- Number of ways to color boundary of each block of M*N table
- Ways to color a skewed tree such that parent and child have different colors
- Color all boxes in line such that every M consecutive boxes are unique
- Check whether it is possible to convert A into B
- Convert given integer X to the form 2^N - 1
- Convert to number with digits as 3 and 8 only
- Program to convert Degree to Radian
- Convert 1 into X in min steps by multiplying with 2 or 3 or by adding 1
- 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.