褪色算法? [英] Color fading algorithm?

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

问题描述

我正在创建一些自定义的Swing组件,我希望从一种颜色淡出到另一种颜色。现在我从RGB转换为HSB,然后递增通过Hue值,并在绘画前转换回RGB,工作很好。

I'm creating some custom Swing components that I'd like to have fade from one color to another. At the moment I'm converting from RGB to HSB then incrementing through the Hue value and converting back to RGB before painting, work's fine.

但是,这循环遍历所有颜色(即试图通过黄色,橙色,红色等从蓝色渐变到绿色循环)。有没有一个体面的算法/方法直接从一种颜色褪色到另一种?

However, this cycles through all the colors (i.e. attempting to fade from blue to green cycles through yellow, orange, red etc). Is there a decent algorithm/method to fade directly from one color into another?

编辑:我已经通过一个Swing计时器更新组件与线程像瘟疫)。

I already had it updating via a Swing Timer (I try to steer clear of touching components with Threads like the plague). I'll have a go this evening with your suggestions, THANKS GUYS!

推荐答案

根据这个示例,以下 Queue< Color> 循环从 Color.green Color.blue ,再回到 Color.green N = 32 步骤。注意,在HSB模型中, Color.green 在数值上小于 Color.blue 。另请参阅使用HSB的此相关示例

Based on this example, the Queue<Color> below cycles from Color.green to Color.blue and back to Color.green again in N = 32 steps. Note that Color.green is numerically less than Color.blue in the HSB model. See also this related example using HSB.

>

public Flash(JComponent component) {
    this.component = component;
    float gHue = Color.RGBtoHSB(0, 1, 0, null)[0];
    float bHue = Color.RGBtoHSB(0, 0, 1, null)[0];
    for (int i = 0; i < N; i++) {
        clut.add(Color.getHSBColor(gHue + (i * (bHue - gHue) / N), 1, 1));
    }
    for (int i = 0; i < N; i++) {
        clut.add(Color.getHSBColor(bHue - (i * (bHue - gHue) / N), 1, 1));
    }
}

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

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