如何褪色从白色到黑色? [英] How to fade color from white to black?

查看:276
本文介绍了如何褪色从白色到黑色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个int数组来表示RGB值。我有一个定时器的颜色从白色到黑色,但我有一些问题。 。



我有一个定时器,每100ms运行一次,并递减数组中的RGB值。



我有,我将最终与中间的其他颜色,绿色黄色,红色等。



我想要做的是从白色

解决方案

声明一个全局变量和调用 fading

  Color fading = Color.white; 

在您的计时器正文中:

  if(fading.getRed()> 0){
fading = new Color(fading.getRed() - 1,fading.getGreen() - 1,fading.getBlue -1);
//做任何与你的渐变颜色相关的操作
}

每当您的计时器执行此代码,它会将RGB值从白色减少到黑色。



结果:

  LOOP NO | COLOR 
1:R = 255,G = 255,B = 255
2:R = 254,G = 254,B = 254
3:R = 253,G = 253,B = 253
4:R = 252,G = 252,B = 252
5:R = 251,G = 251,B = 251
6:R = 250,G = B = 250



255:R = 0,G = 0,B = 0



<



示例:

  import java.awt。颜色; 

public Class YourClass {

颜色褪色= Color.white;

public YourClass(){
TimerTask task = new TimerTask(){
@Override
public void run(){
if(fading.getRed ()> 0){
fading = new Color(fading.getRed() - 1,fading.getGreen() - 1,fading.getBlue() - 1);
//在这里做其他事情
}
}
};

定时器timer =新的Timer();
timer.scheduleAtFixedRate(task,0,100);
}
}


I have a timer that's fading a color from white to black, but I'm having some issues.

I have an array of ints to represent the RGB values.

I have a timer which runs every 100ms and decrements the RGB value in the array.

The problem I'm having is that I will end up with other colors in the middle, green yellow, red, etc.

What I am trying to do is to fade from white to black, so I need the color to become a different shade of grey as the timer runs.

解决方案

Declare a global variable and call it fading.

Color fading = Color.white;

In your timer's body:

if(fading.getRed()>0){
    fading = new Color(fading.getRed()-1,fading.getGreen()-1,fading.getBlue()-1);
    //do whatever you have to do with your fading color
}

Everytime your timer executes this code it will decrease the RGB value from white to black.

The result:

LOOP NO | COLOR
       1: R=255, G=255, B=255
       2: R=254, G=254, B=254
       3: R=253, G=253, B=253
       4: R=252, G=252, B=252
       5: R=251, G=251, B=251
       6: R=250, G=250, B=250
        .
        .
        .
     255: R=0,   G=0,   B=0

EDIT

Example:

import java.awt.Color;

public Class YourClass{

    Color fading = Color.white;

    public YourClass(){
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                if (fading.getRed() > 0) {
                    fading = new Color(fading.getRed() - 1, fading.getGreen() - 1, fading.getBlue() - 1);
                    //do other stuff here
                }
            }
        };

        Timer timer = new Timer();
        timer.scheduleAtFixedRate(task, 0, 100);
    }
}

这篇关于如何褪色从白色到黑色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆