Question
2) Let color be the following structure: struct color int red; int green; int blue; write functions for question a) to ? 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 colorl, 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)
Explanation / Answer
struct color { int red; int green; int blue; }; struct color make_color(int red, int green, int blue) { struct color c; if(red < 0) { red = 0; } else if(red > 255) { red = 255; } if(green < 0) { green = 0; } else if(green > 255) { green = 255; } if(blue < 0) { blue = 0; } else if(blue > 255) { blue = 255; } c.blue = blue; c.green = green; c.red = red; return c; } int getRed(struct color c) { return c.red; } bool equal_color(struct color color1, struct color color2) { return (color1.blue == color2.blue) && (color1.green == color2.green) && (color1.red == color2.red); }