Icon Color Changer : Change Color of Icons

Here is the simple extension to change the color of the icon.

Which works in icons with transparent icons & it changes the color of the non transparent part of the icon.

Updated with latest version
Download : com.xt.iconcolorchanger.aix (3.9 KB)
Code :
IconColorChanger.java.txt (2.2 KB)

3 Likes

Unfortunately, you cannot set transparent colors. The image is therefore irreversibly covered by a color. It cannot be undone, even if you try to assign a new image. The image remains covered. You would have to reload the screen for this.

Apart from that, I can't really think of a reason why or for what this extension could be useful. Hiding an image or replacing it with another one is no problem anyway.

3 Likes

It was working good for my needs ,

Here is another version which make color change base on color matrix, works with transparent and remove color filter block

package com.xt.iconcolorchanger;

import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.widget.ImageView;

import com.google.appinventor.components.annotations.SimpleFunction;
import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
import com.google.appinventor.components.runtime.AndroidViewComponent;
import com.google.appinventor.components.runtime.ComponentContainer;

public class IconColorChanger extends AndroidNonvisibleComponent {
    public IconColorChanger(ComponentContainer container) {
        super(container.$form());
    }

    @SimpleFunction(description = "Applies a color filter to the icons")
    public void ApplyColorFilter(AndroidViewComponent imageComponent, int color) {
        if (imageComponent.getView() instanceof ImageView) {
            final ImageView imageView = (ImageView) imageComponent.getView();
            imageView.post(new Runnable() {
                @Override
                public void run() {
                    float alpha = ((color >> 24) & 0xff) / 255f;
                    float red = ((color >> 16) & 0xff) / 255f;
                    float green = ((color >> 8) & 0xff) / 255f;
                    float blue = (color & 0xff) / 255f;

                    float[] matrix = {
                        0, 0, 0, 0, red * 255,
                        0, 0, 0, 0, green * 255,
                        0, 0, 0, 0, blue * 255,
                        0, 0, 0, alpha, 0
                    };

                    ColorMatrixColorFilter colorFilter = new ColorMatrixColorFilter(new ColorMatrix(matrix));
                    imageView.setColorFilter(colorFilter);
                }
            });
        }
    }

    @SimpleFunction(description = "Removes the color filter from icon")
    public void RemoveColorFilter(AndroidViewComponent imageComponent) {
        if (imageComponent.getView() instanceof ImageView) {
            final ImageView imageView = (ImageView) imageComponent.getView();
            imageView.post(new Runnable() {
                @Override
                public void run() {
                    imageView.clearColorFilter();
                }
            });
        }
    }
}

com.xt.iconcolorchanger.aix (4.8 KB)

1 Like

Neither work for me in companion 2.71u, Android 13. If I set colour to Green, the image component just goes Green.

This only works with icons which has transparent background ,
Here is demo project
demo.aia (13.5 KB)

  1. You should have stated that in your first post
  2. This set the entire non transparent part of the image to the colour set
2 Likes

sure i will update