
КР1 / КР1 ООП (Вариант 5)
.docx#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
class Color {
float red;
float yellow;
float blue;
public:
Color();
Color(float red, float yellow, float blue);
float getRed(){ return red; }
float getYellow(){ return yellow; }
float getBlue(){ return blue; }
void setRed(float red);
void setYellow(float yellow);
void setBlue(float blue);
void setColor(float red, float yellow, float blue);
void print();
void printrgb();
float sum();
};
//Конструктор по умолчанию
Color::Color(){
red = 0;
yellow = 0;
blue = 0;
}
//Конструктор для инициализации компоненты цвета заданными значениями
Color::Color(float red, float yellow, float blue){
this->red = red;
this->yellow = yellow;
this->blue = blue;
}
//Сеттеры для установки значений компонент цвета
void Color::setRed(float red) {
this->red = red;
}
void Color::setYellow(float yellow) {
this->yellow = yellow;
}
void Color::setBlue(float blue) {
this->blue = blue;
}
//Цвет из трёх компонент
void Color::setColor(float red, float yellow, float blue) {
setRed(red);
setYellow(yellow);
setBlue(blue);
}
//Вывод значений компонент цвета для RYB
void Color::print(){
float redf, yellowf, bluef;
redf = red * 255;
yellowf = yellow * 255;
bluef = blue * 255;
cout << "(" << (int) redf << ", " << (int) yellowf << ", " << (int) bluef << ")";
}
//Вывод значений компонент цвета для RYB
void Color::printrgb(){
cout << "(" << red << ", " << yellow << ", " << blue << ")";
}
//Сумма компонент цвета
float Color::sum(){
return red + yellow + blue;
}
//Сортировка по возрастанию суммы компонентов цвета
void sort(Color arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j].sum() > arr[j + 1].sum()) {
swap(arr[j], arr[j + 1]);
}
}
}
}
//Перевод из RYB в RGB
void RYBtoRGB(float R, float Y, float B, float& red, float& green, float& blue) {
float R1 = R * (1 - B) + B;
float G1 = Y * (1 - B) + B;
float B1 = B;
float X = R1 * (1 - Y);
float Y1 = Y * (1 - B);
float Z = B1 * (1 - R);
red = 1.0f * R1 + (-1.0f * X + -1.0f * Y1 + 1.0f * Z);
green = 1.0f * G1 + (1.0f * X + -1.0f * Y1 + -1.0f * Z);
blue = 1.0f * B1 + (1.0f * -X + 1.0f * Y1 + -1.0f * Z);
red = max(0.0f, min(1.0f, red))*255;
green = max(0.0f, min(1.0f, green))*255;
blue = max(0.0f, min(1.0f, blue))*255;
}
int main() {
setlocale(LC_ALL, "Russian");
system("chcp 1251");
srand(time(NULL));
const int n = 5;
Color arr[n];
//Случайные цвета
for (int i = 0; i < n; i++) {
float red = (float)rand() / RAND_MAX * 1;
float yellow = (float)rand() / RAND_MAX * 1;
float blue = (float)rand() / RAND_MAX * 1;
arr[i].setColor(red, yellow, blue);
}
//Вывод получившихся цветов
cout << "RYB до сортировки: ";
for (int i = 0; i < n; i++) {
arr[i].print();
cout << " ";
}
cout << "\n";
cout << "\n";
//Сортировка
sort(arr, n);
cout << "RYB после сортировки: ";
for (int i = 0; i < n; i++) {
arr[i].print();
cout << " ";
}
cout << "\n";
cout << "\n";
//Первеод из RYB в RGB
for (int i = 0; i < n; i++) {
float red, green, blue;
RYBtoRGB(arr[i].getRed(), arr[i].getYellow(), arr[i].getBlue(), red, green, blue);
arr[i].setColor((int) red, (int) green, (int) blue);
}
cout << "Цвета в RGB: ";
for (int i = 0; i < n; i++) {
arr[i].printrgb();
cout << " ";
}
return 0;
}