我有一種方法將父級的文本和圖像色調設置爲某種顏色。現在,如果父母和前景的背景(我是設置的色調)相近,則文本將不可讀。找到兩種顏色之間的對比差異
我該如何檢查這兩種顏色之間的差異,並將其改變(使其變得更亮或更暗)直至它們變得可讀?
下面是我得到了什麼至今:
public static void invokeContrastSafety(ViewGroup parent, int tint, boolean shouldPreserveForeground) {
Drawable background = parent.getBackground();
if (background instanceof ColorDrawable) {
if (isColorDark(((ColorDrawable) background).getColor())) {
// Parent background is dark
if (isColorDark(tint)) {
// Tint (foreground) color is also dark.
if (shouldPreserveForeground) {
// We can't modify tint color, changing background to make things readable.
} else {
// Altering foreground to make things readable
}
invokeInternal(parent, tint);
} else {
// Everything is readable. Just pass it on.
invokeInternal(parent, tint);
}
} else {
// Parent background is light
if (!isColorDark(tint)) {
if (shouldPreserveForeground) {
} else {
}
} else {
invokeInternal(parent, tint);
}
}
}
}
private static boolean isColorDark(int color){
double darkness = 1-(0.299* Color.red(color) + 0.587*Color.green(color) + 0.114*Color.blue(color))/255;
return darkness >= 0.2;
}
看到我的答案希望這會幫助你。 –
可能重複[公式來確定RGB顏色的亮度](https://stackoverflow.com/questions/596216/formula-to-determine-brightness-of-rgb-color) – geza