2013 ACM/ICPC Asia Regional Changsha Online - C

2019独角兽企业重金招聘Python工程师标准>>> 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 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 "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

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

#include <iostream>
#include <string.h>
#include <math.h>
#include <stdio.h>
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<<"v s "<<s<<endl;c=v*sv;ht=h/60;x=c*(1-fabs(((((int)(ht*100000))%200000)*1.0)/100000 - 1));//cout<<"ht "<<ht<<endl;if(0<=ht && ht<1){r1=c;g1=x;b1=0;}else if(1<=ht && ht<2){r1=x;g1=c;b1=0;}else if(2<=ht && ht<3){r1=0;g1=c;b1=x;}else if(3<=ht && ht<4){r1=0;g1=x;b1=c;}else if(4<=ht && ht<5){r1=x;g1=0;b1=c;}else if(5<=ht && ht<6){r1=c;g1=0;b1=x;}else {r1=0;g1=0;b1=0;}m=v-c;/* r=(r1+m)*255;g=(g1+m)*255;b=(b1+m)*255;*/r=(r1+m);g=(g1+m);b=(b1+m);
}
void hsl2rgb(){c=(1-fabs(2*l-1))*sl;//  cout<<"c: "<<c<<endl;ht=h/60;x=c*(1-fabs(((((int)(ht*100000))%200000)*1.0)/100000 - 1));//  cout<<"ht: "<<ht<<endl;//cout<<"x"<<x<<endl;if(0<=ht && ht<1){r1=c;g1=x;b1=0;}else if(1<=ht && ht<2){r1=x;g1=c;b1=0;}else if(2<=ht && ht<3){r1=0;g1=c;b1=x;}else if(3<=ht && ht<4){r1=0;g1=x;b1=c;}else if(4<=ht && ht<5){r1=x;g1=0;b1=c;}else if(5<=ht && ht<6){r1=c;g1=0;b1=x;}else {r1=0;g1=0;b1=0;}m=l-c/2;
//    cout<<"m: "<<m<<endl;/* r=(r1+m)*255;g=(g1+m)*255;b=(b1+m)*255;*/r=(r1+m);g=(g1+m);b=(b1+m);
}
void rgb2hsl(){r=r/255;g=g/255;b=b/255;ma=r>g?(r>b?r:b):(g>b?g:b);mi=r<g?(r<b?r:b):(g<b?g:b);if(ma==mi){h=0;}else if(ma==r && g>=b){h=60*(g-b)/(ma-mi)+0;}else if(ma==r && g<b){h=60*(g-b)/(ma-mi)+360;}else if(ma==g){h=60*(b-r)/(ma-mi)+120;}else if(ma==b){h=60*(r-g)/(ma-mi)+240;}l=(ma+mi)/2;//cout<<l;if(l==0 || ma==mi){sl=0;}else if(0<=l && l<=0.5){sl=(ma-mi)/(ma+mi);}else if(l>0.5){sl=(ma-mi)/(2-(ma+mi));}
}
void rgb2hsv(){ma=r>g?(r>b?r:b):(g>b?g:b);mi=r<g?(r<b?r:b):(g<b?g:b);//cout<<r<<" "<<g<<" "<<b<<" "<<mi<<" "<<ma<<endl;if(ma==mi){h=0;}else if(ma==r && g>=b){h=60*(g-b)/(ma-mi)+0;}else if(ma==r && g<b){h=60*(g-b)/(ma-mi)+360;}else if(ma==g){h=60*(b-r)/(ma-mi)+120;}else if(ma==b){h=60*(r-g)/(ma-mi)+240;}if(ma==0){sv=0;}else {sv=(ma-mi)/ma;}v=ma;
}
int ff(double x)
{int tmp;tmp=floor(x);tmp=(x-tmp-0.5>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<<" s v "<<v<<endl;sv=sv/100;v=v/100;hsv2rgb();//printf("rgb %lf %lf%% %lf%%\n", r, g, b);}else if(strcmp(f, "HSL")==0){scanf("%lf %lf%% %lf%%", &h, &sl, &l);sl=sl/100;l=l/100;hsl2rgb();//printf("rgb %lf %lf%% %lf%%\n", r, g, b);}
//********************************************************if(strcmp(t, "RGB")==0){printf("%s %d %d %d\n", t, ff(r*255), ff(g*255), ff(b*255));}else if(strcmp(t, "HSV")==0){rgb2hsv();printf("%s %d %d%% %d%%\n", t, ff(h), ff(sv*100), ff(v*100));}else if(strcmp(t, "HSL")==0){rgb2hsl();printf("%s %d %d%% %d%%\n", t, ff(h), ff(sl*100), ff(l*100));}}return 0;
}

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/462080.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

vs2017常量文本字符串无法转换成char*

vs2017中这种写法编译不通过&#xff1a;char* pTest "hello"; 解决方法&#xff1a;在C/C选项-命令行-附加选项中增加&#xff1a;/Zc:strictStrings- (Disable string literal type conversion)转载于:https://www.cnblogs.com/dqloveu/p/11149222.html

linux中DIR、dirent、opendir()、readdir()、closedir()函数的使用

一、 1、DIR 属性&#xff1a;数据类型&#xff1b; 头文件&#xff1a;#include <dirent.h> 用法&#xff1a;定义一个指向文件目录的指针&#xff1b; 举例&#xff1a;DIR *dirpt null; 2、dirent 属性&#xff1a;数据类型&#xff0c;结构体&#xff1b; 头…

BNUOJ 4358 左手定则 搜索

题目链接&#xff1a;http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid4358 一道很好的搜索题&#xff0c;要注意DFS函数的写法&#xff0c;特别是return的写法。 View Code 1 #include <iostream>2 #include <cstring>3 #include <cstdio>4 using names…

CentOS安装Confluence Wiki步骤

参考&#xff1a;http://supernetwork.blog.51cto.com/2304163/1187066参考&#xff1a;http://yjiang.tk/?p1085需要的文件CentOS-6.5-x86_64-minimal.isojre-7u67-linux-x64.rpmatlassian-confluence-5.4.4-x64.binmysql-connector-java-5.1.32-bin.jarconfluence5.1-crack.…

memset()、memcpy()、memcmp()的使用方法

1、void *memset(void *s,int ch,size_t n); 属性&#xff1a;函数&#xff1b; 用法&#xff1a;将s中当前位置后面n个字节用ch替换&#xff0c;并返回s&#xff1b; 头文件(C语言)&#xff1a;#include <string.h> 举例&#xff1a; char str_addr[50]; memset(str…

provider: 命名管道提供程序, error: 40 - 无法打开到 SQL Server 的连接错误的解决方法...

这个错误主要有以下几个原因造成&#xff1a; 1. 错误的连接字符串&#xff1a;例如数据源的实例名称“\"错误写成"/"了 2、Named Pipes(NP)没有启动 其他原因&#xff0c;详见&#xff1a;http://social.msdn.microsoft.com/forums/en-US/sqlreportingservices…

使用VS2015编写C/C++开始步骤

下面围绕如何建立工程、如何添加代码和运行展开说明。 一、建立工程 &#xff08;1&#xff09;打开VS2015&#xff0c;然后在菜单栏中选择file—>New—>Project&#xff1b; &#xff08;2&#xff09;在弹出的界面中&#xff0c;选择Win32&#xff0c;编辑工程名字、…

ruby中正则表达式最小匹配与最大匹配

正则表达式中&#xff0c;默认的是最大匹配&#xff0c;即贪婪模式&#xff0c;但有些时候&#xff0c;要最小匹配&#xff0c; 请看下面的例子&#xff1a;(ruby) str "abbbbbdwwdwwwede"puts str[/a.*(dw)/] 结果为&#xff1a;abbbbbdwwdw str "abbbbbdwwd…

sprintf()、fprintf()的使用方法

1、int sprintf(char *str,char *format,[forgument,..]); 属性&#xff1a;函数&#xff1b; 用法&#xff1a;将格式化的字符写入字符串&#xff1b; 头文件&#xff1a;#include <stdio.h> 参数&#xff1a;char *str:要写入字符串的指针&#xff1b; char *form…

界面小项目之小米登录注册

<!DOCTYPE html><html><head> <meta charset"UTF-8"> <title>登录注册</title> <style> body, ul { margin: 0; padding: 0; list-style: none; } .logi…

Crusher Django 学习笔记4 使用Model

http://crusher-milling.blogspot.com/2013/09/crusher-django-tutorial4-using-basic.html 顺便学习一下FQ Crusher Django转载于:https://www.cnblogs.com/impact-crusher/p/3339029.html

C库中的输入函数、输出函数

关于这些C库输入函数、输出函数的用法&#xff0c;可以在命令行输入“man 3 xxx”来查询。 一、printf()函数、scanf()函数 1、printf()函数 函数模型 int printf(const char *format, ...); 函数作用 把存储在计算机中的二进制格式的数值&#xff0c;按照转换说明&#xff0c…

【音乐】想不起来歌名的瞎唱

1、风吹稻&#xff08;盗&#xff09;花香~好地方~ 2、转载于:https://www.cnblogs.com/wxl845235800/p/11152088.html

PWM调光方法在LED亮度调节中的应用

LED 是一种固态电光源&#xff0c; 是一种半导体照明器件&#xff0c;其电学特性具有很强的离散性。它具有体积小、机械强度大、功耗低、寿命长&#xff0c; 便于调节控制及无污染等特征&#xff0c;有极大发展前景的新型光源产品。LED 调光方法的实现分为两种&#xff1a; 模拟…

redhat rpmforge epel 安装源配置

参考阅读 epel 直接安装 RPMforge for CentOS 6 The default RPMforge repository does not replace any CentOS base packages. In the past it used to, but those packages are now in a separate repository (rpmforge-extras) which is disabled by default. You can fi…

知方可补不足~SQL2005使用ROW_NUMBER() OVER()进行数据分页

回到目录 数据分页是这个经常说的东西&#xff0c;无论在WEBForm还是WinForm中它都会被单独拿出来&#xff0c;或者是公用组件&#xff0c;或者是公用类库&#xff0c;反正对于数据分页这个东西&#xff0c;总是我们关注的一个话题&#xff0c;但事实上&#xff0c;数据分页归…

main函数的参数

main函数可以带参数&#xff0c;这个参数可以认为是 main函数的形式参数。C语言规定main函数的参数只能有两个&#xff0c;习惯上这两个参数写为argc和argv。因此&#xff0c;main函数的函数头可写为&#xff1a; main (argc&#xff0c;argv)。C语言还规定argc(第一个形参)必须…

leetcood学习笔记-2-两数相加

题目描述&#xff1a; 方法一&#xff1a; # Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val x # self.next Noneclass Solution:def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:cur…

AIX samba服务器配置

最简单的安装文件是。bff文件了&#xff0c;菜鸟的最爱。下载地址我找了很久&#xff0c;现在奉献出来&#xff1a;http://www.bullfreeware.com/一、下载samba-3.0.4.0.bff二、将文件放在AIX系统的临时安装目录下&#xff0c;如&#xff1a;/usr/samba三、 smit installp&…

Forward框架的逆袭:解析Forward+渲染

转载请注明出处为KlayGE游戏引擎&#xff0c;本文地址为http://www.klayge.org/2012/04/21/forward%e6%a1%86%e6%9e%b6%e7%9a%84%e9%80%86%e8%a2%ad%ef%bc%9a%e8%a7%a3%e6%9e%90forward%e6%b8%b2%e6%9f%93/ AMD在7900系列显卡发布的时候同时推出了Leo demo&#xff0c; 并说明它…