Android HSL 顏色值計算

需求

在這裏插入圖片描述

代碼

    float[] outHsl = new float[]{0f, 0f, 0f};

        ColorUtils.colorToHSL(Color.parseColor("#ef2b2c"), outHsl);
        ((ImageView) findViewById(R.id.imageView5)).setBackgroundColor(Color.parseColor("#ef2b2c"));

        LogUtils.e("" + outHsl.toString());


        float[] newHsl = new float[]{15, outHsl[1] * 1.16f, outHsl[2] * 0.93f};


        ColorUtils.HSLToColor(newHsl);
        ((ImageView) findViewById(R.id.imageView6)).setBackgroundColor(ColorUtils.HSLToColor(newHsl));
        ((ImageView) findViewById(R.id.imageView7)).setBackgroundColor(Color.parseColor("#ff4306"));
        float[] outHsl1 = new float[]{0f, 0f, 0f};
        ColorUtils.colorToHSL(Color.parseColor("#ff4306"), outHsl1);

        ((TextView) findViewById(R.id.textView10)).setText("主 #ef2b2c \n" +
                "SHL" + Arrays.toString(outHsl) + "\n" +
                "C1 #ff4306 \n" +
                "C1 SHL" + Arrays.toString(newHsl) + "\n" +
                "C1 SHL" + Arrays.toString(outHsl1) + "\n"

        );

結果

在這裏插入圖片描述

簡單的封裝使用

    /**
     *
     * @param color 顏色值
     * @param outHsl 輸出比例
     * @return
     */
    public static int computeHSL(String color, float... outHsl) {
        float[] hsl = new float[]{0f, 0f, 0f};
        ColorUtils.colorToHSL(Color.parseColor(color), hsl);
        float[] newHsl = new float[]{outHsl[0], hsl[1] * outHsl[1], hsl[2] * outHsl[2]};
        return ColorUtils.HSLToColor(newHsl);

    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章