문제

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?

도움이 되었습니까?

해결책

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;

다른 팁

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.

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top