So far, there are many color models in different area. For screen display, the most popular model is RGB color model. A color in the RGB color model is described indicating how much of each of the red, green, and blue is included. So one can easily determined a color by an RGB triplet (r, g, b). But there are other representation of the points in RGB color model,HSL and HSV are the two most popular representations among them and widely used in color pickers and in image editing software. They also use a triple (h,s,l) or (h,s,v) to determine a color but each component are with different meanings. each channel in HSL stands for hue, saturation, and lightness and in HSV stands for hue, saturation, and value. Note that while "hue" in HSL and HSV refers to the same attribute, their definitions of "saturation" differ dramatically.
For RGB triplet, we use digital 8-bit per channel notation, so the r,g,b can vary from 0 to 255. If all the components are at zero the result is black; if all are at maximum, the result is the brightest representable white.
For HSV and HSL, the hue channel is in unit of degrees, its value vary from 0 to 360(exclusive), and the saturation, lightness and value channel use percentage notation and their value vary from 0% to 100%.
For more detail about the RGB model and these representations, you can refer to HERE .
The problem here is ask you to implement a color representation conversion procedure to convert the representation between RGB,HSL and HSV following the methods below. Or you can find more detail of the converting method in HERE .
Converting HSV to RGB
Given a color with hue H ∈ [0°, 360°), saturation SHSV ∈ [0, 1], and value V ∈ [0, 1], we first find chroma:
Then we can find a point (R1, G1, B1) along the bottom three faces of the RGB cube, with the same hue and chroma as our color (using the intermediate value X for the second largest component of this color):
Finally, we can find R, G, and B by adding the same amount to each component, to match value:
Converting HSL to RGB
Given an HSL color with hue H ∈ [0°, 360°), saturation SHSL ∈ [0, 1], and lightness L ∈ [0, 1], we can use the same strategy. First, we find chroma:
Then we can, again, find a point (R1, G1, B1) along the bottom three faces of the RGB cube, with the same hue and chroma as our color (using the intermediate value X for the second largest component of this color):
Finally, we can find R, G, and B by adding the same amount to each component, to match lightness:
Convert RGB to HSL and HSV
First unify (r, g, b) into a number between 0 and 1. Let max equals to the maximum value in r, g and b. Let min equals to the minimum value in r, g and b. The HSL is with hue h ∈ [0°, 360°), saturation s ∈ [0, 1], and lightness l ∈ [0, 1]
When max = min, h is defined as 0.
HSL and HSV have the same definition of hue. The s and v value in HSV is defined as follows:
There are multiple cases in input. The first line of each case is name of the target representation which you need to convert to. The second line is the representation of the color. It could one of the RGB representation "RGB r g b"(0 ≤ r,g,b ≤ 255), or HSL representation "HSL h s% l%"(0 ≤ h < 360; 0 ≤ s, l ≤ 100), or HSV representation "HSV h s% v%"(0 ≤ h < 360; 0 ≤ s, v ≤ 100). Please note that all numeric value is integer.
For each case, output the color representation in format of target representation. Each numeric value should round to nearest integer. See sample for more information.
HSL RGB 174 82 144 HSV HSL 62 80% 83% RGB HSV 324 56% 71%
HSL 320 36% 50% HSV 62 28% 97% RGB 181 80 140
#include<iostream> #include<cstring> #include<cmath> #include<cstring> #include<cstdio> #include<algorithm> #include<map> using namespace std; int zr,zg,zb; double r,g,b; int zh1,zh2; int zs1,zs2,zl,zv; char tar[10]; char fro[10]; void RGBtoHSL() { double h,s,l; double ma,mi; ma=max(max(r,g),b); mi=min(min(r,g),b); if(ma==mi) h=0.0; else if(ma==r&&g>=b) h=60.0*(g-b)/(ma-mi); else if(ma==r&&g<b) h=60.0*(g-b)/(ma-mi)+360.0; else if(ma==g) h=60.0*(b-r)/(ma-mi)+120.0; else if(ma==b) h=60.0*(r-g)/(ma-mi)+240.0; l=(ma+mi)/2.0; if(l==0.0||ma==mi) s=0.0; else if(l>0.0&&l<=0.5) s=(ma-mi)/(2.0*l); else if(l>0.5) s=(ma-mi)/(2.0-2.0*l); zh1=(int)(h+0.5); zl=(int)(l*100.0+0.5); zs1=(int)(s*100.0+0.5); } void RGBtoHSV() { double h,s,v; double ma,mi; ma=max(max(r,g),b); mi=min(min(r,g),b); if(ma==mi) h=0.0; else if(ma==r&&g>=b) h=60.0*(g-b)/(ma-mi); else if(ma==r&&g<b) h=60.0*(g-b)/(ma-mi)+360.0; else if(ma==g) h=60.0*(b-r)/(ma-mi)+120.0; else if(ma==b) h=60.0*(r-g)/(ma-mi)+240.0; v=ma; if(ma==0.0) s=0.0; else s=1.0-mi/ma; zh2=(int)(h+0.5); zs2=(int)(s*100.0+0.5); zv=(int)(v*100.0+0.5); } void HSVtoRGB() { double r1,g1,b1,m; double c,x,h1,hh; c=zv*0.01*zs2*0.01; h1=zh2*1.0/60; hh=h1; while(hh>=2) hh-=2; x=c*(1-fabs(hh-1)); if(h1>=0&&h1<1) r1=c,g1=x,b1=0; else if(h1<2) r1=x,g1=c,b1=0; else if(h1<3) r1=0,g1=c,b1=x; else if(h1<4) r1=0,g1=x,b1=c; else if(h1<5) r1=x,g1=0,b1=c; else if(h1<6) r1=c,g1=0,b1=x; m=zv*0.01-c; r=(r1+m); g=(g1+m); b=(b1+m); zr=int(r*255+0.5); zg=int(g*255+0.5); zb=int(b*255+0.5); } void HSLtoRGB() { double c,h1,hh,x,r1,g1,b1,m; c=(1-fabs(2*zl*0.01-1.0))*zs1*0.01; h1=zh1*1.0/60; hh=h1; while(hh>=2) hh-=2; x=c*(1-fabs(hh-1)); if(h1>=0&&h1<1) r1=c,g1=x,b1=0; else if(h1<2) r1=x,g1=c,b1=0; else if(h1<3) r1=0,g1=c,b1=x; else if(h1<4) r1=0,g1=x,b1=c; else if(h1<5) r1=x,g1=0,b1=c; else if(h1<6) r1=c,g1=0,b1=x; m=0.01*zl-0.5*c; r=(r1+m); g=(g1+m); b=(b1+m); zr=int(r*255+0.5); zg=int(g*255+0.5); zb=int(b*255+0.5); } int main() { while(~scanf("%s%s",tar,fro)) { if(strcmp(fro,"RGB")==0) { scanf("%d%d%d",&zr,&zg,&zb); if(strcmp(tar,"RGB")==0) { printf("%s %d %d %d\n",tar,zr,zg,zb); } else if(strcmp(tar,"HSL")==0) { r=zr/255.0,g=zg/255.0,b=zb/255.0; RGBtoHSL(); printf("%s %d %d%% %d%%\n",tar,zh1,zs1,zl); } else if(strcmp(tar,"HSV")==0) { r=zr/255.0,g=zg/255.0,b=zb/255.0; RGBtoHSV(); printf("%s %d %d%% %d%%\n",tar,zh2,zs2,zv); } } else if(strcmp(fro,"HSL")==0) { char tt[5]; scanf("%d",&zh1); scanf("%s",tt); if(strlen(tt)==2) zs1=tt[0]-'0'; else if(strlen(tt)==3) zs1=(tt[0]-'0')*10+(tt[1]-'0'); else zs1=(tt[0]-'0')*100+(tt[1]-'0')*10+(tt[2]-'0'); scanf("%s",tt); if(strlen(tt)==2) zl=tt[0]-'0'; else if(strlen(tt)==3) zl=(tt[0]-'0')*10+(tt[1]-'0'); else zl=(tt[0]-'0')*100+(tt[1]-'0')*10+(tt[2]-'0'); //cout<<zh1<<" "<<zs1<<" "<<zl<<endl; if(strcmp(tar,"HSL")==0) { printf("%s %d %d%% %d%%\n",tar,zh1,zs1,zl); } else if(strcmp(tar,"RGB")==0) { HSLtoRGB(); printf("%s %d %d %d\n",tar,zr,zg,zb); } else if(strcmp(tar,"HSV")==0) //如果hsl转换为hsv的话,先转换成rgb { HSLtoRGB(); //printf("RGB %d %d %d\n",zr,zg,zb); RGBtoHSV(); printf("%s %d %d%% %d%%\n",tar,zh2,zs2,zv); } } else if(strcmp(fro,"HSV")==0) { char tt[5]; scanf("%d",&zh2); scanf("%s",tt); if(strlen(tt)==2) zs2=tt[0]-'0'; else if(strlen(tt)==3) zs2=(tt[0]-'0')*10+(tt[1]-'0'); else zs2=(tt[0]-'0')*100+(tt[1]-'0')*10+(tt[2]-'0'); scanf("%s",tt); if(strlen(tt)==2) zv=tt[0]-'0'; else if(strlen(tt)==3) zv=(tt[0]-'0')*10+(tt[1]-'0'); else zv=(tt[0]-'0')*100+(tt[1]-'0')*10+(tt[2]-'0'); if(strcmp(tar,"HSV")==0) { printf("%s %d %d%% %d%%\n",tar,zh2,zs2,zv); } else if(strcmp(tar,"RGB")==0) { HSVtoRGB(); printf("%s %d %d %d\n",tar,zr,zg,zb); } else if(strcmp(tar,"HSL")==0) { HSVtoRGB(); RGBtoHSL(); printf("%s %d %d%% %d%%\n",tar,zh1,zs1,zl); } } } return 0; } /* HSL RGB 174 82 144 HSV HSL 62 80% 83% RGB HSV 324 56% 71% RGB HSL 320 36% 50% HSL HSV 62 28% 97% HSV RGB 181 80 140 */