acm GBR HLV转换
#include <stdio.h>
#include <string>
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
void RGB2HSV(double R, double G, double B, double &H, double &S, double &V)
{
// r,g,b values are from 0 to 1
// h = [0,360], s = [0,1], v = [0,1]
// if s == 0, then h = -1 (undefined)
double min, max, delta,tmp;
tmp = R>G?G:R;
min = tmp>B?B:tmp;
tmp = R>G?R:G;
max = tmp>B?tmp:B;
R/=255; G/=255; B/=255;
min/=255;
max /= 255;
V = max; // v
delta = max - min;
if( max != 0 )
S = delta / max; // s
else
{
// r = g = b = 0 // s = 0, v is undefined
S = 0;
H = 0;
return;
}
if (delta == 0){
H = 0;
return;
}
else if(R == max){
if (G >= B)
H = (G - B) / delta; // between yellow & magenta
else
H = (G - B) / delta + 6.0;
}
else if( G == max )
H = 2.0 + ( B - R ) / delta; // between cyan & yellow
else if (B == max)
H = 4.0 + ( R - G ) / delta; // between magenta & cyan
H *= 60.0; // degrees
}
void RGB2HSL(double R, double G, double B, double &H, double &S, double &L)
{
// r,g,b values are from 0 to 1
// h = [0,360], s = [0,1], v = [0,1]
// if s == 0, then h = -1 (undefined)
double min, max, delta,tmp;
tmp = R>G?G:R;
min = tmp>B?B:tmp;
tmp = R>G?R:G;
max = tmp>B?tmp:B;
R/=255; G/=255; B/=255;
min/=255;
max /= 255;
L = (max+min)/2; // v
delta = max - min;
if(L==0 || delta == 0)
S = 0;
else if(L <= 0.5 && L >0)
S =(max-min)/(2*L);
else
S =(max-min)/(2-2*L);
if (delta == 0){
H = 0;
return;
}
else if(R == max){
if (G >= B)
H = (G - B) / delta; // between yellow & magenta
else
H = (G - B) / delta + 6.0;
}
else if( G == max )
H = 2.0 + ( B - R ) / delta; // between cyan & yellow
else if (B == max)
H = 4.0 + ( R - G ) / delta; // between magenta & cyan
H *= 60.0; // degrees
}
//函数把val转到r、g、b里面
void HSV2RGB(double H, double S, double V, double &R, double &G, double &B)
{
//hsv to rgb
S/=100;
V/=100;
double C = V *S;
H /= 60;
double tp;
if(int(H) % 2 - 1 > 0) tp =int(H) % 2 - 1;
else
tp = 1-int(H) % 2 ;
double X = C*(1- tp);
double m = V-C;
if(H<1)
{
R = (C+m+ 0.005)*255;
G = (X+ m+ 0.005)*255;
B = (0 +m+ 0.005)*255;
return;
}
if(H<2)
{
R = (X +m+ 0.005)*255;
G = (C +m+ 0.005)*255;
B = (0 +m+ 0.005)*255;
return;
}
if(H<3)
{
R = (0 + m+ 0.005)*255;
G = (C + m+ 0.005)*255;
B = (X + m+ 0.005)*255;
return;
}
if(H<4)
{
R = (0 + m+ 0.005)*255;
G = (X + m+ 0.005)*255;
B = (C + m+ 0.005)*255;
return;
}
if(H<5)
{
R = (X + m+ 0.005)*255;
G = (0 + m+ 0.005)*255;
B = (C + m+ 0.005)*255;
return;
}
if(H<6)
{
R = (C + m + 0.005)*255;
G = (0 + m + 0.005)*255;
B = (X + m + 0.005)*255;
return;
}
}
void HSL2RGB(double H, double S, double L, double &R, double &G, double &B)
{
L/=100;
S/=100;
double tp;
if(2*L-1 >= 0) tp = 2*L-1;
else
tp = 1-2*L;
double C =(1-tp)*S;
H /= 60;
d
if(int(H) % 2 - 1 > 0) tp =int(H) % 2 - 1;
else
tp = 1-int(H) % 2 ;
double X = C*(1- tp);
double m = L - C*0.5;
if(H<1)
{
R = (C+m+ 0.005)*255;
G = (X+ m+ 0.005)*255;
B = (0 +m+ 0.005)*255;
return;
}
if(H<2)
{
R = (X +m+ 0.005)*255;
G = (C +m+ 0.005)*255;
B = (0 +m+ 0.005)*255;
return;
}
if(H<3)
{
R = (0 + m+ 0.005)*255;
G = (C + m+ 0.005)*255;
B = (X + m+ 0.005)*255;
return;
}
if(H<4)
{
R = (0 + m+ 0.005)*255;
G = (X + m+ 0.005)*255;
B = (C + m+ 0.005)*255;
return;
}
if(H<5)
{
R = (X + m+ 0.005)*255;
G = (0 + m+ 0.005)*255;
B = (C + m+ 0.005)*255;
return;
}
if(H<6)
{
R = (C + m + 0.005)*255;
G = (0 + m + 0.005)*255;
B = (X + m + 0.005)*255;
return;
}
}
int main()
{
freopen("read.txt", "r", stdin);
char str1[100], str2[100];
while(cin >> str1)
{
double a, b, c;
double tp1, tp2, tp3;
//cin >> str2 >> a >> b >>c;
if(strcmp(str1, "RGB") == 0)
{
scanf("%s", str2);
if(strcmp(str2, "RGB") == 0)
{
scanf("%lf %lf %lf", &a, &b, &c);
cout << "RGB" << ' ' <<int(a) << ' ' << int(b) << ' ' << int(c) <<endl;
}
else if(strcmp(str2, "HSL") == 0)
{
scanf("%lf %lf%% %lf%%", &a, &b, &c);
double r, g, f;
HSL2RGB(a, b, c, r, g, f);
cout << "RGB" << ' ' << int(r+0.5) << ' ' << int(g+0.5) << ' ' << int(f+0.5) <<endl;
}
else
{
scanf("%lf %lf%% %lf%%", &a, &b, &c);
double r, g, f;
HSV2RGB(a, b, c, r,g, f);
cout << "RGB" << ' ' << int(r+0.5) << ' ' << int(g+0.5) << ' ' << int(f+0.5) <<endl;
}
}
else if(strcmp(str1, "HSL") == 0)
{
scanf("%s", str2);
if(strcmp(str2, "HSL") == 0)
{
scanf("%lf %lf%% %lf%%", &a, &b, &c);
cout << "HSL" << ' '<< int(a) << ' ' << int(b) << '%' << ' '<<int(c) << '%' <<endl;
}
else if(strcmp(str2, "RGB") == 0)
{
scanf("%lf %lf %lf", &a, &b, &c);
RGB2HSL(a, b, c, tp1, tp2, tp3);
cout << "HSL" << ' '<< int(tp1+0.5) << ' '<< int((tp2+0.005)*100) << '%' << ' ' << int((tp3+0.005)*100) << '%' <<endl;
}
else
{
scanf("%lf %lf%% %lf%%", &a, &b, &c);
double r, g, f;
HSV2RGB(a, b, c, r, g, f);
RGB2HSL(r, g, f, tp1, tp2, tp3);
cout << "HSL" << ' '<< int(a) << ' '<< int((tp2+0.005)*100) << '%' << ' ' << int((tp3+0.005)*100) << '%' <<endl;
}
}
else
{
scanf("%s", str2);
if(strcmp(str2, "RGB") == 0)
{
scanf("%lf %lf %lf", &a, &b, &c);
RGB2HSV(a, b, c, tp1, tp2, tp3);
cout << "HSV" << ' ' << int(tp1+0.5) << ' '<< int((tp2+0.005)*100) << '%' << ' ' << int((tp3+0.005)*100) << '%' <<endl;
}
else if(strcmp(str2, "HSL") == 0)
{
scanf("%lf %lf%% %lf%%", &a, &b, &c);
double r, g, f;
HSL2RGB(a, b, c, r, g, f);
RGB2HSV(r, g, f, tp1, tp2, tp3);
cout << "HSV" << ' '<< int(a) << ' '<< int((tp2+0.005)*100) << '%' << ' ' << int((tp3+0.005)*100) << '%' <<endl;
}
else
{
scanf("%lf %lf%% %lf%%", &a, &b, &c);
cout << "HSV" << ' '<< int(a) << ' ' << int(b) << '%' << ' '<<int(c) << '%' <<endl;
}
}
}
return 0;
}
附件列表