如何通过代码在位图上应用蓝色到黄色的渐变,亮度作为因素

发布于 2024-12-26 12:33:52 字数 138 浏览 4 评论 0原文

我需要将蓝色到黄色的渐变应用于位图。 这里的因素是亮度。 照片的暗区域需要呈蓝色,最亮区域需要呈黄色。 因此,每个像素的亮度都需要作为一个因素。

有人可以帮助我如何用 c++ 或 java 实现这个目标吗? 输入是原始照片的 RGB 整数值数组。

I need to apply a blue to yellow gradient to a bitmap.
The factor here is the brightness.
The dark areas of the photo need to be blueish and the brightest area's yellow.
So the brightness of every pixel needs to be taken as a factor.

Can someone help me how to accomplish this in c++ or java?
The input is an array of rgb integer values of the original photo.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

向地狱狂奔 2025-01-02 12:33:52

听起来有点像家庭作业问题,但这是总体思路,或者至少是我将如何做到这一点。

对于每个像素,计算平均亮度,因此将 R GB 加在一起,然后除以 3 即可得到结果(您需要此处使用大于 8 位的变量!)。

现在您有一个介于 0-255 范围内的值,指示像素的亮度(有多种方法可以计算亮度,但目前仅此一种)。

全蓝色是 (0,0,255),全黄色是 (255,255,0) — 因此您需要在这些值之间进行插值(我们将在此处使用线性插值) ):

例如,如果您的亮度为 50,则约为 255 的 20%,因此您需要 80% 蓝色和 20% 黄色的颜色。您可以像这样计算红色通道的值:

R = (brightness / max) * (R in Yellow - R in Blue);

对其他通道进行类似的计算,因此对于亮度为 50 的像素,我们会这样做:

R = (50 / 255) * 255;
G = (50 / 255) * 255;

当然,我们不能有负值,并使用 B in Yellow - B in Blue 想法不会将其剪切为蓝色通道,您需要反转插值。通过取 0.2 并从 1 中减去它,我们可以在另一个方向上处理 0-255 的范围:

B = (1 - (50 / 255)) * 255;

额外说明:要在 C++ 中处理类似的事情,我建议使用 SDL,这种事情既好又简单。

Sounds a bit like a homework question, but here's the general idea, or at least, how I would do it.

For each pixel, calculate the average brightness, so add R G and B together then divide by 3 to get the result (you'll need to use a variable greater than 8 bits here!).

Now you have a value back in the range of 0-255 indicating the brightness of the pixel (there are various ways to calculate brightness but this will do for now).

Full blue is (0,0,255), full yellow is (255,255,0) — so you need to interpolate between these values (we'll use linear interpolation here):

If your brightness is 50 for instance, it's ~20% of 255, so you want a colour that's 80% blue and 20% yellow. You can calculate the valye for the red channel like so:

R = (brightness / max) * (R in Yellow - R in Blue);

With similar calculations for the other channels, so for our pixel with a brightness of 50 we'd do:

R = (50 / 255) * 255;
G = (50 / 255) * 255;

Of course, we can't have negative values, and using B in Yellow - B in Blue idea isn't going to cut it for the blue channel, you need to invert the interpolation. By taking our 0.2 and subtracting it from 1 we can work through the range 0-255 in the other direction:

B = (1 - (50 / 255)) * 255;

Extra note: To work with something like this in C++ I'd suggest using SDL, it's nice and easy this kind of thing.

那片花海 2025-01-02 12:33:52

如果我理解正确,以下内容(分别应用于所有像素)应该可以满足您的要求:

// max_value gives the maximum allowed value for red, green and blue; that is,
// if red, green and blue are all equal to max_value, you have full white)
change_pixel(int& red, int& green, int& blue, int max_value)
{
  blue = (red+green+blue)/3;
  red = green = (max_value-blue);
}

If I understood you correctly, the following (applied to all pixels individually) should do what you want:

// max_value gives the maximum allowed value for red, green and blue; that is,
// if red, green and blue are all equal to max_value, you have full white)
change_pixel(int& red, int& green, int& blue, int max_value)
{
  blue = (red+green+blue)/3;
  red = green = (max_value-blue);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文