Develop a structure for storing color ( Red , Green , Blue components).

To provide for the possibility of forming color from individual components, obtaining any component, increasing / decreasing the brightness of all components, increasing / decreasing the brightness of an individual component.

Develop a structure for storing color circle data + functions for manipulating variables of this type.

Create an array of colored circles and develop a function to sort the circles according to different signs: by area, by color, by distance from the origin.

Direct in the right direction what needs to be done.

I think for the color structure like this:

struct Cvet{ red = , green=, blue= }

or somehow differently need?

    1 answer 1

    In words? Yes, a structure or a class (if they want the functionality of getting components, it means a class with encapsulated data ...) of type

     class Color { public: Color() = default; Color(int r, int g, int b):r(r),g(g),b(b){} ~Color() = default; void setRed(int R) { r = R; }; void setGreen(int G) ...; void setBlue(int B) ...; int getRed() const { return r; }; int getGreen() const ...; int getBlue() const ...; void brightness(int value, int color = 0); // Для всех цветов или для конкретного private: int r = 0, g = 0, b = 0; }; 

    Further simply paint functions. You need to tinker with brightness to decide how it should work — to set absolute values ​​or a relative increase, for example. Perhaps add a field of maximum color value - of type static const int maxValue = ...;

    Well, with the circles - exactly the same:

     class Circle { ... private: Color color; int x, y; }; 

    Well, think about what features are needed ...

    • And what's the point of writing a class with private fields, and then doing a trivial setter and getter. Isn't it better to write a structure right away? Especially since the data is "flat"? - pavel
    • @pavel I wrote - " if they want the functionality of getting components, it means a class with encapsulated data ." Almost no meaning, but in educational tasks the didactic meaning is often more important than the common sense. - Harry
    • @pavel, meaning in encapsulation. If suddenly the developer knocks on the head to store the color as a #RRGGBB string, or as a single whole number, or HSV, or CMYK, then only the implementation of setters / getters will change, not tons of code that uses the open variables r , g and b - yrHeTaTeJlb
    • @yrHeTaTeJlb is debatable ... IMHO it is better to change everything than to try to drive 4 variables to 3 or worse - not to notice that the color scheme is different and calculate something is not right. Better already 2 structures and a converter then. - pavel
    • @pavel, so who says that the scheme is different? RGB scheme. Setters / getters operate with a red, green and blue component. And what's inside no one should know. Today I have three inta, tomorrow an array of ints, after tomorrow an array of charms. All these conversions should not affect another code - yrHeTaTeJlb