Created
June 3, 2020 07:21
-
-
Save m4xp1/e1d55677f22a2ac3ef1194ac592ecd87 to your computer and use it in GitHub Desktop.
Generate best colors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package one.xcorp.myeye.core.utils; | |
import android.graphics.Color; | |
import android.graphics.PorterDuff; | |
import android.graphics.PorterDuffColorFilter; | |
import android.graphics.drawable.Drawable; | |
import android.support.annotation.ColorInt; | |
public class Colorize { | |
public static final float HSV_SATURATION_DARK = 0.99f; | |
public static final float HSV_VALUE_DARK = 0.99f; | |
public static final float HSV_SATURATION_DEFAULT = 0.5f; | |
public static final float HSV_VALUE_DEFAULT = 0.95f; | |
public static final float HSV_SATURATION_LIGHT = 0.3f; | |
public static final float HSV_VALUE_LIGHT = 0.99f; | |
private final int offset; | |
private final int count; | |
private float saturation = HSV_SATURATION_DEFAULT; | |
private float value = HSV_VALUE_DEFAULT; | |
private int position; | |
public Colorize() { | |
this(0, -1); | |
} | |
public Colorize(int offset, int count) { | |
this.offset = offset; | |
this.count = count > 0 ? count : Integer.MAX_VALUE; | |
} | |
public int getOffset() { | |
return offset; | |
} | |
public int getCount() { | |
return count; | |
} | |
public float getSaturation() { | |
return saturation; | |
} | |
public Colorize setSaturation(float saturation) { | |
this.saturation = saturation; | |
return this; | |
} | |
public float getValue() { | |
return value; | |
} | |
public Colorize setValue(float value) { | |
this.value = value; | |
return this; | |
} | |
public int getPosition() { | |
return position; | |
} | |
public Colorize setPosition(int position) { | |
if (position > count - 1) { | |
throw new IndexOutOfBoundsException("Specified position exceeds total colors count"); | |
} | |
this.position = position; | |
return this; | |
} | |
/** | |
* Generated random color had to be "good looking". | |
* | |
* @return color | |
*/ | |
@ColorInt | |
public int generateColor() { | |
// use random init value 0.038764358f and golden ratio 0.618033988749895f | |
float hue = (0.038764358f + 0.618033988749895f * offset + position++) % 1; | |
int color = Color.HSVToColor(new float[]{hue * 360, saturation, value}); | |
if (position > count - 1) { | |
position = 0; | |
} | |
return color; | |
} | |
/** | |
* Generate text color had to be "good looking". | |
* | |
* @param color background color | |
* @return text color | |
*/ | |
@ColorInt | |
public static int getContrastColor(@ColorInt int color) { | |
// Counting the perceptive luminance - human eye favors green color... | |
double a = 1 - (0.299 * Color.red(color) + 0.587 * | |
Color.green(color) + 0.114 * Color.blue(color)) / 255; | |
return a < 0.5 ? Color.BLACK : Color.WHITE; | |
} | |
/** | |
* Change drawable color. | |
* | |
* @param drawable drawable | |
* @param color color | |
* @return mutate drawable with specified color | |
*/ | |
public static Drawable colorize(Drawable drawable, @ColorInt int color) { | |
if (drawable != null) { | |
drawable = drawable.mutate(); | |
drawable.setColorFilter( | |
new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN)); | |
} | |
return drawable; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment