2013 ACM/ICPC Asia Regional Changsha Online Contest C

本文介绍RGB、HSL和HSV三种颜色模型的基本概念,并提供详细的数学公式和C++代码实现,用于不同颜色模型之间的转换。

莫名其妙的WA,莫名其妙的A……

不过正好练练代码风格。

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 (R1G1B1) 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 RG, 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 (R1G1B1) 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 RG, 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 (rgb) into a number between 0 and 1. Let max equals to the maximum value in rg and b. Let min equals to the minimum value in rg 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 = minh 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 \,
#include <cstdio>
#include <algorithm>
using namespace std;
typedef void(*cfunc)(double f[], double t[]);
double getH(double ma, double mi, double r, double g, double b)
{
	if (ma == mi)
		return 0;
	else if (ma == r && g >= b)
		return  60 * (g - b) / (ma - mi);
	else if (ma == r && g < b)
		return 60 * (g - b) / (ma - mi) + 360;
	else if (ma == g)
		return 60 * (b - r) / (ma - mi) + 120;
	else
		return 60 * (r - g) / (ma - mi) + 240;
}
void RGBtoHSL(double f[], double t[])
{
	double r = f[0] / 255, g = f[1] / 255, b = f[2] / 255;
	double ma = max(r, max(g, b));
	double mi = min(r, min(g, b));
	t[0] = getH(ma, mi, r, g, b);
	if (ma + mi == 0 || ma == mi)
		t[1] = 0;
	else if (0 < ma + mi && ma + mi <= 1)
		t[1] = (ma - mi) / (ma + mi);
	else if (ma + mi > 1)
		t[1] = (ma - mi) / (2 - (ma + mi));
	t[2] = (ma + mi) * .5;
	for (size_t i = 1; i < 3; i++)
		t[i] *= 100;
}
void RGBtoHSV(double f[], double t[])
{
	double r = f[0] / 255, g = f[1] / 255, b = f[2] / 255;
	double ma = max(r, max(g, b));
	double mi = min(r, min(g, b));
	t[0] = getH(ma, mi, r, g, b);
	t[1] = ma == 0 ? 0 : 1 - mi / ma;
	t[2] = ma;
	for (size_t i = 1; i < 3; i++)
		t[i] *= 100;
}
void getBase(double c, double h, double v[])
{
	double h_ = h / 60;
	double x = c * (1 - abs(fmod(h_, 2) - 1));
	if (0 <= h_ && h_ < 1)
		v[0] = c, v[1] = x;
	else if (h_ < 2)
		v[0] = x, v[1] = c;
	else if (h_ < 3)
		v[1] = c, v[2] = x;
	else if (h_ < 4)
		v[1] = x, v[2] = c;
	else if (h_ < 5)
		v[0] = x, v[2] = c;
	else if (h_ < 6)
		v[0] = c, v[2] = x;
}
void HSLtoRGB(double f[], double t[])
{
	double h = f[0], s = f[1] / 100, l = f[2] / 100;
	double c = (1 - abs(2 * l - 1)) * s;
	double rgb[3] = { 0,0,0 };
	getBase(c, h, rgb);
	double m = l - c * .5;
	for (int i = 0; i < 3; i++)
		t[i] = rgb[i] + m;
	for (size_t i = 0; i < 3; i++)
		t[i] *= 255;
}
void HSVtoRGB(double f[], double t[])
{
	double h = f[0], s = f[1] / 100, v = f[2] / 100;
	double c = v * s;
	double rgb[3] = { 0,0,0 };
	getBase(c, h, rgb);
	double m = v - c;
	for (int i = 0; i < 3; i++)
		t[i] = rgb[i] + m;
	for (size_t i = 0; i < 3; i++)
		t[i] *= 255;
}
void HSLtoHSV(double f[], double t[])
{
	double tmp[3];
	HSLtoRGB(f, tmp);
	RGBtoHSV(tmp, t);
}
void HSVtoHSL(double f[], double t[])
{
	double tmp[3];
	HSVtoRGB(f, tmp);
	RGBtoHSL(tmp, t);
}
cfunc crt['V' + 1]['V' + 1];
int main()
{
	crt['B']['L'] = RGBtoHSL;
	crt['B']['V'] = RGBtoHSV;
	crt['L']['B'] = HSLtoRGB;
	crt['L']['V'] = HSLtoHSV;
	crt['V']['B'] = HSVtoRGB;
	crt['V']['L'] = HSVtoHSL;
	crt['B']['B'] = crt['L']['L'] = crt['V']['V'] = [](double f[], double t[]) {
		for (int i = 0; i < 3; i++)
			t[i] = f[i];
	};
	char t[16], f[16];
	while (scanf("%s%s", t, f) != EOF)
	{
		double v[3], a[3];
		switch (f[2])
		{
		case 'B': scanf("%lf%lf%lf", v, v + 1, v + 2); break;
		case 'L':
		case 'V': scanf("%lf%lf%%%lf%%", v, v + 1, v + 2); break;
		}
		crt[f[2]][t[2]](v, a);
		switch (t[2])
		{
		case 'B': printf("RGB %.0f %.0f %.0f\n", a[0], a[1], a[2]); break;
		case 'L': printf("HSL %.0f %.0f%% %.0f%%\n", a[0], a[1], a[2]); break;
		case 'V': printf("HSV %.0f %.0f%% %.0f%%\n", a[0], a[1], a[2]); break;
		}
	}
	return 0;
}


内容概要:本文介绍了一个基于Simulink的混合储能驱动永磁同步电机全系统仿真模型,涵盖了系统整体架构与关键控制策略,重点实现了电流环的二阶滑模控制(STSMC)、有限集模型预测控制(FCS-MPC)和PI控制等多种先进控制方法。该模型集成了混合储能系统与永磁同步电机驱动系统,能够模拟复杂工况下的动态响应、能量管理过程及多变量耦合特性,适用于高性能电机控制系统的设计、分析与验证,尤其在新能源汽车、电动驱动系统和工业自动化等领域具有重要应用价值。; 适合人群:具备Simulink仿真基础、电力电子与电机控制背景的高校研究生、科研人员及自动化、电气工程领域的研发工程师。; 使用场景及目标:①用于研究和对比不同电流控制策略(如STSMC、FCS-MPC、PI)在永磁同步电机系统中的动态性能、鲁棒性与抗干扰能力;②支撑混合储能系统在电动驱动、新能源汽车、智能电网等领域的系统级仿真与优化设计;③为先进控制算法的开发与工程化落地提供高保真、模块化的仿真平台。; 阅读建议:建议结合Simulink模型与相关控制理论进行对照学习,重点关注各功能模块之间的信号交互、控制逻辑设计及参数整定方法,可通过修改负载条件、切换控制模式等方式开展对比实验,深入理解系统动态行为与控制效果差异。
软件概述 UG(Unigraphics NX)是一款由西门子(Siemens PLM Software)开发的交互式CAD/CAM/CAE系统。作为全球领先的产品工程解决方案,它集成了产品设计、工程仿真与制造加工于一体。其功能强大且应用广泛,能够轻松实现各种复杂实体和造型的构造,为模具、汽车、航空航天及通用机械等行业提供了高性能的机械设计与制图灵活性。 软件基础信息 • 支持系统: 64位 Windows 10、Windows 11 核心功能模块 一、创新设计:高效、灵活、无缝协同 全链路产品设计 涵盖从2D布局、3D建模、装配设计到图纸文档记录的各个环节,大幅提升设计吞吐量,缩短交付周期超35%。 强大的同步建模技术 打破数据壁垒,可无缝导入并直接修改来自其他CAD系统的几何模型,是跨平台协同设计的理想选择。 复杂装配管理 专为大型复杂产品打造,即使面对成千上万的零件也能从容应对,快速识别并解决数字样机中的干涉等问。 集成设计验证 内置自动验证功能,实时监控设计是否符合公司及行业标准;结合PLM数据可视化合成,辅助工程师做出更明智的决策。 二、综合仿真(Simcenter 3D):精准预测,降低试错成本 极速前后处理 依托先进的几何引擎,将强大的分析命令与几何编辑紧密集成,相比传统有限元工具,可缩短高达70%的仿真建模时间。 全方位结构分析 在同一环境中集成线性静力学、动态、疲劳及非线性分析,底层由业界顶尖的NX Nastran解算器提供支持,确保计算的高精度与可靠性。 声学与热管理分析 提供内外声学仿真以优化音质、降低噪音;具备一流的热传导仿真能力,帮助电子产品和工业机械实现最佳热管理方案。 多物理场耦合 简化了结构动力学、热传导、流体流动等复杂物理现象的模拟过程,消除外部数据传输错误,真实还原产品运行工况。 三、智能制造(CAM):打通从计划到车间的数字主线 全面的制造解决方案 提供从工装设计、CAM编程到机床控制器(如Sinumerik)的一体化支持,助力制定更科学的生产决策。 深度集成的PLM环境 借助Teamcenter实现数据和流程的统一管理,避免多数据库冲突,支持重用验证过的加工工艺与刀具库。 车间级互联 通过DNC系统与车间无缝对接,直接将加工数据和刀具清单下发至CNC机床,实现计划与生产的紧密结合。 提质增效 优化NC编程与刀具路径,提升表面精加工平与零件精度;减少人为错误,显著提高新机床部署成功率及制造资源利用率。 总结 UG NX 2023作为一款集成化的产品工程解决方案,通过其强大的设计、仿真和制造功能,为现代制造业提供了完整的数字化产品开发平台。无论是复杂产品的设计验证,还是精密制造的流程优化,UG NX 2023都能为工程师团队提供高效、可靠的解决方案,助力企业提升产品创新能力和市场竞争力。 适用领域 模具设计、汽车制造、航空航天、通用机械、消费电子等
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值