mirror of https://github.com/M66B/FairEmail.git
Check background/foreground luminance
This commit is contained in:
parent
65b63104fd
commit
4928d02dd3
|
@ -130,7 +130,7 @@ public class HtmlHelper {
|
||||||
private static final int DEFAULT_FONT_SIZE = 16; // pixels
|
private static final int DEFAULT_FONT_SIZE = 16; // pixels
|
||||||
private static final int DEFAULT_FONT_SIZE_PT = 12; // points
|
private static final int DEFAULT_FONT_SIZE_PT = 12; // points
|
||||||
private static final int GRAY_THRESHOLD = Math.round(255 * 0.2f);
|
private static final int GRAY_THRESHOLD = Math.round(255 * 0.2f);
|
||||||
private static final double BG_LUM_THRESHOLD = 0.1;
|
private static final int COLOR_THRESHOLD = Math.round(255 * 0.1f);
|
||||||
private static final float MIN_LUMINANCE = 0.7f;
|
private static final float MIN_LUMINANCE = 0.7f;
|
||||||
private static final int TAB_SIZE = 2;
|
private static final int TAB_SIZE = 2;
|
||||||
private static final int MAX_ALT = 250;
|
private static final int MAX_ALT = 250;
|
||||||
|
@ -598,42 +598,46 @@ public class HtmlHelper {
|
||||||
Integer color = parseColor(value);
|
Integer color = parseColor(value);
|
||||||
|
|
||||||
if ("color".equals(key)) {
|
if ("color".equals(key)) {
|
||||||
boolean bg = false;
|
Integer bg = null;
|
||||||
if (background_color) {
|
if (background_color) {
|
||||||
Element e = element;
|
Element e = element;
|
||||||
while (e != null && !bg)
|
while (e != null && bg == null)
|
||||||
if (e.hasAttr("x-background"))
|
if (e.hasAttr("x-background"))
|
||||||
bg = true;
|
bg = parseWebColor(e.attr("x-background"));
|
||||||
else
|
else
|
||||||
e = e.parent();
|
e = e.parent();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Background color set:
|
if (bg == null) {
|
||||||
// keep text color as-is
|
|
||||||
if (!bg) {
|
|
||||||
// Special case:
|
// Special case:
|
||||||
// external draft / dark background / dark font
|
// external draft / dark background / dark font
|
||||||
if (color != null && !view && dark) {
|
if (color != null && !view && dark) {
|
||||||
float lum = (float) ColorUtils.calculateLuminance(color);
|
double lum = ColorUtils.calculateLuminance(color);
|
||||||
if (lum < 0.1f)
|
if (lum < 0.1)
|
||||||
color = null;
|
color = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (color != null && view)
|
if (color != null && view)
|
||||||
color = adjustColor(dark, textColorPrimary, color);
|
color = adjustColor(dark, textColorPrimary, color);
|
||||||
|
} else {
|
||||||
|
// Check background/foreground luminance
|
||||||
|
if (color != null) {
|
||||||
|
double lbg = ColorUtils.calculateLuminance(bg);
|
||||||
|
double lfg = ColorUtils.calculateLuminance(color);
|
||||||
|
if (Math.abs(lbg - lfg) < 0.1) {
|
||||||
|
color = (lbg < 0.5 ? Color.WHITE : Color.BLACK);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (color != null)
|
if (color != null)
|
||||||
element.attr("x-color", "true");
|
element.attr("x-color", "true");
|
||||||
} else /* background */ {
|
} else /* background */ {
|
||||||
if (color != null && view) {
|
if (color != null && !hasColor(color))
|
||||||
double lum = ColorUtils.calculateLuminance(color);
|
continue;
|
||||||
if (dark ? lum < BG_LUM_THRESHOLD : lum > 1 - BG_LUM_THRESHOLD)
|
|
||||||
color = Color.TRANSPARENT;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (color != null)
|
if (color != null)
|
||||||
element.attr("x-background", "true");
|
element.attr("x-background", encodeWebColor(color));
|
||||||
|
|
||||||
if (dark) {
|
if (dark) {
|
||||||
boolean fg = false;
|
boolean fg = false;
|
||||||
|
@ -1713,6 +1717,14 @@ public class HtmlHelper {
|
||||||
return color;
|
return color;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static boolean hasColor(int color) {
|
||||||
|
int r = Color.red(color);
|
||||||
|
int g = Color.green(color);
|
||||||
|
int b = Color.blue(color);
|
||||||
|
return (Math.abs(r - g) >= COLOR_THRESHOLD ||
|
||||||
|
Math.abs(r - b) >= COLOR_THRESHOLD);
|
||||||
|
}
|
||||||
|
|
||||||
// https://tools.ietf.org/html/rfc3676
|
// https://tools.ietf.org/html/rfc3676
|
||||||
static String flow(String text) {
|
static String flow(String text) {
|
||||||
boolean continuation = false;
|
boolean continuation = false;
|
||||||
|
|
Loading…
Reference in New Issue