渐变算法的 Java 实现

    /**
     * 指定长度的渐变。
     * @param c0 起始颜色。
     * @param c1 结束颜色。
     * @param len 受限的渐变长度,为保证每一个颜色都不一样,会根据颜色找出长度最大值。
     * @return 长度为参数 len+1 的所有颜色。
     */
    public static ArrayList<Integer> gradient(int c0, int c1, double len) {
        int[] fc = { (c0 & 0xff0000) >> 16, (c0 & 0xff00) >> 8, c0 & 0xff };
        int[] tc = { (c1 & 0xff0000) >> 16, (c1 & 0xff00) >> 8, c1 & 0xff };
        len = Math.min(len, Math.max(Math.max(Math.abs(fc[0] - tc[0]), Math.abs(fc[1] - tc[1])), Math.abs(fc[2] - tc[2])));
        double[] s = { (tc[0] - fc[0]) / len, (tc[1] - fc[1]) / len, (tc[2] - fc[2]) / len, };
        ArrayList<Integer> r = new ArrayList<Integer>();
        for (int i = 0; i < len; i++) {
            r.add(fc[0] + (int) (i * s[0]) << 16 | fc[1] + (int) (i * s[1]) << 8 | fc[2] + (int) (i * s[2]));
        }
        r.add(c1);
        return r;
    }

 C#用得都懒了,难怪C#开发者的平均水平低。偶尔用用 C# 幸福感倍增!

原文地址:https://www.cnblogs.com/ly45/p/6228039.html