2013 ACM/ICPC Asia Regional Changsha Online - C Color Representation Conversion

Color Representation Conversion

Time Limit: 1 Second      Memory Limit: 32768 KB

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:

C = V \times S_{HSV}\,\!

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):

\begin{align}  H^\prime &= \frac{H}{60^\circ} \\  X        &= C (1 - |H^\prime \;\bmod 2 - 1|)\end{align}
  (R_1, G_1, B_1) =    \begin{cases}      (0, 0, 0) &\mbox{if } H \mbox{ is undefined} \\      (C, X, 0) &\mbox{if } 0 \leq H^\prime < 1 \\      (X, C, 0) &\mbox{if } 1 \leq H^\prime < 2 \\      (0, C, X) &\mbox{if } 2 \leq H^\prime < 3 \\      (0, X, C) &\mbox{if } 3 \leq H^\prime < 4 \\      (X, 0, C) &\mbox{if } 4 \leq H^\prime < 5 \\      (C, 0, X) &\mbox{if } 5 \leq H^\prime < 6    \end{cases}
\begin{align}  &m = V - C \\  &(R, G, B) = (R_1 + m, G_1 + m, B_1 + m)\end{align}

Finally, we can find R, G, and B by adding the same amount to each component, to match value:

\begin{align}  &m = V - C \\  &(R, G, B) = (R_1 + m, G_1 + m, B_1 + m)\end{align}

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:

C = \begin{align}  (1 - \left\vert 2 L - 1 \right\vert) \times S_{HSL} \end{align}

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):

\begin{align}  H^\prime &= \frac{H}{60^\circ} \\  X        &= C (1 - |H^\prime \;\bmod 2 - 1|)\end{align}
  (R_1, G_1, B_1) =    \begin{cases}      (0, 0, 0) &\mbox{if } H \mbox{ is undefined} \\      (C, X, 0) &\mbox{if } 0 \leq H^\prime < 1 \\      (X, C, 0) &\mbox{if } 1 \leq H^\prime < 2 \\      (0, C, X) &\mbox{if } 2 \leq H^\prime < 3 \\      (0, X, C) &\mbox{if } 3 \leq H^\prime < 4 \\      (X, 0, C) &\mbox{if } 4 \leq H^\prime < 5 \\      (C, 0, X) &\mbox{if } 5 \leq H^\prime < 6    \end{cases}

Finally, we can find R, G, and B by adding the same amount to each component, to match lightness:

\begin{align}  &m = L - \textstyle{\frac{1}{2}}C \\  &(R, G, B) = (R_1 + m, G_1 + m, B_1 + m)\end{align}

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]

h =\begin{cases}0^\circ & \mbox{if } max = min \\60^\circ \times \frac{g - b}{max - min} + 0^\circ,   & \mbox{if } max = r \mbox{ and } g \ge b \\60^\circ \times \frac{g - b}{max - min} + 360^\circ,   & \mbox{if } max = r \mbox{ and } g < b \\60^\circ \times \frac{b - r}{max - min} + 120^\circ, & \mbox{if } max = g \\60^\circ \times \frac{r - g}{max - min} + 240^\circ, & \mbox{if } max = b\end{cases}
l = \begin{matrix} \frac{1}{2} \end{matrix} (max + min)
s = \begin{cases}0 & \mbox{if } l = 0 \mbox{ or } max = min \\\frac{max-min}{max+min} = \frac{max-min}{2l}, & \mbox{if } 0  \frac{1}{2}\end{cases}


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:


s =\begin{cases}0, & \mbox{if } max = 0 \\\frac{max - min}{max} = 1 - \frac{min}{max}, & \mbox{otherwise}\end{cases}
v = max \,
Input

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.

Output

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.

Sample Input
HSL
RGB 174 82 144
HSV
HSL 62 80% 83%
RGB
HSV 324 56% 71%
Sample Output
HSL 320 36% 50%
HSV 62 28% 97%
RGB 181 80 140


思路:

直接按照它给的公式算就够了,值得注意的是:

1.double进行%运算,就相当于一直减那个数。

2.R、P、G算出来之后要四舍五入。


代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define maxn 105
#define mod 1000000000
#define INF 0x3f3f3f3f
using namespace std;

int R,G,B;
double r,g,b;
int H,S,V,L;
map<string,int>mp;
string obj,now;

int  getint()
{
	char c;
	int t=0;
	c=getchar();
	while(c<'0'||c>'9')  c=getchar();
	while(c>='0'&&c<='9')
	{
		t=10*t+c-'0';
		c=getchar();
	}
	return t;
}
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);
    H=(int)(h+0.5);
    L=(int)(l*100.0+0.5);
    S=(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;
    H=(int)(h+0.5);
    S=(int)(s*100.0+0.5);
    V=(int)(v*100.0+0.5);
}

void HSVtoRGB()
{
    int i,j;
    double r1,g1,b1,m;
    double c,x,h1,hh;
    c=V*0.01*S*0.01;
    h1=H*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=V*0.01-c;
    r=(r1+m);
    g=(g1+m);
    b=(b1+m);
    R=int(255*(r1+m)+0.5);
    G=int(255*(g1+m)+0.5);
    B=int(255*(b1+m)+0.5);
}
void HSLtoRGB()
{
    int i,j;
    double c,h1,hh,x,r1,g1,b1,m;
    c=(1-fabs(2*L*0.01-1))*S*0.01;
    h1=H*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*L-0.5*c;
    r=(r1+m);
    g=(g1+m);
    b=(b1+m);
    R=int(255*(r1+m)+0.5);
    G=int(255*(g1+m)+0.5);
    B=int(255*(b1+m)+0.5);
}
int main()
{
    int i,j,ma,mi;
    mp.clear();
    mp["RGB"]=1;
    mp["HSV"]=2;
    mp["HSL"]=3;
    while(cin>>obj)
    {
        cin>>now;
        if(mp[now]==1)  // RGB
        {
            scanf("%d%d%d",&R,&G,&B);
            if(mp[obj]==1)
            {
                printf("RGB %d %d %d\n",R,G,B);
            }
            else  if(mp[obj]==2) // HSV
            {
                r=R/255.0;
                g=G/255.0;
                b=B/255.0;
                RGBtoHSV();
                printf("HSV %d %d%% %d%%\n",H,S,V);
            }
            else
            {
                r=R/255.0;
                g=G/255.0;
                b=B/255.0;
                RGBtoHSL();
                printf("HSL %d %d%% %d%%\n",H,S,L);
            }
        }
        else if(mp[now]==2) // HSV
        {
            H=getint();
            S=getint();
            V=getint();
            if(mp[obj]==1)
            {
                HSVtoRGB();
                printf("RGB %d %d %d\n",R,G,B);
            }
            else if(mp[obj]==2)
            {
                printf("HSV %d %d%% %d%%\n",H,S,V);
            }
            else
            {
                HSVtoRGB();
                RGBtoHSL();
                printf("HSL %d %d%% %d%%\n",H,S,L);
            }
        }
        else    // HSL
        {
            H=getint();
            S=getint();
            L=getint();
            if(mp[obj]==1)
            {
                HSLtoRGB();
                printf("RGB %d %d %d\n",R,G,B);
            }
            else if(mp[obj]==2)
            {
                HSLtoRGB();
                RGBtoHSV();
                printf("HSV %d %d%% %d%%\n",H,S,V);
            }
            else printf("HSL %d %d%% %d%%\n",H,S,L);
        }
    }
    return 0;
}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值