help in writing a class

Hello,

I am writing class works with three colors intensity Red Green Blue

the difficulty starts here
1
2
3
4
5
mixture’s job is to create, and return by value, a Color object that is the result of averaging 
the 2 Colors it was passed. For instance, if green   were a Color variable with intensities <0, 255, 0>,
blue were a Color variable with intensities <0, 0, 255>, and main said
mixture( green, blue ).output()
then the output  member function would output <0, 128, 128>


first of all VS is not giving me 128 instead 127, since the result of dividing
255/2 = 127.5

second how do I need extra variables to get the average in mixture function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
int main()
{
	Color background(0, 270, 84);
	Color A( 17, 204, 150);
	A.output();
	cout << endl;
	Color green(0, 255, 0);
	Color blue(0, 0, 255);
	mixture(green, blue).output();
}

//Constructor functions Definition
Color::Color(unsigned red, unsigned green, unsigned blue)
	:redVal(red), greenVal(green), blueVal(blue)
{
	if (red<0 && red>255) die("Invalid red coordinate");
	if (green<0 && green>255) die("Invalid green coordinate");
	if (blue<0 && blue>255) die("Invalid blue coordinate");
}

//Accessor functions Definition
unsigned Color::getRed() const
{
	return redVal;
}
unsigned Color::getGreen() const
{
	return greenVal;
}
unsigned Color::getBlue() const
{
	return blueVal;
}

//Mutator functions Definition
Color & Color::setRed(unsigned red)
{
	redVal = red;
	return *this;
}
Color & Color::setGreen(unsigned green)
{
	greenVal = green;
	return *this;
}
Color & Color::setBlue(unsigned blue)
{
	blueVal = blue;
	return *this;
}

//
const Color & Color::output() const
{
	cout <<"<"<< redVal << "," << greenVal << "," << blueVal <<">"<< endl;
	return *this;
}


up to this point everything should be OK

but here is where I am stuck

1
2
3
4
5
6
7
8
Color mixture(const Color & color0, const Color & color1, double weight)
{
	
	Color colav,colav1;
	colav = (color0.getGreen()) / 2;
	colav1 = (color1.getBlue()) / 2;
	return colav, colav1;
}
Last edited on
You only need to declare one Color object and then call its setGreen, setBlue, and setRed functions. Then return that single object. The problem you have now is that you've declared two Color objects and you're trying to return them both.
Your function isn't averaging colors right now; it's just dividing one component of each color by two. Furthermore, you only want to return one Color since a mixture of two Colors creates one Color. Consider changing your function to something like this:

1
2
3
4
5
6
7
8
9
10
11
12

Color mixture(const Color & color0, const Color & color1, double weight)      //weight is a fraction from 0 to 1: weight = (amount of color0)/(amount of color0 + amount of color1)  
{
   Color mixedcolor(0, 0, 0);      //Need to initialize the Color

   //Don't include both of these
   mixedcolor.setBlue(weight*color0.getBlue() + (1-weight)*color1.getBlue());      //Weighted version
   mixedcolor.setBlue((color0.getBlue() + color1.getBlue())/2);      //Unweighted version
   //repeat for red, green
   
   return Color;
}


EDIT: Forgot to divide by two in average.
Last edited on
Thanks both of you were helpful

now I am having problem with rounding I tried the ways I know such as adding .5 or static_cast but didn't work

"warning C4244: 'argument"

You will need to show your code.

@hyperfine - Line 11 should be:
 
  return mixedcolor;


You can't return a type name.
1
2
3
4
5
6
7
8
9
10
11
12
13
Color mixture(const Color & color0, const Color & color1, double weight)
{
	Color mixedcol(0,0,0);
	mixedcol.setRed((color0.getRed() + color1.getRed()) / 2);
	mixedcol.setGreen((color0.getGreen() + color1.getGreen()) / 2);
	mixedcol.setBlue((color0.getBlue() + color1.getBlue()) / 2);

	mixedcol.setRed(weight*color0.getRed() + (1-weight)*color1.getRed());
	mixedcol.setGreen(weight*color0.getGreen() + (1-weight)*color1.getGreen());
	mixedcol.setBlue(weight*color0.getBlue() + (1-weight)*color1.getBlue());

	return mixedcol;
}
please need help to finish this assignment I having problem rounding the answers
Topic archived. No new replies allowed.