Adjust luminance for dark/light colors

pull/162/head
M66B 5 years ago
parent b33b72356d
commit cfeb673c4b

@ -111,6 +111,8 @@ public class Helper {
static final int NOTIFICATION_UPDATE = 4;
static final float LOW_LIGHT = 0.6f;
static final float MIN_LUMINANCE = 0.33f;
static final int BUFFER_SIZE = 8192; // Same as in Files class
static final String FAQ_URI = "https://github.com/M66B/FairEmail/blob/master/FAQ.md";

@ -24,6 +24,7 @@ import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.ImageDecoder;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
@ -44,6 +45,7 @@ import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.graphics.ColorUtils;
import androidx.core.text.HtmlCompat;
import androidx.core.util.PatternsCompat;
import androidx.preference.PreferenceManager;
@ -145,6 +147,8 @@ public class HtmlHelper {
final Document document = new Cleaner(whitelist).clean(parsed);
boolean dark = Helper.isDarkTheme(context);
// Sanitize span styles
for (Element span : document.select("*")) {
String style = span.attr("style");
@ -157,12 +161,39 @@ public class HtmlHelper {
if (kv.length == 2)
switch (kv[0].trim().toLowerCase(Locale.ROOT)) {
case "color":
sb.append(param).append(";");
String c = kv[1].trim().toLowerCase(Locale.ROOT);
Integer color = null;
if (!c.startsWith("#"))
try {
color = Color.parseColor(c);
} catch (IllegalArgumentException ex) {
Log.e("Color=" + c);
}
if (color == null)
try {
color = Integer.decode(c) | 0xFF000000;
} catch (NumberFormatException ex) {
Log.e("Color=" + c);
}
if (color != null) {
double lum = ColorUtils.calculateLuminance(color);
if (dark
? lum < Helper.MIN_LUMINANCE
: lum > 1 - Helper.MIN_LUMINANCE)
color = ColorUtils.blendARGB(color,
dark ? Color.WHITE : Color.BLACK, Helper.MIN_LUMINANCE);
c = String.format("#%06x", 0xFFFFFF & color);
sb.append("color:").append(c).append(";");
}
break;
case "background":
case "background-color":
break;
case "line-through":
sb.append(param).append(";");
break;
}
}

@ -37,7 +37,8 @@ class Identicon {
byte[] hash = getHash(email);
int color = Color.argb(255, hash[0], hash[1], hash[2]);
color = ColorUtils.blendARGB(color, dark ? Color.WHITE : Color.BLACK, 0.2f);
color = ColorUtils.blendARGB(color,
dark ? Color.WHITE : Color.BLACK, Helper.MIN_LUMINANCE);
Paint paint = new Paint();
paint.setColor(color);
@ -76,16 +77,17 @@ class Identicon {
byte[] hash = getHash(email);
int color = Color.argb(255, hash[0], hash[1], hash[2]);
color = ColorUtils.blendARGB(color, dark ? Color.WHITE : Color.BLACK, 0.3f);
color = ColorUtils.blendARGB(color,
dark ? Color.WHITE : Color.BLACK, Helper.MIN_LUMINANCE);
Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(color);
float y = (299 * Color.red(color) + 587 * Color.green(color) + 114 * Color.blue(color)) / 1000f;
double lum = ColorUtils.calculateLuminance(color);
Paint paint = new Paint();
paint.setColor(y < 128 ? Color.WHITE : Color.BLACK);
paint.setColor(lum < 0.5 ? Color.WHITE : Color.BLACK);
paint.setTextSize(size / 2f);
paint.setTypeface(Typeface.DEFAULT_BOLD);

Loading…
Cancel
Save