简体   繁体   English

将RGB转换为HSL以实现更高分辨率的C ++

[英]Converting RGB to HSL for higher resolution C++

I am writing a program in c++, the program was to open an image and move the cursor around the image to read the temperature under the cursor. 我正在用C ++编写程序,该程序是打开图像并在图像周围移动光标以读取光标下方的温度。 The image has 14 different colour patches and I can read the colour and convert this into a temperature. 图像有14种不同的色块,我可以读取颜色并将其转换为温度。

The next thing I have to do is produce a method that can visualise the temperature with more resolution, ie with more than 14 colour patches. 接下来,我要做的是产生一种可以更高分辨率(即具有14个以上色块)可视化温度的方法。 Then draw the new colour patch at the bottom of the GUI on a panel to check if I can visually differentiate the colours for different temperatures in the range from 300K to 30000K. 然后在面板上GUI的底部绘制新的色块,以检查是否可以在视觉上区分300K至30000K范围内不同温度的颜色。

I am unsure on how to perform this last part. 我不确定如何执行最后一部分。 I think it has something to do with converting the RGB values to HSL, but I cant see how this would give me say 28 colour patches. 我认为这与将RGB值转换为HSL有关,但是我看不到这如何使我说28个色块。

Any help would be appreciated. 任何帮助,将不胜感激。

Thanks 谢谢

Converting from RGB to HSV is a fancy step that you can try later. 从RGB转换为HSV是一个不错的步骤,您可以稍后尝试。 I imagine that you want to render what looks like a continuous color gradient: 我想您想渲染看起来像连续的颜色渐变的东西:

在此处输入图片说明

Am I correct? 我对么? If you need to render hundreds of colors, but you have only 14 colors at hand, then you will need to blend between adjacent colors, which is called "interpolation." 如果需要渲染数百种颜色,但是手头只有14种颜色,则需要在相邻颜色之间进行混合,这称为“插值”。

We're going to try a "linear interpolation" because it's simple. 我们将尝试“线性插值”,因为它很简单。 You can try a "bicubic interpolation" later. 您可以稍后尝试“双三次插值”。

Let's say that you're about to render one of the single-color vertical lines in the gradient above. 假设您要在上面的渐变中渲染单色垂直线之一。 First you need to determine what color it's supposed to be. 首先,您需要确定它应该是什么颜色。 Let's say that the horizontal position of the vertical line can be between 0 and 99. 假设垂直线的水平位置可以在0到99之间。

Then, you need to find which two of your 14 colors to blend between: 然后,您需要找到14种颜色中的哪两种在以下两种颜色之间进行混合:

float x = horizontal_position * 13.f / 99.f;
int a = floorf(x);
int b = std::min( 13, a + 1 );

Now you need to blend between the two colors to find the color of the vertical line: 现在,您需要在两种颜色之间混合以找到垂直线的颜色:

float blend = x - a;
color result = palette[a] * (1-blend) + palette[b] * blend;

"color" in the pseudocode above can be RGB, YUV, HSV, etc. That part is up to you. 上面的伪代码中的“颜色”可以是RGB,YUV,HSV等。这取决于您。 Beware that HSV is a "nonlinear color space" and therefore blending two HSV colors linearly may produce unexpected results. 请注意,HSV是“非线性色彩空间”,因此,线性混合两种HSV颜色可能会产生意外的结果。

I'm not sure I understand exactly what you wish to do, but it kind of sounds like you're looking for an interpolating algorithm to basically enlarge your image, filling in the gaps with values outside the 14 colors the original has. 我不确定我确切地知道您想做什么,但是听起来好像您正在寻找一种插值算法来基本上放大图像,用原始14种颜色以外的值填充空白。

There are a myriad of such algorithms, but a simple bicubic interpolation should suffice. 有无数这样的算法,但是简单的双三次插值就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM