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,一经查实,立即删除!

相关文章

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.…

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

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

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…

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…

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; 并说明它…

Java NIO学习系列三:Selector

前面的两篇文章中总结了Java NIO中的两大基础组件Buffer和Channel的相关知识点&#xff0c;在NIO中都是通过Channel和Buffer的协作来读写数据的&#xff0c;在这个基础上通过selector来协调多个channel以同时读写数据&#xff0c;本文我们就来学习一下selector。 Java NIO中引入…

Java JTable3

预览&#xff1a; 代码 &#xff1a; /*** */ package com.han;import java.awt.Color; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; imp…

关于ASCII字符的那些事儿

1、单字符一般用单引号加一个字符表示&#xff0c;比如字符1表示为‘1’&#xff0c;而数字1直接表示为1&#xff1b; char ch 1 ;int num 1;2、计算机用数字来存储字符&#xff0c;比如字符1&#xff0c;在计算机里面为49&#xff08;十进制&#xff09;&#xff1b;而数字…

JqueryMobile学习之二---对话框

对话框 通过在链接中添加data-rel”dialog”的属性&#xff0c;可以使链接页面的显示方式变为对话框。给显示的对话框加入切换的效果也是一个不错的选择 例如我们将about的链接变成一个对话框并加入相应的切换效果。代码如下 <p><a href"#about" data-rel&q…

“leave the world behind”十一快乐出行

这个十一你打算怎么过&#xff1f;每天睡到自然醒&#xff0c;然后闷在家里埋头上网&#xff1f;选择晴朗好天气出去逛街&#xff0c;四处淘宝贝&#xff0c;淘美食&#xff1f;还是选择一个好的路线出去玩一周&#xff1f;其实行无论宅着还是选择出行&#xff0c;一定要让自己…

分享一个文件上传工具类

文件上传状态枚举类&#xff1a; View Code 1 package com.hoo.enums;2 3 4 5 /**6 7 * <b>function:</b> 文件上传状态8 9 * package com.hoo.enums 10 11 * fileName UploadState.java 12 13 * createDate 2010-10-11 下午1…

静态库和动态库的区别

库是写好的&#xff0c;现有的&#xff0c;成熟的&#xff0c;可以复用的代码。现实中每个程序都要依赖很多基础的底层库&#xff0c;不可能每个人的代码都从零开始&#xff0c;因此库的存在意义非同寻常。 本质上来说&#xff0c;库是一种可执行代码的二进制形式&#xff0c;可…

257. Binary Tree Paths

1、问题描述 2、代码&#xff08;非本人所写&#xff0c;十分精彩的C代码&#xff09; int pathsNum(struct TreeNode* root); void Traverse(struct TreeNode* root, char** array, char* spre, int* pindex); char* stringAdd(char* s, int val);char** binaryTreePaths(stru…

HDOJ树形DP专题之Centroid

题目链接 这题跟Balance Act那题差不多&#xff0c;求图的质点。我直接将那题改了一下提交&#xff0c;结果PE了一次&#xff0c;又WA了一次&#xff0c;最后发现是单case&#xff0c;多case的提交为什么WA呢&#xff1f; View Code 1 #include <stdio.h>2 #include <…

LAMP平台--部署Discuz论坛

环境&#xff1a;为了推广公司的产品并为客户服务提供一个交流平台&#xff0c;公司购买了一套Discuz论坛系统&#xff0c;要求安装到现有的LAMP服务器中&#xff0c;并简单划分论坛版块。需求&#xff1a;部署论坛服务器&#xff0c;安装Discuz论坛系统添加新区和版块产品发布…

ACM中java快速入门

2019独角兽企业重金招聘Python工程师标准>>> ACM中java快速入门 附&#xff1a; Chapter I. Java的优缺点各种书上都有&#xff0c;这里只说说用Java做ACM-ICPC的特点&#xff1a; (1) 最明显的好处是&#xff0c;学会Java&#xff0c;可以参加Java Challenge …

OV7725的帧率和PCLK寄存器设置

一、OV7725的PCLK的改变和以下几个寄存器有关&#xff1a; 1&#xff1a;OX0D&#xff08;COM4&#xff09;&#xff1b; ------------------------------------------------------------------------------------------------------------------ 0X0D COM4 41 …

演示:两台交换机成环后的STP计算原则

演示&#xff1a;两台交换机成环后的STP计算原则演示目标&#xff1a;理解两台交换机成环后&#xff0c;STP的计算原则&#xff0c;重点理解PID的作用。演示环境&#xff1a;如下图7.49所示。演示背景&#xff1a;上图所示的环境为两台交换机的生成树环境&#xff0c;其中S1有较…