爪哇摇摆。如何将颜色强度从黑色更改为白色?

发布于 2024-10-01 01:23:11 字数 250 浏览 0 评论 0原文

我不知道如何将颜色仅从黑色更改为白色。灰度。

我的例子。我的值从 0 到 100。

它们的表示如下:

0 - 黑色

100 - 白色

我正在绘制 JComponent,每当我必须更改颜色时,我都必须调用

g.setColor(Color);

但如何告诉它仅将颜色从黑色更改为白色,取决于我的号码(号码越大,颜色越白)? 如何操纵RGB?

I don't see the how to change color only from black to white. Grayscale.

My example. I have values that goes from 0 to 100.

Their representation is like this:

0 - Black

100 - White

I am painting JComponent and whenever I have to change color I have to call

g.setColor(Color);

But how to tell it to change color only from Black to White, depending of my number (the larger the number, color is whiter)?
How to manipulate over RGB?

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

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

发布评论

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

评论(3

绮筵 2024-10-08 01:23:11

Color API 具有 HSB 值,您可能会发现它们更易于使用。

您还可以使用 HSL 颜色。当您从黑色或白色开始时,请检查“亮度”选项卡。

The Color API has HSB values which you may find easier to use.

You can also use the HSL Colors. Check out the "luminance" tab when you start with a black or white color.

海螺姑娘 2024-10-08 01:23:11

当然,从黑色到白色本身并没有颜色变化,除非逐渐变浅的灰色阴影。

因此,只需使用相同的 RGB 值,将 0 - 100 视为 255 的百分比。

例如,50% 是 128,128,128 的 RGB(尽管根据舍入,您可能会得到 127,127,127)。

从百分比值变为 0-255 的表达式为:

rgb=(pct*255)/100;

There is, of course, no color change from black to white, per se, except through gradually lighter shades of gray.

So, just use equal values of RGB, treating 0 - 100 as a percentage of 255.

For example, 50% is RGB of 128,128,128 (though depending on rounding you might arrive at 127,127,127).

The expression for going from a percentage value to a 0-255 is:

rgb=(pct*255)/100;
疾风者 2024-10-08 01:23:11

只要 rgb 中的值相等,您就会得到灰色。

也是如此

g.setColor(new Color(0, 0, 0);

白色

g.setColor(new Color(255,255,255);

。黑色

g.setColor(new Color(128,128,128);

是白色和黑色的等量混合。

As long as values are equal in rgb you have a grey.

so

g.setColor(new Color(0, 0, 0);

is white.

g.setColor(new Color(255,255,255);

is black

g.setColor(new Color(128,128,128);

is an equal blend of white and black.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文