博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
2013 ACM/ICPC Asia Regional Changsha Online - C
阅读量:6719 次
发布时间:2019-06-25

本文共 4525 字,大约阅读时间需要 15 分钟。

hot3.png

竟然没写出来 还是比较坑,好吧

 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 .

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 .

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 "RGBrgb"(0 ≤r,g,b≤ 255), or HSL representation "HSLhs%l%"(0 ≤h< 360; 0 ≤s,l≤ 100), or HSV representation "HSVhs%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

HSLRGB 174 82 144HSVHSL 62 80% 83%RGBHSV 324 56% 71%

Sample Output

HSL 320 36% 50%HSV 62 28% 97%RGB 181 80 140

#include 
#include
#include
#include
using namespace std;char f[5], t[5], rgb[5];double h, sl ,sv, l, v, r, g, b;double c, ht, x, m, r1, g1, b1, ma, mi;void hsv2rgb(){ //cout<
<<"v s "<
<
g?(r>b?r:b):(g>b?g:b); mi=r
=b){ h=60*(g-b)/(ma-mi)+0; }else if(ma==r && g
0.5){ sl=(ma-mi)/(2-(ma+mi)); }}void rgb2hsv(){ ma=r>g?(r>b?r:b):(g>b?g:b); mi=r
=b){ h=60*(g-b)/(ma-mi)+0; }else if(ma==r && g
0.0000001?tmp+1:tmp); return tmp;}int main(){ while(scanf("%s", t)!=EOF){ scanf("%s", f);; if(strcmp(f, "RGB")==0){ scanf("%lf %lf %lf", &r, &g, &b); //printf("rgb %lf %lf%% %lf%%\n", r, g, b); } else if(strcmp(f, "HSV")==0){ scanf("%lf %lf%% %lf%%", &h, &sv, &v); //cout<
<<" s v "<
<

转载于:https://my.oschina.net/dianpaopao/blog/163290

你可能感兴趣的文章
金丝雀测试实践
查看>>
KubeEdge:开源的Kubernetes原生边缘计算框架
查看>>
AccessibilityService
查看>>
麦当劳数字化转型中获得的6个数据科学经验
查看>>
react反模式之index作为key
查看>>
如何撰写好文档?精益文档的六个实践
查看>>
专访朱诗雄:Apache Spark中的全新流式引擎Structured Streaming
查看>>
移动端DNUN:危险通知和用户导航
查看>>
举重若轻的人人车移动端数据平台
查看>>
麻省理工学院研究人员设计出针对幽灵党和熔毁的DAWG方法
查看>>
自由软件救世主Richard Stallman:我们可以比比特币做得更好
查看>>
AlphaZero进化论:从零开始,制霸所有棋类游戏
查看>>
百度云BaaS体系揭秘,突破共识机制、单机计算和串行处理三大瓶颈
查看>>
Prometheus正式从CNCF毕业
查看>>
专访《更敏捷的测试》作者Janet Gregory和Lisa Crispin
查看>>
伯克利论断:Serverless 才是云时代的主宰
查看>>
理解BERT Transformer:Attention is not all you need!
查看>>
PHP实现博客Ping功能源码分享
查看>>
端到端的超媒体REST API设计
查看>>
Microsoft的现代数据管理
查看>>