天天看點

漸變算法的 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