220 lines
7.9 KiB
Java
220 lines
7.9 KiB
Java
package com.discord.utilities.colors;
|
|
|
|
import android.graphics.Bitmap;
|
|
import android.graphics.Color;
|
|
import androidx.annotation.ColorInt;
|
|
import androidx.core.graphics.ColorUtils;
|
|
import c.a.g.a;
|
|
import c.a.g.b;
|
|
import c.d.b.a.a;
|
|
import d0.z.d.m;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import kotlin.coroutines.Continuation;
|
|
import kotlin.jvm.internal.DefaultConstructorMarker;
|
|
import kotlinx.coroutines.CoroutineDispatcher;
|
|
import rx.Observable;
|
|
import rx.subjects.BehaviorSubject;
|
|
import s.a.a.n;
|
|
import s.a.j0;
|
|
import s.a.w0;
|
|
/* compiled from: RepresentativeColors.kt */
|
|
public final class RepresentativeColors<T> {
|
|
private static final int BLURPLE = Color.parseColor("#5865f2");
|
|
public static final Companion Companion = new Companion(null);
|
|
private final HashMap<T, Integer> representativeColors = new HashMap<>();
|
|
private final BehaviorSubject<Map<T, Integer>> representativeColorsSubject = BehaviorSubject.k0();
|
|
|
|
/* compiled from: RepresentativeColors.kt */
|
|
public static final class Companion {
|
|
private Companion() {
|
|
}
|
|
|
|
public /* synthetic */ Companion(DefaultConstructorMarker defaultConstructorMarker) {
|
|
this();
|
|
}
|
|
}
|
|
|
|
/* compiled from: RepresentativeColors.kt */
|
|
public static abstract class RepresentativeColorResult {
|
|
|
|
/* compiled from: RepresentativeColors.kt */
|
|
public static final class Failure extends RepresentativeColorResult {
|
|
private final Exception exception;
|
|
|
|
/* JADX INFO: super call moved to the top of the method (can break code semantics) */
|
|
public Failure(Exception exc) {
|
|
super(null);
|
|
m.checkNotNullParameter(exc, "exception");
|
|
this.exception = exc;
|
|
}
|
|
|
|
public static /* synthetic */ Failure copy$default(Failure failure, Exception exc, int i, Object obj) {
|
|
if ((i & 1) != 0) {
|
|
exc = failure.exception;
|
|
}
|
|
return failure.copy(exc);
|
|
}
|
|
|
|
public final Exception component1() {
|
|
return this.exception;
|
|
}
|
|
|
|
public final Failure copy(Exception exc) {
|
|
m.checkNotNullParameter(exc, "exception");
|
|
return new Failure(exc);
|
|
}
|
|
|
|
public boolean equals(Object obj) {
|
|
if (this != obj) {
|
|
return (obj instanceof Failure) && m.areEqual(this.exception, ((Failure) obj).exception);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public final Exception getException() {
|
|
return this.exception;
|
|
}
|
|
|
|
public int hashCode() {
|
|
Exception exc = this.exception;
|
|
if (exc != null) {
|
|
return exc.hashCode();
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
public String toString() {
|
|
StringBuilder L = a.L("Failure(exception=");
|
|
L.append(this.exception);
|
|
L.append(")");
|
|
return L.toString();
|
|
}
|
|
}
|
|
|
|
/* compiled from: RepresentativeColors.kt */
|
|
public static final class Success extends RepresentativeColorResult {
|
|
private final int color;
|
|
|
|
public Success(@ColorInt int i) {
|
|
super(null);
|
|
this.color = i;
|
|
}
|
|
|
|
public static /* synthetic */ Success copy$default(Success success, int i, int i2, Object obj) {
|
|
if ((i2 & 1) != 0) {
|
|
i = success.color;
|
|
}
|
|
return success.copy(i);
|
|
}
|
|
|
|
public final int component1() {
|
|
return this.color;
|
|
}
|
|
|
|
public final Success copy(@ColorInt int i) {
|
|
return new Success(i);
|
|
}
|
|
|
|
public boolean equals(Object obj) {
|
|
if (this != obj) {
|
|
return (obj instanceof Success) && this.color == ((Success) obj).color;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public final int getColor() {
|
|
return this.color;
|
|
}
|
|
|
|
public int hashCode() {
|
|
return this.color;
|
|
}
|
|
|
|
public String toString() {
|
|
return a.z(a.L("Success(color="), this.color, ")");
|
|
}
|
|
}
|
|
|
|
private RepresentativeColorResult() {
|
|
}
|
|
|
|
public /* synthetic */ RepresentativeColorResult(DefaultConstructorMarker defaultConstructorMarker) {
|
|
this();
|
|
}
|
|
}
|
|
|
|
public static final /* synthetic */ int access$getBLURPLE$cp() {
|
|
return BLURPLE;
|
|
}
|
|
|
|
public static final /* synthetic */ RepresentativeColorResult access$getRepresentativeColor(RepresentativeColors representativeColors, Bitmap bitmap) {
|
|
return representativeColors.getRepresentativeColor(bitmap);
|
|
}
|
|
|
|
public static final /* synthetic */ HashMap access$getRepresentativeColors$p(RepresentativeColors representativeColors) {
|
|
return representativeColors.representativeColors;
|
|
}
|
|
|
|
public static final /* synthetic */ BehaviorSubject access$getRepresentativeColorsSubject$p(RepresentativeColors representativeColors) {
|
|
return representativeColors.representativeColorsSubject;
|
|
}
|
|
|
|
private final int getColorDistance(int i, int i2) {
|
|
int i3 = ((i >> 16) & 255) - ((i2 >> 16) & 255);
|
|
int i4 = ((i >> 8) & 255) - ((i2 >> 8) & 255);
|
|
int i5 = (i & 255) - (i2 & 255);
|
|
return Math.abs((i5 * i5) + (i4 * i4) + (i3 * i3));
|
|
}
|
|
|
|
private final List<Integer> getPrimaryColorsForBitmap(Bitmap bitmap) {
|
|
a.b bVar = c.a.g.a.a;
|
|
m.checkNotNullParameter(bitmap, "bitmap");
|
|
int width = bitmap.getWidth();
|
|
int height = bitmap.getHeight();
|
|
int[] iArr = new int[(width * height)];
|
|
bitmap.getPixels(iArr, 0, width, 0, 0, width, height);
|
|
return d0.t.m.listOf(Integer.valueOf(new c.a.g.a(new b(iArr), 2, null).e.get(0).d));
|
|
}
|
|
|
|
private final RepresentativeColorResult getRepresentativeColor(Bitmap bitmap) {
|
|
try {
|
|
int alphaComponent = ColorUtils.setAlphaComponent(getPrimaryColorsForBitmap(bitmap).get(0).intValue(), 255);
|
|
int i = Integer.MAX_VALUE;
|
|
int width = bitmap.getWidth();
|
|
int i2 = alphaComponent;
|
|
for (int i3 = 0; i3 < width; i3++) {
|
|
int height = bitmap.getHeight();
|
|
for (int i4 = 0; i4 < height; i4++) {
|
|
int alphaComponent2 = ColorUtils.setAlphaComponent(bitmap.getPixel(i3, i4), 255);
|
|
int colorDistance = getColorDistance(alphaComponent, alphaComponent2);
|
|
if (colorDistance < i) {
|
|
i2 = alphaComponent2;
|
|
i = colorDistance;
|
|
}
|
|
}
|
|
}
|
|
return new RepresentativeColorResult.Success(i2);
|
|
} catch (Exception e) {
|
|
return new RepresentativeColorResult.Failure(e);
|
|
}
|
|
}
|
|
|
|
public final Object getRepresentativeColorAsync(Bitmap bitmap, Continuation<? super RepresentativeColorResult> continuation) {
|
|
return c.q.a.k.a.i0(j0.a, new RepresentativeColors$getRepresentativeColorAsync$2(this, bitmap, null), continuation);
|
|
}
|
|
|
|
public final void handleBitmap(T t, Bitmap bitmap) {
|
|
m.checkNotNullParameter(bitmap, "bitmap");
|
|
w0 w0Var = w0.i;
|
|
CoroutineDispatcher coroutineDispatcher = j0.a;
|
|
c.q.a.k.a.G(w0Var, n.b, null, new RepresentativeColors$handleBitmap$1(this, t, bitmap, null), 2, null);
|
|
}
|
|
|
|
public final Observable<Integer> observeRepresentativeColor(T t) {
|
|
Observable<Integer> r = this.representativeColorsSubject.F(new RepresentativeColors$observeRepresentativeColor$1(t)).r();
|
|
m.checkNotNullExpressionValue(r, "representativeColorsSubj… .distinctUntilChanged()");
|
|
return r;
|
|
}
|
|
}
|