2019-06-03 23:25:05 +02:00
|
|
|
package org.mian.gitnex.helpers;
|
|
|
|
|
2020-06-06 22:04:07 +02:00
|
|
|
import android.graphics.Bitmap;
|
2019-06-03 23:25:05 +02:00
|
|
|
import android.graphics.Color;
|
2020-06-06 22:04:07 +02:00
|
|
|
import android.graphics.drawable.BitmapDrawable;
|
|
|
|
import android.widget.ImageView;
|
2019-06-03 23:25:05 +02:00
|
|
|
import androidx.annotation.ColorInt;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Author M M Arif
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class ColorInverter {
|
|
|
|
|
|
|
|
@ColorInt
|
|
|
|
public int getContrastColor(@ColorInt int color) {
|
|
|
|
|
|
|
|
double a = 1 - (0.299 * Color.red(color) + 0.587 * Color.green(color) + 0.114 * Color.blue(color)) / 255;
|
|
|
|
|
|
|
|
int d;
|
|
|
|
if (a < 0.5) {
|
2020-06-06 22:04:07 +02:00
|
|
|
d = 30; // almost black
|
2019-06-03 23:25:05 +02:00
|
|
|
} else {
|
|
|
|
d = 255; // white
|
|
|
|
}
|
|
|
|
|
|
|
|
return Color.rgb(d, d, d);
|
|
|
|
}
|
|
|
|
|
2020-06-06 22:04:07 +02:00
|
|
|
@ColorInt
|
|
|
|
public int getImageViewContrastColor(ImageView imageView) {
|
|
|
|
|
|
|
|
if(imageView != null) {
|
|
|
|
|
|
|
|
Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
|
2020-06-23 16:54:27 +02:00
|
|
|
|
|
|
|
int colorSum = 0;
|
|
|
|
int divisionValue = 0;
|
|
|
|
|
|
|
|
for(int height=0; height<bitmap.getHeight(); height += 10) {
|
|
|
|
|
|
|
|
for(int width=0; width<bitmap.getWidth(); width += 10) {
|
|
|
|
|
|
|
|
colorSum += bitmap.getPixel(width, height);
|
|
|
|
divisionValue++;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate average color
|
|
|
|
return getContrastColor(colorSum / divisionValue);
|
2020-06-06 22:04:07 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
return Color.rgb(255, 255, 255);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-06-03 23:25:05 +02:00
|
|
|
}
|