Don't use too much bright or dark feeds colors

This commit is contained in:
Shinokuni 2020-07-25 14:33:32 +02:00
parent 7314440534
commit dee7f6e9ad
3 changed files with 39 additions and 5 deletions

View File

@ -117,4 +117,20 @@ public final class Utils {
return bitmap;
}
public static boolean isColorTooBright(@ColorInt int color) {
return getColorLuma(color) > 210;
}
public static boolean isColorTooDark(@ColorInt int color) {
return getColorLuma(color) < 40;
}
private static double getColorLuma(@ColorInt int color) {
int r = (color >> 16) & 0xff;
int g = (color >> 8) & 0xff;
int b = (color >> 0) & 0xff;
return 0.2126 * r + 0.7152 * g + 0.0722 * b;
}
}

View File

@ -1,9 +1,9 @@
package com.readrops.app.utils.feedscolors
import androidx.palette.graphics.Palette
import com.readrops.db.entities.Feed
import com.readrops.app.utils.HtmlParser
import com.readrops.app.utils.Utils
import com.readrops.db.entities.Feed
fun setFeedColors(feed: Feed) {
getFaviconLink(feed)
@ -13,12 +13,17 @@ fun setFeedColors(feed: Feed) {
val palette = Palette.from(bitmap).generate()
val dominantSwatch = palette.dominantSwatch
if (dominantSwatch != null)
feed.textColor = dominantSwatch.rgb
feed.textColor = if (dominantSwatch != null && !Utils.isColorTooBright(dominantSwatch.rgb)
&& !Utils.isColorTooDark(dominantSwatch.rgb)) {
dominantSwatch.rgb
} else 0
val mutedSwatch = palette.mutedSwatch
if (mutedSwatch != null)
feed.backgroundColor = mutedSwatch.rgb
feed.backgroundColor = if (mutedSwatch != null && !Utils.isColorTooBright(mutedSwatch.rgb)
&& !Utils.isColorTooDark(mutedSwatch.rgb)) {
mutedSwatch.rgb
} else 0
}
}

View File

@ -1,9 +1,12 @@
package com.readrops.app;
import android.graphics.Color;
import com.readrops.app.utils.Utils;
import org.junit.Test;
import static junit.framework.Assert.assertTrue;
import static junit.framework.TestCase.assertEquals;
public class UtilsTest {
@ -14,4 +17,14 @@ public class UtilsTest {
assertEquals("This is a text to clean", Utils.cleanText(text));
}
@Test
public void colorTooBrightTest() {
assertTrue(Utils.isColorTooBright(-986896));
}
@Test
public void colorTooDarkTest() {
assertTrue(Utils.isColorTooDark(Color.parseColor("#1a1a1a")));
}
}