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

Need Help Please! C++ program: Within a computer, color is often maintained as a

ID: 3816852 • Letter: N

Question

Need Help Please! C++ program:

Within a computer, color is often maintained as an object with three components, a red channel, a green channel, and a blue channel, each with values between 0-255. The attached file(testcolors.txt) has four fields per line to represent a color. The first three fields are integers from 0 to 255 representing the red, green, and blue channels of the color, and the fourth field is the standard name of the color. testcolors.txt as below:-

000 206 209 darkturquoise
047 079 079 darkslategrey
100 149 237 cornflowerblue
124 252 000 lawngreen
139 000 000 darkred
165 042 042 brown
188 143 143 rosybrown
218 112 214 orchid
238 232 170 palegoldenrod
248 248 255 ghostwhite
255 099 071 tomato
255 228 181 moccasin
255 255 000 yellow

The RGB color space can be converted to the HSL color space using the following algorithm. In your programming do the following from a to l.

a-Convert the RGB levels to values between 0.0 and 1.0.
b-Find the maximum and minimum values of the three (red, green, and blue).
c-Compute the sum S and the difference D of the maximum and minimum values.
d-The luminance value is the average of the min and max (ie. (min + max) / 2).
e-If the min and max are equal, hue and saturation are both 0.0, and the conversion is complete
f-if luminance is less than 0.5, saturation is D / S, otherwise saturation is D / (2.0 - S).
g-if red was the maximum, hue is (green - blue) / D
h-if green was the maximum, hue is 2.0 + (blue - red) / D
i-if blue was the maximum, hue is 4.0 + (red - green) / D
j-Multiple hue by 60.0 to convert it to degrees.
k-If hue is negative, add 360.0 degrees.
l-This produces the HSL values from RGB values.

------For this assignment we need to write two classes as below------
class RGBColor {
short red;
short green;
short blue;
std::string name;
public:
RGBColor(std::string n, int r, int g, int b);
short getRed() const;
short getGreen() const;
short getBlue() const;
std::string getName();
std::string toString();
};
class HSLColor {
float hue;
float sat;
float light;
std::string name;
void rgb2hsl(RGBColor& clr);
public:
HSLColor(RGBColor& clr);
float getHue();
float getSaturation();
float getLuminance();
std::string getName();
std::string toString();
};

Code the test function int main() to read the color data, one color per line, and create an RCGColor object from the data.

The RGBColor object should then be used to create an HSLCoilor object which will convert the RGB to HSL according to the given algorithm. The HSL object should then be dispayed using its toString() method.
The main loop of the int main() test driver should look like:
ifs >> red;
while (!ifs.eof()){
++rcd;
ifs >> green >> blue >> name;
rgb = new RGBColor(name, red, green, blue);
hsl = new HSLColor(*rgb);
cout << rgb->toString() << " = " << hsl->toString() << " " << rgb->getName() << endl;
ifs >> red; }

The output for this assignment should look like as below:-
RGB=( 0, 206, 209) = HSL=( 180.9, 1.000, 0.410) darkturquoise
RGB=( 47, 79, 79) = HSL=( 180.0, 0.254, 0.247) darkslategrey
RGB=( 100, 149, 237) = HSL=( 218.5, 0.792, 0.661) cornflowerblue
RGB=( 124, 252, 0) = HSL=( 90.5, 1.000, 0.494) lawngreen
RGB=( 139, 0, 0) = HSL=( 0.0, 1.000, 0.273) darkred
RGB=( 165, 42, 42) = HSL=( 0.0, 0.594, 0.406) brown
RGB=( 188, 143, 143) = HSL=( 0.0, 0.251, 0.649) rosybrown
RGB=( 218, 112, 214) = HSL=( 302.3, 0.589, 0.647) orchid
RGB=( 238, 232, 170) = HSL=( 54.7, 0.667, 0.800) palegoldenrod
RGB=( 248, 248, 255) = HSL=( 240.0, 1.000, 0.986) ghostwhite
RGB=( 255, 99, 71) = HSL=( 9.1, 1.000, 0.639) tomato
RGB=( 255, 228, 181) = HSL=( 38.1, 1.000, 0.855) moccasin
RGB=( 255, 255, 0) = HSL=( 60.0, 1.000, 0.500) yellow

Thank You So Much for Helping.

Explanation / Answer

#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

class RGBColors{
   short red;
   short green;
   short blue;
   string name;
   public:
       RGBColors(){};
       RGBColors(string n, int r, int g, int b)
       {
           name = n;
           red = r;
           green = g;
           blue = b;
       };
       short getRed()
       {
           return red;
       }
       short getGreen()
       {
           return green;
       }
       short getBlue(){
           return blue;
       }
       string getName(){
           return name;
       }
};

class HSLColor{
   float hue;
   float sat;
   float light;
  
   float red;
   float green;
   float blue;
   std::string name;
   float S, D, min, max;
   void rgb2hsl(RGBColors*& clr)
   {
       hue = getHue();
       hue = hue * 60.0;
       if (hue<0){
           hue = hue+360.0;
       }
       sat = getSaturation();
       light = getLuminance();
   };
   public:
       HSLColor(){};
       HSLColor(RGBColors*& clr){
           if (clr->getRed() > 0)
               red = (clr->getRed())/100;
           if (clr->getGreen() > 0)
               green = (clr->getGreen())/100;
           if (clr->getBlue() > 0)
               blue = (clr->getBlue())/100;
           name = (clr->getName());
           max = red; min = blue;
           if (green > max) {
               max = green;
           }
           if (blue > max) {
               max = blue;
           }
           if (red < green && red < blue)
           {
               min = red;
           }else if (green<blue && green < red)
               min = green;
           S = min+max;
           D = max - min;
           rgb2hsl(clr);
       };
       float getHue(){
           if (min == max)
               hue = 0;
           else{
               if(max == red)
                   hue = (green - blue) / D;
               else if (max == green)
                   hue = 2.0 + (blue - red) / D;
               else
                   hue = 4.0 + (red - green) / D;
           }
           hue = hue * 60.0;
           if (hue<0){
               hue = hue+360.0;
           }
           return hue;
       };
       float getSaturation(){
           if (min == max)
               sat = 0;
           else {
               float lumin = getLuminance();
               if (lumin < .5){
                   sat = D / S;
               }else {
                   sat = D / (2.0 - S);
               }
           }
           cout << min;
               return sat;
       };
       float getLuminance(){
           light = (min + max) / 2;
           return light;
       };
       std::string getName(){
           return name;
       };
};
int main()
{
   string redclrs[30], greenclrs[30], blueclrs[30], strvlaue[30];
   int rid = 0, gid = 0, bid = 0, stid = 0;
   ifstream myfile("test.txt"); //use your file name
   if(!myfile)
   {
       cout<<"Error opening output file"<<endl;
       return 0;
   }
   int count = 0;
   while(!myfile.eof())
   {
       getline(myfile,redclrs[rid], ' ');
       getline(myfile,greenclrs[gid],' ');
       getline(myfile,blueclrs[bid],' ');
       getline(myfile,strvlaue[stid],' ');
       rid++; gid++; bid++; stid++;count++;
   }
  
   int inc; int redcs, greencs, bluecs;
   if (count>0){
       for(inc=0; inc<count; inc++)
       {
           stringstream redc(redclrs[inc]);
           redc >> redcs;
           stringstream greenc(greenclrs[inc]);
           greenc >> greencs;
           stringstream bluec(blueclrs[inc]);
           bluec >> bluecs;
           RGBColors* rgb = new RGBColors(strvlaue[inc], redcs, greencs, bluecs);
           HSLColor* hsl = new HSLColor(rgb);
cout << "RGB=("<<redcs<<","<<greencs<<","<<bluecs<<") = HSL = (" << hsl->getHue()<<","<<hsl->getSaturation()<< ","<<hsl->getLuminance()<<hsl->getName()<< ")";
           cout << endl;
       }
   }
  
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote