C++ Within a computer, color is often maintained as an object with three compone
ID: 3842914 • Letter: C
Question
C++ 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 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.RGB colors are difficult to interpret, in that it is not easy for a person to imagine the color that results from various levels of red, green, and blue. This has given rise to other color descriptions (color spaces). One of the most common is hue, saturation, and luminance or HSL.
The RGB color space can be converted to the HSL color space using the following algorithm.
Convert the RGB levels to values between 0.0 and 1.0. Find the maximum and minimum values of the three (red, green, and blue). Compute the sum S and the difference D of the maximum and minimum values. The luminance value is the average of the min and max (ie. (min + max) / 2). If the min and max are equal, hue and saturation are both 0.0, and the conversion is complete if luminance is less than 0.5, saturation is D / S, otherwise saturation is D / (2.0 - S). if red was the maximum, hue is (green - blue) / D if green was the maximum, hue is 2.0 + (blue - red) / D if blue was the maximum, hue is 4.0 + (red - green) / D Multiple hue by 60.0 to convert it to degrees. If hue is negative, add 360.0 degrees. This produces the HSL values from RGB values.
For this assignment, write two classes
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(); };
and
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 assign_colors() 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 output should look like:
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
The main loop of the assign_colors() 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; } Input data "testcolor.txt" 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 C++ 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 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.
RGB colors are difficult to interpret, in that it is not easy for a person to imagine the color that results from various levels of red, green, and blue. This has given rise to other color descriptions (color spaces). One of the most common is hue, saturation, and luminance or HSL.
The RGB color space can be converted to the HSL color space using the following algorithm.
Convert the RGB levels to values between 0.0 and 1.0. Find the maximum and minimum values of the three (red, green, and blue). Compute the sum S and the difference D of the maximum and minimum values. The luminance value is the average of the min and max (ie. (min + max) / 2). If the min and max are equal, hue and saturation are both 0.0, and the conversion is complete if luminance is less than 0.5, saturation is D / S, otherwise saturation is D / (2.0 - S). if red was the maximum, hue is (green - blue) / D if green was the maximum, hue is 2.0 + (blue - red) / D if blue was the maximum, hue is 4.0 + (red - green) / D Multiple hue by 60.0 to convert it to degrees. If hue is negative, add 360.0 degrees. This produces the HSL values from RGB values.
For this assignment, write two classes
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(); };
and
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 assign_colors() 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 output should look like:
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
The main loop of the assign_colors() 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; } Input data "testcolor.txt" 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 C++ 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 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.
RGB colors are difficult to interpret, in that it is not easy for a person to imagine the color that results from various levels of red, green, and blue. This has given rise to other color descriptions (color spaces). One of the most common is hue, saturation, and luminance or HSL.
The RGB color space can be converted to the HSL color space using the following algorithm.
Convert the RGB levels to values between 0.0 and 1.0. Find the maximum and minimum values of the three (red, green, and blue). Compute the sum S and the difference D of the maximum and minimum values. The luminance value is the average of the min and max (ie. (min + max) / 2). If the min and max are equal, hue and saturation are both 0.0, and the conversion is complete if luminance is less than 0.5, saturation is D / S, otherwise saturation is D / (2.0 - S). if red was the maximum, hue is (green - blue) / D if green was the maximum, hue is 2.0 + (blue - red) / D if blue was the maximum, hue is 4.0 + (red - green) / D Multiple hue by 60.0 to convert it to degrees. If hue is negative, add 360.0 degrees. This produces the HSL values from RGB values.
For this assignment, write two classes
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(); };
and
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 assign_colors() 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 output should look like:
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
The main loop of the assign_colors() 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; } Input data "testcolor.txt" 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 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
Explanation / Answer
//RGBColor.h
#ifndef RGB_H
#define RGB_H
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
class RGBColor
{
short red;
short green;
short blue;
std::string name;
public:
RGBColor()
{
name="";
red=0;
green=0;
blue=0;
}
RGBColor(std::string n, int r, int g, int b)
{
name=n;
red=r;
green=g;
blue=b;
}
short getRed() const
{
return red;
}
short getGreen() const
{
return green;
}
short getBlue() const
{
return blue;
}
std::string getName()
{
return name;
}
std::string toString()
{
stringstream rgb;
rgb<<"RGB=("<<red<<" , "<<green<<", "<<blue<<")";
return rgb.str();
}
};
#endif RGB_H
--------------------------------------------------------------------------------------------------
//HSLColor.h
#ifndef HSL_H
#define HSL_H
#include<iostream>
#include<sstream>
#include<string>
#include "RGBColor.h"
using namespace std;
/* minimum of 3 values */
#define min3(a, b, c) ((a)<(b) ? ((a)<(c) ? (a) : (c)) : ((b)<(c) ? (b) : (c)))
/* maximum of 3 values */
#define max3(a, b, c) ((a)>(b) ? ((a)>(c) ? (a) : (c)) : ((b)>(c) ? (b) : (c)))
class HSLColor
{
float hue;
float sat;
float light;
std::string name;
void rgb2hsl(RGBColor& clr);
public:
HSLColor(RGBColor& clr)
{
rgb2hsl(clr);
}
float getHue()
{
return hue;
}
float getSaturation()
{
return sat;
}
float getLuminance()
{
return light;
}
std::string getName()
{
return name;
}
std::string toString()
{
stringstream hsl;
hsl<<"HSL=( "<<hue<<" , "<<sat<<", "<<light<<")";
return hsl.str();
}
};
//Conversion from rgb to hsl
//Note :The conversion has little -ve values.
//Your povided algorithm may have error.
void HSLColor::rgb2hsl(RGBColor& clr)
{
double max, min, S,D=0;
double r=clr.getRed();
double g=clr.getGreen();
double b=clr.getBlue();
max = max3(r,g,b);
min = min3(r,g,b);
light = (max + min) / 2;
if (max == min)
{
hue = sat = 0;
}
else
{
if(light<0.5)
{
sat=D/S;
}
else
{
sat=D/(2.0-S);
}
if(max==r)
{
hue=(g-b)/D;
}
else if(max==g)
{
hue=2.0+(b-r)/D;
}
else if(max==b)
{
hue=4.0+(r-g)/D;
}
hue = hue * 60;
if (hue < 0)
hue += 360;
}
}
#endif HSL_H
--------------------------------------------------------------------------------------------------
//tester.cpp
#include<iostream>
#include<fstream>
#include "RGBColor.h"
#include "HSLColor.h"
using namespace std;
int main()
{
float red,green,blue;
string name;
RGBColor *rgb;
HSLColor *hsl;
//Create an input file streeam
ifstream ifs;
//open input file testcolor.txt
ifs.open("testcolor.txt");
ifs >> red;
while (!ifs.eof())
{
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;
}
system("pause");
return 0;
}
--------------------------------------------------------------------------------------------------
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
--------------------------------------------------------------------------------------------------
Output:
RGB=( 0 , 206, 209) = HSL=( 180.861 , -1.00966, -4.31602e+008) darkturquoise
RGB=( 47 , 79, 79) = HSL=( 180 , -0.258065, -4.31602e+008) darkslategrey
RGB=( 100 , 149, 237) = HSL=( 218.54 , -0.408955, -4.31602e+008) cornflowerb
lue
RGB=( 124 , 252, 0) = HSL=( 90.4762 , -1.008, -4.31602e+008) lawngreen
RGB=( 139 , 0, 0) = HSL=( 0 , -1.0146, -4.31602e+008) darkred
RGB=( 165 , 42, 42) = HSL=( 0 , -0.6, -4.31602e+008) brown
RGB=( 188 , 143, 143) = HSL=( 0 , -0.136778, -4.31602e+008) rosybrown
RGB=( 218 , 112, 214) = HSL=( 302.264 , -0.323171, -4.31602e+008) orchid
RGB=( 238 , 232, 170) = HSL=( 54.7059 , -0.167488, -4.31602e+008) palegolden
rod
RGB=( 248 , 248, 255) = HSL=( 240 , -0.0139721, -4.31602e+008) ghostwhite
RGB=( 255 , 99, 71) = HSL=( 9.13043 , -0.567901, -4.31602e+008) tomato
RGB=( 255 , 228, 181) = HSL=( 38.1081 , -0.170507, -4.31602e+008) moccasin
RGB=( 255 , 255, 0) = HSL=( 60 , -1.00791, -4.31602e+008) yellow
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.