How do I recolor an image? (see images)

后端 未结 7 667
没有蜡笔的小新
没有蜡笔的小新 2020-12-29 08:53

How do I achieve this kind of color replacement programmatically? \"replacing


So this i

7条回答
  •  离开以前
    2020-12-29 08:58

    If any Android devs end up looking at this, this is what I came up with to gray scale and tint an image using CodesInChaos's formula and the android graphics classes ColorMatrix and ColorMatrixColorFilter.

    Thanks for the help!

    public static ColorFilter getColorFilter(Context context) {
        final int tint = ContextCompat.getColor(context, R.color.tint);
    
        final float R = Color.red(tint);
        final float G = Color.green(tint);
        final float B = Color.blue(tint);
    
        final float Rs = R / 255;
        final float Gs = G / 255;
        final float Bs = B / 255;
    
        // resultColor = oldColor + (1 - oldColor/255) * tintColor
        final float[] colorTransform = {
                1, -Rs, 0, 0, R,
                1, -Gs, 0, 0, G,
                1, -Bs, 0, 0, B,
                0, 0, 0, 0.9f, 0};
    
        final ColorMatrix grayMatrix = new ColorMatrix();
        grayMatrix.setSaturation(0f);
        grayMatrix.postConcat(new ColorMatrix(colorTransform));
        return new ColorMatrixColorFilter(grayMatrix);
    }
    

    The ColorFilter can then be applied to an ImageView

    imageView.setColorFilter(getColorFilter(imageView.getContext()));
    

提交回复
热议问题