原题直通车:Color Representation Conversion
分析:直接模拟!可惜当时没调试出来……
代码:
#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
#define eps 1e-6
using namespace std;
char cmd[5], s[5], ch;
bool zero(double x) {
if(fabs(x)<eps) return true;
return false;
}
void RGB_HSV(double &h, double &s, double &v) {
double r=h, g=s, b=v;
double R=r/255.0, G=g/255.0, B=b/255.0, S=0, V=0;
double mx=max(R, max(G,B)), mi=min(R, min(G,B));
if(zero(mx-mi)) h=0;
else if(mx==R) {
if(G>=B) h=60*(G-B)/(mx-mi);
else h=60*(G-B)/(mx-mi)+360;
}
else if(mx==G) h=60.0*(B-R)/(mx-mi)+120;
else if(mx==B) h=60.0*(R-G)/(mx-mi)+240;
if(fabs(mx)>eps) S=1.0-mi/mx;
V=mx;
s=S*100.0, v=V*100.0;
}
void RGB_HSL(double &h, double &s, double& l) {
double r=h, g=s, b=l;
double R=r/255.0, G=g/255.0, B=b/255.0, S=0, L=0;
double mx=max(R,max(G,B)), mi=min(R,min(G,B));
if(zero(mx-mi)) h=0;
else if(mx==R) {
if(G>=B) h=60*(G-B)/(mx-mi);
else h=60*(G-B)/(mx-mi)+360.0;
}
else if(mx==G) h=60.0*(B-R)/(mx-mi)+120;
else if(mx==B) h=60.0*(R-G)/(mx-mi)+240;
L=(mx+mi)/2.0;
if(zero(L)||zero(mx-mi)) S=0;
else if(L>0&&L<0.5) S=(mx-mi)/(2*L);
else S=(mx-mi)/(2-2*L);
l=L*100.0, s=S*100.0;
}
void HSV_RGB(double &r, double &g, double &b) {
double h=r, s=g, v=b;
double R=0, G=0, B=0;
double S=s/100.0, V=v/100.0;
double C=S*V;
double H=h/60.0;
double X=C*(1.0-fabs((H-(int)H/2*2)-1.0));
if(H>=5.0) R=C, G=0, B=X;
else if(H>=4.0) R=X, G=0, B=C;
else if(H>=3.0) R=0, G=X, B=C;
else if(H>=2.0) R=0, G=C, B=X;
else if(H>=1.0) R=X, G=C, B=0;
else if(H>=0.0) R=C, G=X, B=0;
else R=0, G=0, B=0;
double m=V-C;
r=(m+R)*255.0, g=255.0*(G+m), b=(B+m)*255;
}
void HSL_RGB(double &r, double &g, double &b) {
double h=r, s=g, l=b;
double R=0, G=0, B=0, S=s/100.0, L=l/100.0;
double C=(1.0-fabs(2.0*L-1.0))*S;
double H=h/60.0;
double X=C*(1.0-fabs((H-(int)H/2*2)-1.0));
if(H>=5) R=C, G=0, B=X;
else if(H>=4) R=X, G=0, B=C;
else if(H>=3) R=0, G=X, B=C;
else if(H>=2) R=0, G=C, B=X;
else if(H>=1) R=X, G=C, B=0;
else if(H>=0) R=C, G=X, B=0;
else R=0, G=0, B=0;
double m=L-C/2.0;
r=(m+R)*255.0, g=(G+m)*255.0, b=(B+m)*255.0;
}
int main() {
while(~scanf("%s%s",cmd, s)) {
if(strcmp(s, "HSL")==0) {
double h, s, l;
scanf("%lf%lf%c%c%lf%c",&h, &s, &ch, &ch, &l, &ch);
getchar();
if(strcmp(cmd,"HSL")==0)
printf("HSL %d %d%% %d%%\n",(int)(h+0.5),(int)(s+0.5),(int)(l+0.5));
else {
HSL_RGB(h, s, l);
if(strcmp(cmd, "RGB")==0)
printf("RGB %d %d %d\n",(int)(h+0.5),(int)(s+0.5),(int)(l+0.5));
else {
RGB_HSV(h, s, l);
printf("HSV %d %d%% %d%%\n",(int)(h+0.5),(int)(s+0.5),(int)(l+0.5));
}
}
} else if(strcmp(s, "HSV")==0) {
double h, s, v;
scanf("%lf%lf%c%c%lf%c",&h, &s, &ch, &ch, &v, &ch);
getchar();
if(strcmp(cmd, "HSV")==0)
printf("HSV %d %d%% %d%%\n",(int)(h+0.5),(int)(s+0.5),(int)(v+0.5));
else {
HSV_RGB(h, s, v);
if(strcmp(cmd,"RGB")==0)
printf("RGB %d %d %d\n",(int)(h+0.5),(int)(s+0.5),(int)(v+0.5));
else {
RGB_HSL(h, s, v);
printf("HSL %d %d%% %d%%\n",(int)(h+0.5),(int)(s+0.5),(int)(v+0.5));
}
}
}
else {
double r, g, b;
scanf("%lf%lf%lf",&r,&g,&b);
if(strcmp(cmd, "RGB")==0)
printf("RGB %d %d %d\n",(int)(r+0.5),(int)(g+0.5),(int)(b+0.5));
else if(strcmp(cmd, "HSV")==0) {
RGB_HSV(r, g, b);
printf("HSV %d %d%% %d%%\n",(int)(r+0.5),(int)(g+0.5),(int)(b+0.5));
}
else {
RGB_HSL(r,g,b);
printf("HSL %d %d%% %d%%\n",(int)(r+0.5),(int)(g+0.5),(int)(b+0.5));
}
}
}
return 0;
}
/*
HSL
RGB 174 82 144
HSV
HSL 62 80% 83%
RGB
HSV 324 56% 71%
*/