Android UI How to - Get Highlight Color From Bitmap in case the averaged color is too light or too dark








Question

We would like to know how to get Highlight Color From Bitmap in case the averaged color is too light or too dark.

Answer

//w  w w  .  j ava2  s  .  c o m
import android.graphics.Bitmap;
import android.graphics.Color;

public class Main {
    public static int getHighlightColorFromBitmap(final Bitmap bitmap) {
        int incolor = Color.BLACK;
        if (null != bitmap) {
            final Bitmap bitmap1px = Bitmap.createScaledBitmap(bitmap, 1, 1, false);
            incolor = bitmap1px.getPixel(0, 0);
        }
        return getHighlightColor(incolor);
    }
    public static int getHighlightColor(int sampleColor) {
        float[] hsvBackground = new float[3];
        Color.colorToHSV(sampleColor, hsvBackground);
        hsvBackground[2] = 0.3f; // value parameter

        return Color.HSVToColor(0xf2, hsvBackground);
    }
}