Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- using namespace std;
- int RGB_na_CMYK(float R, float G, float B){
- float _R, _G, _B, C, M, Y, K, max;
- if (R <= 255 && R >= 0 && G <= 255 && G >= 0 && B <= 255 && B >= 0){
- _R = R / 255;
- _G = G / 255;
- _B = B / 255;
- max = _R;
- if (_G > max) max = _G;
- if (_B > max) max = _B;
- K = 1 - max;
- C = (1 - _R - K) / (1 - K);
- M = (1 - _G - K) / (1 - K);
- Y = (1 - _B - K) / (1 - K);
- cout << "RGB(" << R << ", " << G << ", " << B << ")->CMYK(" << C << ", " << M << ", " << Y << ", " << K << ")" << endl << endl;
- return 0;
- }
- else return -1;
- }
- int CMYK_na_RGB(float C, float M, float Y, float K){
- float R, G, B;
- if (C >= 0 && M >= 0 && Y >= 0 && K >= 0 && C <= 1 && M <= 1 && Y <= 1 && K <= 1){
- R = 255 * (1 - C) * (1 - K);
- G = 255 * (1 - M) * (1 - K);
- B = 255 * (1 - Y) * (1 - K);
- cout << "CMYK(" << C << ", " << M << ", " << Y << ", " << K << ")->RGB(" << R << ", " << G << ", " << B << ")" << endl << endl;
- return 0;
- }
- else return -1;
- }
- int RGB_na_HSL(float R, float G, float B){
- float _R, _G, _B, H, S, L, max, min, delta;
- int i; // co jest maxem
- if (R <= 255 && R >= 0 && G <= 255 && G >= 0 && B <= 255 && B >= 0){
- _R = R / 255;
- _G = G / 255;
- _B = B / 255;
- max = _R; i = 1;
- if (_G > max) {max = _G; i = 2;}
- if (_B > max) {max = _B; i = 3;}
- min = _R;
- if (_G < min) min = _G;
- if (_B < min) min = _B;
- delta = max - min;
- // OBLICZANIE L
- L = (max + min) / 2;
- // OBLICZANIE S
- if (delta == 0) S = 0;
- else S = delta / (1 - abs(2 * L - 1));
- // OBLICZANIE H
- switch (i){
- case 1:
- H = (_G - _B) / delta;
- H = fmodf(H, 6);
- H *= 60;
- break;
- case 2:
- H = 60 * (((_B - _R) / delta) + 2);
- break;
- case 3:
- H = 60 * (((_R - _G) / delta) + 4);
- break;
- }
- // Zaokrąglenie
- //cout.setf(ios::fixed);
- //cout.precision(1);
- // Poprawki procentowe
- S *= 100;
- L *= 100;
- cout << "RGB(" << R << ", " << G << ", " << B << ")->HSL(" << H << "st, " << S << "%, " << L << "%)" << endl << endl;
- return 0;
- }
- else return -1;
- }
- int HSL_na_RGB(float H, float S, float L){
- float C, X, m, _R, _G, _B, R, G, B;
- if (H >= 0 && H < 360 && S >= 0 && S <= 100 && L>= 0 && L <= 100){
- float _L = L / 100;
- float _S = S / 100;
- C = (1 - abs(2 * _L - 1)) * _S;
- X = C * (1 - abs(fmodf((H / 60), 2) - 1));
- m = _L - C / 2;
- if (H <= 60) { _R = C; _G = X; _B = 0; }
- if (H >= 60 && H <= 120) { _R = X; _G = C; _B = 0; }
- if (H >= 120 && H <= 180) { _R = 0; _G = C; _B = X; }
- if (H >= 180 && H <= 240) { _R = 0; _G = X; _B = C; }
- if (H >= 240 && H <= 300) { _R = X; _G = 0; _B = C; }
- if (H >= 300 && H <= 360) { _R = C; _G = 0; _B = X; }
- R = _R + m;
- G = _G + m;
- B = _B + m;
- cout << "HSL(" << H << "st, " << S << "%, " << L << "%)->" << "RGB(" << R << ", " << G << ", " << B << ")" << endl << endl;
- return 0;
- }
- else return -1;
- }
- int RGB_na_HSV(float R, float G, float B){
- float _R, _G, _B, H, S, V, max, min, delta;
- int i; // co jest maxem
- if (R <= 255 && R >= 0 && G <= 255 && G >= 0 && B <= 255 && B >= 0){
- _R = R / 255;
- _G = G / 255;
- _B = B / 255;
- max = _R; i = 1;
- if (_G > max) { max = _G; i = 2; }
- if (_B > max) { max = _B; i = 3; }
- min = _R;
- if (_G < min) min = _G;
- if (_B < min) min = _B;
- delta = max - min;
- // OBLICZANIE V
- V = max;
- // OBLICZANIE S
- if (delta == 0) S = 0;
- else S = delta / max;
- // OBLICZANIE H
- switch (i){
- case 1:
- H = (_G - _B) / delta;
- H = fmodf(H, 6);
- H *= 60;
- break;
- case 2:
- H = 60 * (((_B - _R) / delta) + 2);
- break;
- case 3:
- H = 60 * (((_R - _G) / delta) + 4);
- break;
- }
- // Poprawki procentowe
- S *= 100;
- V *= 100;
- cout << "RGB(" << R << ", " << G << ", " << B << ")->HSV(" << H << "st, " << S << "%, " << V << "%)" << endl << endl;
- return 0;
- }
- else return -1;
- }
- int main(){
- RGB_na_CMYK(255, 100, 255);
- CMYK_na_RGB(1, 1, 0, 0);
- RGB_na_HSL(255, 100, 100);
- HSL_na_RGB(359, 50, 50);
- RGB_na_HSV(255, 100, 100);
- system("PAUSE");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement