简体   繁体   English

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

[英]Java swing. How to change color intensity from black to white?

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. 我的值从0到100。

Their representation is like this: 他们的表示是这样的:

0 - Black 0-黑色

100 - White 100-白色

I am painting JComponent and whenever I have to change color I have to call 我正在绘制JComponent,每当需要更改颜色时,都必须调用

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? 如何操纵RGB?

The Color API has HSB values which you may find easier to use. Color API具有HSB值,您可能会发现它更易于使用。

You can also use the HSL Colors . 您还可以使用HSL颜色 Check out the "luminance" tab when you start with a black or white color. 从黑色或白色开始,请查看“亮度”选项卡。

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. 因此,只需使用相等的RGB值,将0-100视为255的百分比。

For example, 50% is RGB of 128,128,128 (though depending on rounding you might arrive at 127,127,127). 例如,50%是128,128,128的RGB(尽管取决于舍入,您可能会得出127,127,127)。

The expression for going from a percentage value to a 0-255 is: 从百分比值到0-255的表达式为:

rgb=(pct*255)/100;

As long as values are equal in rgb you have a grey. 只要rgb中的值相等,您就会显示为灰色。

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. 是白色和黑色的均等混合。

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

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