Modifies the text color to get a better color for the background the text is displayed on - Android Graphics

Android examples for Graphics:Background Color

Description

Modifies the text color to get a better color for the background the text is displayed on

Demo Code


//package com.java2s;
import android.graphics.Color;

public class Main {
    /**//from  w  w  w . j  a v  a  2s.c o m
     * Modifies the text color to get a better
     * color for the background the text is displayed on
     * @param color
     * @param offset
     * @return optimized text color
     */
    public static int getOptimizedTextColor(int color, float offset) {
        int textColor = shiftColorHue(color, offset);

        double y = 0.2126 * Color.red(textColor) + 0.7152
                * Color.green(textColor) + 0.0722 * Color.blue(textColor);
        if (y < 180)
            textColor = Color.argb(255, 255, 255, 255);
        else
            textColor = Color.argb(255, 0, 0, 0);

        return textColor;
    }

    /**
     * Shifts the hue by the offset specified
     * @param color
     * @param offset
     * @return shifted color
     */
    public static int shiftColorHue(int color, float offset) {
        float[] colorShiftHSV = new float[3];
        Color.colorToHSV(color, colorShiftHSV);
        colorShiftHSV[0] = (colorShiftHSV[0] + offset) % 360;

        return Color.HSVToColor(colorShiftHSV);
    }
}

Related Tutorials