2019独角兽企业重金招聘Python工程师标准>>>

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

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:

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:

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

2013 ACM/ICPC Asia Regional Changsha Online - C相关推荐

  1. 2013 ACM/ICPC Asia Regional Chengdu Online We Love MOE Girls 字符串STL 的应用

    题目来源: http://acm.hdu.edu.cn/showproblem.php?pid=4730 分析1:string s.substr(pos, n) 从 pos 开始 截取n个 字符 代码 ...

  2. 2013 ACM/ICPC Asia Regional Online —— Warmup1 1005 Balls Rearrangement

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4706 以前做过的题目: i从0到n-1时,如果一个一个加会很慢,注意到如果mod a的序列 和mod ...

  3. Problem 1002-2017 ACM/ICPC Asia Regional Shenyang Online

    网络赛:2017 ACM/ICPC Asia Regional Shenyang Online 题目来源:cable cable cable Problem Description: Connecti ...

  4. 2015 ACM/ICPC Asia Regional Shenyang Online题解

    以下所有AC题解程序来自"仙客传奇"团队. AC题数:7/13 ABCFGJL A. Traversal AC的C++语言程序: #include <bits/stdc++. ...

  5. 2017 ACM ICPC Asia Regional - Daejeon

    2017 ACM ICPC Asia Regional - Daejeon Problem A Broadcast Stations 题目描述:给出一棵树,每一个点有一个辐射距离\(p_i\)(待确定 ...

  6. 2014 ACM/ICPC Asia Regional Guangzhou Online C题Wang Xifeng's Little Plot(dfs)

    Wang Xifeng's Little Plot Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav ...

  7. The 36th ACM/ICPC Asia Regional Dalian Site 1006 Dave

    Dave Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65768/65768K (Java/Other) Total Submissi ...

  8. 2016 ACM/ICPC Asia Regional Shenyang Online

    I:QSC and Master 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5900 题意: 给出n对数keyi,vali表示当前这对数的键值和权值 ...

  9. hdu 5023 poj 2777(线段染色)2014 ACM/ICPC Asia Regional 广州 Online

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5023 http://poj.org/problem?id=2777 题意:给出一个长度为N的线段,分 ...

最新文章

  1. 从权限管理看互联网产品的盈利方式
  2. php连接数据库语言,PHP语言连接MYSQL数据库实例代码
  3. 加密、解密、摘要、签名、证书一文搞懂
  4. 事件驱动之JDK观察者模式
  5. mysql备份更换存储引擎_mysql数据库innodb存储引擎备份脚本
  6. BZOJ3019 : [Balkan2012]handsome
  7. 开发Teams Tabs应用程序
  8. AM335x kernel4.4.12 LCD 时钟翻转设置记录
  9. 数据结构压缩_将数据压缩到数据结构中
  10. 在线教育行业内容营销洞察白皮书(2021年版)
  11. ubuntu 15.10 安装jdk
  12. 使用OTL连接数据库有感篇(一)
  13. C语言 三角函数 时钟周期,三角函数的周期公式
  14. python实现对文件夹的图片分类存放(自动新建文件夹存放图片)
  15. html表格中间有空白,word文档表格中间出现空白怎么解决
  16. 数据结构期末大题速成
  17. 打印********的平行四边形
  18. 画笔和画刷的种类和使用方法
  19. 首个ChatGPT开发的应用上线;ChatMind思维导图工具;中文提示词大全;Copilot平替 | ShowMeAI日报
  20. 行人重识别 代码阅读(来自郑哲东 简单行人重识别代码到88%准确率)

热门文章

  1. 简单暴力到dp的优化(中级篇)
  2. java mvc 菜鸟_【java框架】SpringMVC(1)--SpringMVC入门
  3. Java命令:jstack — 获取线程dump信息
  4. vs2015 支持Android arm neon Introducing Visual Studio’s Emulator for Android
  5. 永远和靠谱的人在一起!
  6. Linux 上 安装 nginx、 阿里云服务器上安装 nginx
  7. mysql 索引:类型 、创建
  8. Prime Distance POJ - 2689 线性筛
  9. wampserver 虚拟主机
  10. Android Log