Home » Android » image processing – Android color splash

image processing – Android color splash

Posted by: admin May 14, 2020 Leave a comment

Questions:

Can someone suggest me a fast method or a library for color splash effect? For example, I select a color and a photo gets desaturated for all colors except that one I have picked.

I have tried using pixel by pixel color check and then replace the color but it is too slow for big images.

int width = originalImage.getWidth();
int height = originalImage.getHeight();
int[] pixels = new int[width * height];
originalImage.getPixels(pixels, 0, width, 0, 0, width, height);

for (int x = 0; x < pixels.length; ++x) {
     pixels[x] = Distance(pixels[x], fromColor) < 4 ? targetColor : pixels[x];
}

Bitmap newImage = Bitmap.createBitmap(width, height, originalImage.getConfig());
newImage.setPixels(pixels, 0, width, 0, 0, width, height);


public int Distance(int a, int b) {
     return Math.abs(Color.red(a) - Color.red(b)) + Math.abs(Color.green(a) -
                     Color.green(b)) + Math.abs(Color.blue(a) - Color.blue(b));
}

EDIT:

Here are the original and the processed images, the color I am keeping is #ff9350:
enter image description here
enter image description here

How to&Answers:

You can try to go over possible color RGB values and prepareHashSet containing only those color values that should not be
desaturated. It seems that there should not be a lot of values.

After that you will be able to check whether the color should be desaturated or not. Thus, you will get rather long precalculation for a new given color, but your code that converts photo will become faster.

It can be implemented like so (haven’t tested it under Android, just some kind of Java pseudo-code to show the idea):

// precalculation
Set<Color> keptColors = new HashSet<Color>();
final int MAX_DISTANCE = 4;
for (int r=red(fromColor)-MAX_DISTANCE; r<=red(fromColor)+MAX_DISTANCE; r++) {
    for (int g=green(fromColor)-MAX_DISTANCE; g<=green(fromColor)+MAX_DISTANCE; g++) {
        for (int b=blue(fromColor)-MAX_DISTANCE; b<=blue(fromColor)+MAX_DISTANCE; b++) {
            if (Distance(rgb(r,g,b)), fromColor) < MAX_DISTANCE {
                keptColors.add(rgb(r,g,b));
            }
        }
    }
}
...

// in you photo processing code
keptColors.contains(pixels[x]) ? pixels[x] : targetColor;