在n次迭代之后趋向于给定数量的算法

时间:2015-11-21 00:57:22

标签: java algorithm math colors trend

我正在尝试找到一个好的算法来完成以下任务:

我有两种RGB颜色。我从一种颜色(比如红色= 255,0,0)开始,经过多次迭代后,我希望它变为蓝色(0,0,255)。

我当前的算法只是取颜色的每个分量的总和并除以2,这样做的技巧却太快了。在每次迭代中,我希望数字仅改变其原始值的十分之一。因此,迭代1可能会返回颜色(230,0,25),依此类推。请记住,目标颜色也可以更改。突然而不是蓝色,我想要绿色。

有谁知道实现这个目标的好方法?我似乎无法弄清楚数学。

谢谢!

2 个答案:

答案 0 :(得分:0)

好的'数学工作(像往常一样),所以让我们从一个更加数学的方法开始:

value-space RGB: [0,255]^3

Let a,b e RGB , step_w, step_no e N
f(a , b , step_w , step_no) = (a0 + (b0 - a0) / step_w * step_no , a1 + (b1 ...

从数学到实际代码:

Color f(Color a , Color b , int step_w , int step_no){
    return new Color(a.getRed() + (b.getRed() - a.getRed()) / step_w * step_no , a.getGreen() + (b.getGreen() - a.getGreen()) / step_w * step_no , ...);
}

step_w是总步数和step_no到目前为止执行的步数。对于f(c1 , c2 , x , y)c1会返回y = 0c2会返回y = x0 < y < x会返回匹配的颜色。

虽然有更好的色彩转换方法(实验室色彩空间,HSL等),看起来更自然。

答案 1 :(得分:0)

已有两篇关于其他色彩空间和线性方法的帖子。

但如果您真的在寻找一种能够满足您要求的算法,请查看:

static class ColorChanger {
    static private final int    APPROACH_STEPS  = 10;

    private final Color         mStartColor;
    private final Color         mTargetColor;

    private int                 mApproachStep   = 0;
    private Color               mCurrentColor;

    public ColorChanger(final Color pStartColor, final Color pTargetColor) {
        mStartColor = pStartColor;
        mTargetColor = pTargetColor;
        System.out.println("\nStarting color is: " + mStartColor);
        System.out.println("Approaching target 1: " + mTargetColor);
    }

    public Color approach() {
        ++mApproachStep;
        if (mApproachStep <= APPROACH_STEPS) { // dont overshoot target color. could throw an exception here too
            final int newRedCode = nextColorCode(mStartColor.getRed(), mTargetColor.getRed());
            final int newGreenCode = nextColorCode(mStartColor.getGreen(), mTargetColor.getGreen());
            final int newBlueCode = nextColorCode(mStartColor.getBlue(), mTargetColor.getBlue());
            mCurrentColor = new Color(newRedCode, newGreenCode, newBlueCode);
        }
        System.out.println("\tNew step color is: " + mCurrentColor);
        return mCurrentColor;
    }

    private int nextColorCode(final int pCurrentCode, final int pTargetCode) {
        final int diff = pTargetCode - pCurrentCode;
        final int newCode = pCurrentCode + diff * mApproachStep / APPROACH_STEPS;
        return newCode;
    }

    public Color getCurrentColor() {
        return mCurrentColor;
    }

    public boolean isTargetColor() {
        return mApproachStep == APPROACH_STEPS;
    }
}

public static void main(final String[] args) {
    final Color startColor = Color.RED;
    final Color targetColor1 = Color.GREEN;
    final Color targetColor2 = Color.BLUE;
    final Color targetColor3 = Color.RED;

    // approach in only 5 steps, will by far not reach target color
    final ColorChanger cc1 = new ColorChanger(startColor, targetColor1);
    for (int i = 0; i < 5; i++) {
        cc1.approach();
    }

    // full approach #1
    final ColorChanger cc2 = new ColorChanger(cc1.getCurrentColor(), targetColor2);
    while (!cc2.isTargetColor()) {
        cc2.approach();
    }

    // full approach #2
    final ColorChanger cc3 = new ColorChanger(cc2.getCurrentColor(), targetColor3);
    for (int i = 0; i < ColorChanger.APPROACH_STEPS; i++) {
        cc3.approach();
    }

    System.out.println("Program ends");
}