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

Let color be the following structure: struct color { int red; int green; int blu

ID: 3712929 • Letter: L

Question

Let color be the following structure: struct color { int red; int green; int blue; }

Write functions for question a) to f)

a) struct color make_color (int red, int green, int blue); Return a color structure containing the specified red, green, and blue values. If any argument is less than zero, the corresponding member of the structure will contain zero instead. If any argument is greater than 255, the corresponding member of the structure will contain 255.

b) int getRed(struct color c) Returns the value of c’s red member

c) bool equal_color(struct color color1, struct color color2); Returns true if the corresponding members of color1 and color2 are equal. (Note: please typedef int as bool, and define true and false as Macros) 2

d) struct color brighter(struct color c); Returns a color structure that represents a brighter version of the color c. The structure is identical to c, except that each member has been divided by 0.7 (with the result truncated to an integer). However, there are three special cases: (1) If all members of c are zero, the function returns a color whose members all have the value 3. (2) If any member of c is greater than 0 but less than 3, it is replaced by 3 before the division by 0.7. (3) If dividing by 0.7 causes a member to exceed 255, it is reduced to 255.

e) struct color darker(struct color c) Returns a color structure that represents a darker version of the color c. The structure is identical to c, except that each member has been multiplied by 0.7 (with the result truncated to an integer).

f) void reset(struct color *p) Reset the red, green and blue member to 0 of the color by using a pointer pointing to it.

g) Write a declaration for a const variable named MAGENTA of type struct color and initialize its members as 255,0, and 255, respectively.

h) Write statements to declare an array of colors using pointer and allocate memory space to it using malloc. The size of the array is 5.

i) Continue the question above and write statements to initialize the colors in the array. Each member in the color should be initialized as 0.

Note: Please test the correctness of the program by writing a main function. And display the output.

Explanation / Answer

Given
struct color
{
int red;
int green;
int blue;
};

Answer for questions:
--------------------

Answer a)
---------
struct color make_color (int red, int green, int blue)
{
struct color c;

//check red is in valid range and set appropriately
if(red < 0)
c.red = 0;
else if(red > 255)
c.red = 255;
else
c.red = red;

//check green is in valid range and set appropriately
if(green < 0)
c.green = 0;
else if(green > 255)
c.green = 255;
else
c.green = green;


//check blue is in valid range and set appropriately
if(blue < 0)
c.blue = 0;
else if(blue > 255)
c.blue = 255;
else
c.blue = blue;


return c;
  
}


=========================

Answer b)
---------
int getRed(struct color c)
{
return c.red;
}


=========================
Answer c)
---------
#define true 1
#define false 0
typedef int bool;

bool equal_color(struct color color1, struct color color2)
{
if(color1.red == color2.red && color1.green == color2.green && color1.blue == color2.blue)
return true;
else
return false;
}

=========================
Answer d)
---------
struct color brighter(struct color c)
{
if(c.red == 0 && c.green == 0 && c.blue == 0)
{
c.red = 3;
c.green = 3;
c.blue = 3;
}
else
{
//if any component is > 0 but < 3, set it 3 before dividing by 0.7
if(c.red > 0 && c.red < 3)
c.red = 3;
if(c.green > 0 && c.green < 3)
c.green = 3;
if(c.blue > 0 && c.blue < 3)
c.blue = 3;

c.red = c.red / 0.7;
c.green = c.green / 0.7;
c.blue = c.blue / 0.7;

//if any component is greater than 255 , set it to 255
if(c.red > 255)
c.red = 255;
if(c.green > 255)
c.green = 255;
if(c.blue > 255)
c.blue = 255;
  
}

return c;
}

=========================
Answer e)
---------
struct color darker(struct color c)
{
c.red = c.red * 0.7;
c.green = c.green * 0.7;
c.blue = c.blue * 0.7;
return c;
}

=========================
Answer f)
---------

void reset(struct color *p)
{
p->red = 0;
p->green = 0;
p->blue = 0;
}


=========================
Answer g)
---------
const struct color MAGENTA = {255, 0, 255};


=========================
Answer h)
---------
struct color *colArr = (struct color *) malloc(sizeof(struct color) * 5);


=========================
Answer h)
---------
int i;
for(i = 0; i < 5; i++)
colArr[i] = make_color(0, 0, 0);

=============================================================
Please do rate the answer if it helped. Thank you.