Android颜色偏移计算“轮子”-ColorShades

作用:取两个颜色的[0,1]区间值。和系统提供的ArgbEvaluator类功能差不多


import android.graphics.Color;

/**
 * Source from :
 * https://gist.github.com/cooltechworks/4f37021b1216f773daf8
 * Color shades will provide all the intermediate colors between two colors.
 * It just requires a decimal value between
 * 0.0 to 1.0
 * and it provides the exact shade combination of the two color with this shade value.
 * Textual explanation :
 * <p>
 * White          LtGray          Gray            DkGray           Black
 * 0               0.25            0.5             0.75            1
 * Given two colors as White and Black,and shade
 * as 0    gives White
 * as 0.25 gives Light gray
 * as 0.5  gives Gray
 * as 0.75 gives Dark gray
 * as 1    gives Black.
 */
public class ColorShades {

    private int mFromColor;
    private int mToColor;
    private float mShade;

    public ColorShades setFromColor(int fromColor) {
        this.mFromColor = fromColor;
        return this;
    }

    public ColorShades setToColor(int toColor) {
        this.mToColor = toColor;
        return this;
    }

    public ColorShades setFromColor(String fromColor) {
        this.mFromColor = Color.parseColor(fromColor);
        return this;
    }

    public ColorShades setToColor(String toColor) {
        this.mToColor = Color.parseColor(toColor);
        return this;
    }

    public ColorShades forLightShade(int color) {
        setFromColor(Color.WHITE);
        setToColor(color);
        return this;
    }

    public ColorShades forDarkShare(int color) {
        setFromColor(color);
        setToColor(Color.BLACK);
        return this;
    }

    public ColorShades setShade(float shade) {
        this.mShade = shade;
        return this;
    }

    /**
     * Generates the shade for the given color.
     *
     * @return the int value of the shade.
     */
    public int generate() {
        int fromR = Color.red(mFromColor);
        int fromG = Color.green(mFromColor);
        int fromB = Color.blue(mFromColor);

        int toR = Color.red(mToColor);
        int toG = Color.green(mToColor);
        int toB = Color.blue(mToColor);

        int diffR = toR - fromR;
        int diffG = toG - fromG;
        int diffB = toB - fromB;

        int red = fromR + (int) ((diffR * mShade));
        int green = fromG + (int) ((diffG * mShade));
        int blue = fromB + (int) ((diffB * mShade));

        return Color.rgb(red, green, blue);
    }

    /**
     * Assumes the from and to color are inverted before generating the shade.
     *
     * @return the int value of the inverted shade.
     */
    public int generateInverted() {
        int fromR = Color.red(mFromColor);
        int fromG = Color.green(mFromColor);
        int fromB = Color.blue(mFromColor);

        int toR = Color.red(mToColor);
        int toG = Color.green(mToColor);
        int toB = Color.blue(mToColor);

        int diffR = toR - fromR;
        int diffG = toG - fromG;
        int diffB = toB - fromB;

        int red = toR - (int) ((diffR * mShade));
        int green = toG - (int) ((diffG * mShade));
        int blue = toB - (int) ((diffB * mShade));
        return Color.rgb(red, green, blue);
    }

    /**
     * Gets the String equivalent of the generated shade
     *
     * @return String value of the shade
     */
    public String generateInvertedString() {
        return String.format("#%06X", 0xFFFFFF & generateInverted());
    }

    /**
     * Gets the inverted String equivalent of the generated shade
     *
     * @return String value of the shade
     */
    public String generateString() {
        return String.format("#%06X", 0xFFFFFF & generate());
    }
}

使用:

val shades = ColorShades()
shades.setFromColor(Color.RED)
        .setToColor(Color.BLUE)
        .setShade(offsetVal)
setBackgroundColor(shades.generate())

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值