Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C ++ program .. (this code should run in C++) HEX/RGB Converter In this program

ID: 3662651 • Letter: C

Question

C ++ program .. (this code should run in C++)

HEX/RGB Converter

In this program you are to present a User with a menu. The menu should have the following options:

Enter RGB values

Enter HEX value

Exit

Your program can assume integer input.

If an input is outside of the range of values, you should display an error message, and re-display the menu.

When the User selects Option 1, you will prompt them for a red, green, and blue value. For example:

Enter a value for Red (0-255): 200

Enter a value for Green (0-255): 130

Enter a value for Blue (0-255): 57

Your program can assume integer input with the specified range

Upon entering the values, your program should display the hexadecimal representation of the RGB values entered. Below is an example:

RGB( 200, 130, 57 ) = 0xC88239

Conversely, if the User selections Option 2, You should prompt the User for a 6-Character hex value, and display the corresponding RGB values. For example:

Enter a hex value: C88239

0xC88239 = RGB( 200, 130, 57 )

Your program can assume the input is a valid uppercase 6-character hex value

Your program should continuously loop until Option 3 is selected.

ConverterClass

To solve this problem, you are to implement a Converter class that contains 2 constructors.

One constructor will take a string that represents a 6-character hex value.

The second constructor will take 3 integer values the represent a red, green, and blue channel respectively.

To display the results, you should use accessors to print the required values.

Explanation / Answer

Answer -


#include <iostream>
#include <sstream>
#include<string>

void hex2rgb(char hex[])
{
int* rgb = new int[3];
std::stringstream ss;
std::string str;
int size = sizeof(hex);
for (int i = 0; i < 3; i++) {
if (size == 3) { str = std::string(2, hex[i]); }
else if (size == 6) { str = std::substr(i * 2, 2); }
else { break; }
ss << std::hex << str;
ss >> rgb[i];
ss.clear();
}
std::cout<<rgb<<rgb+2<<rgb+4;
}

int main()
{
int r,g,b,ch;
std::cout<<"Enter 1 for RGB values";
std::cout<<" Enter 2 for hexadecimal value";
std::cout<<" Enter 3 for Exit";
std::cout<<" Enter Choice : ";
std::cin>>ch;
do
{
if(ch==1)
{
std::cout<<" Enter Red value : ";
std::cin>>r;
std::cout<<" Enter Green Value: ";
std::cin>>g;
std::cout<<" Enter blue Value: ";
std::cin>>b;
std::cout<< " The number in hexadecimal: 0x" << std::hex <<r<< std::hex <<g<< std::hex <<b<< ' ';
break;
  
}
else if(ch==2)
{
char st[100];
std::cout<<" Enter hexadecimal value : ";
std::cin>>st;
hex2rgb(st);
break;
}
}while(ch!=3);
  
}