Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include "Color.h"
- #include "TransColor.h"
- using namespace std;
- int main() {
- setlocale(LC_ALL, "RUS"); //переводим консоль в русскую кодировку
- Color ultraBlue = Color(0, 0, 255);
- Color userColor;
- cin >> userColor;
- if (userColor == ultraBlue)
- cout << "Это ультра голубой цвет!!!" << endl;
- else
- cout << "Это не ультра голубой цвет(((" << endl;
- system("pause");
- }
- #include <iostream>
- using namespace std;
- class Color {
- protected:
- int R, G, B;
- bool correct_color_flag(int x) {
- return x >= 0 && x <= 255;
- }
- public:
- Color() {
- R = 0;
- G = 0;
- B = 0;
- }
- Color(int r, int g, int b) { //конструктор инициализации
- if (!correct_color_flag(r)) throw "incorrect RED component value";
- if (!correct_color_flag(g)) throw "incorrect GREEN component value";
- if (!correct_color_flag(b)) throw "incorrect BLUE component value";
- R = r;
- G = g;
- B = b;
- }
- Color setRedColor(int x) {
- if (correct_color_flag(x))
- R = x;
- else
- throw "incorrect RED component value";
- return *this;
- }
- Color setGreenColor(int x) {
- if (correct_color_flag(x))
- G = x;
- else
- throw "incorrect GREEN component value";
- return *this;
- }
- Color setBlueColor(int x) {
- if (correct_color_flag(x))
- B = x;
- else
- throw "incorrect BLUE component value";
- return *this;
- }
- Color(const Color& tmp) { //конструктор копирования
- R = tmp.R;
- G = tmp.G;
- B = tmp.B;
- }
- ~Color() { //деструктор
- }
- Color& operator = (Color obj) {
- R = obj.R;
- G = obj.G;
- B = obj.B;
- return *this;
- }
- bool operator > (Color obj) {
- return R + G + B > obj.R + obj.G + obj.B;
- }
- bool operator < (Color obj) {
- return R + G + B < obj.R + obj.G + obj.B;
- }
- bool operator != (Color obj) {
- return (R != obj.R || G != obj.G || B != obj.B);
- }
- bool operator >= (Color obj) {
- return R + G + B >= obj.R + obj.G + obj.B;
- }
- bool operator <= (Color obj) {
- return R + G + B <= obj.R + obj.G + obj.B;
- }
- bool operator == (Color obj) {
- return (R == obj.R && G == obj.G && B == obj.B);
- }
- friend istream& operator >> (istream& in, Color& obj) {
- cout << "Enter values for the color components" << endl;
- int x;
- cout << "Enter RED component ";
- in >> x;
- obj.setRedColor(x);
- cout << endl << "Enter GREEN component ";
- in >> x;
- obj.setGreenColor(x);
- cout << endl << "Enter BLUE component ";
- in >> x;
- obj.setBlueColor(x);
- return in;
- }
- friend ostream& operator << (ostream& out, Color& obj) {
- out << "Color components" << endl;
- out << "RED component " << obj.R << endl << "GREEN component " << obj.G << endl << "BLUE component " << obj.B << endl;
- return out;
- }
- friend Color& operator+ (Color c1, Color c2) {
- int R = c1.R + c2.R;
- int G = c1.G + c2.G;
- int B = c1.B + c2.B;
- if (R > 255) R = 255;
- if (G > 255) G = 255;
- if (B > 255) B = 255;
- Color c3(R, G, B);
- return c3;
- }
- friend Color& operator- (Color c1, Color c2) {
- int R = c1.R - c2.R;
- int G = c1.G - c2.G;
- int B = c1.B - c2.B;
- if (R < 0) R = 0;
- if (G < 0) G = 0;
- if (B < 0) B = 0;
- Color c3(R, G, B);
- return c3;
- }
- };
- #include <iostream>
- #include "Color.h"
- using namespace std;
- class TransColor : Color {
- public:
- enum ColorType
- {
- RGB,
- CMY
- };
- /*TransColor tc = TransColor(100, 100, 100, TransColor::CMY);*/
- TransColor(int fComponent, int sComponent, int tComponent, ColorType type) : Color(fComponent, sComponent, tComponent) {
- curentType = type;
- }
- TransColor(const TransColor& tc) : Color(tc.R, tc.G, tc.B) {
- curentType = tc.curentType;
- }
- ~TransColor() { Color::~Color(); }
- TransColor() { Color::Color(); }
- TransColor toColorType(ColorType newType) {
- if (curentType != newType) {
- R = 255 - R;
- G = 255 - G;
- B = 255 - B;
- curentType = newType;
- }
- return *this;
- }
- ColorType getColorType() {
- return curentType;
- }
- private:
- ColorType curentType;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement