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

Note that these are colour images. In a colour image, each pixel is represented

ID: 3764323 • Letter: N

Question

Note that these are colour images. In a colour image, each pixel is represented by a trio of values, R, G, and B, which are floating point numbers between 0.0 and 1.0. R represents the amount of red, G represents the amount of green, and B represents the amount of blue. Using various combinations of amounts of red, green, and blue, we can display just about any colour we want, but exactly which combinations of R, G and B yield which colours isn't really important to our discussion here.

To blend the above two images, sort of like a cross-fade, we would, for each pixel in the parrot image, obtain its colour (as an R, G, B triple), obtain the colour of the pixel at the same position in the cat image (as another R, G, B triple), and then find the average of the two colours. If (r1, g1, b1) is the colour of the first pixel, and (r2, g2, b2) is the colour of the second pixel, then the average of those two colours is:

( (r1+r2)/2, (g1+g2)/2, (b1+b2)/2 ).

Define a record type (i.e. a struct in C++) called Colour to store the red, green and blue values for one pixel. It's up to you to choose appropriate types for the members of your record.

Write a function called blend that takes two Colour records as parameters and returns a Colour record that is the average of the two colours.

Write a function called printColour that prints out the contents of a Colour record in a reasonable way that identifies which values are which. For example: if the Colour record contained the colour (0.5, 0.7 0.9) the function might print something like this:

R: 0.5, G: 0.7, B: 0.9

In your main() function, declare and initialize two Colour records with colour data of your choosing (remember: colour values should be between 0.0 and 1.0!).

Use the blend function from step 2 to obtain the average of these two colours.

Print out the two original colours and the blended colour using your function from step 3.

Explanation / Answer

#include <iostream>

using namespace std;

typedef struct Color
{
   float red,green,blue;
}pixel;

pixel blend(pixel a, pixel b)
{
   pixel c;
   c.red = (a.red + b.red )/2.0;
   c.green = (a.green + b.green )/2.0;
   c.blue = (a.blue + b.blue )/2.0;
   return c;
}

void printColor(pixel a)
{
   cout<<"R:"<<a.red<<", G:"<<a.green<<", B:"<<a.blue<<endl;
}

int main()
{
   pixel a,b,c;
   a.red = 0.1;
   a.green = 0.2;
   a.blue = 0.3;

   b.red = 0.4;
   b.green = 0.5;
   b.blue = 0.6;

   c = blend(a,b);
   cout<<"Original colors: ";
   printColor(a);
   printColor(b);
   cout<<"Averaged color ";
   printColor(c);

}

/*******BELOW CODE IS WITHOUT USING TYPEDEF******/

#include <iostream>

using namespace std;

struct Color
{
   float red,green,blue;
};

struct Color blend(struct Color a,struct Color b)
{
   struct Color c;
   c.red = (a.red + b.red )/2.0;
   c.green = (a.green + b.green )/2.0;
   c.blue = (a.blue + b.blue )/2.0;
   return c;
}

void printColor(struct Color a)
{
   cout<<"R:"<<a.red<<", G:"<<a.green<<", B:"<<a.blue<<endl;
}

int main()
{
   struct Color a,b,c;
   a.red = 0.1;
   a.green = 0.2;
   a.blue = 0.3;

   b.red = 0.4;
   b.green = 0.5;
   b.blue = 0.6;

   c = blend(a,b);
   cout<<"Original colors: ";
   printColor(a);
   printColor(b);
   cout<<"Averaged color ";
   printColor(c);

}

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