discord-jadx/app/src/main/java/com/discord/utilities/colors/RepresentativeColors.java

237 lines
8.4 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 b.a.g.a;
import b.a.g.b;
import b.a.g.d;
import b.d.b.a.a;
import b.i.a.f.e.o.f;
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.k0;
import s.a.x0;
/* compiled from: RepresentativeColors.kt */
/* loaded from: classes2.dex */
public final class RepresentativeColors<T> {
private final HashMap<T, Integer> representativeColors = new HashMap<>();
private final BehaviorSubject<Map<T, Integer>> representativeColorsSubject = BehaviorSubject.k0();
public static final Companion Companion = new Companion(null);
private static final int BLURPLE = Color.parseColor("#5865f2");
/* compiled from: RepresentativeColors.kt */
/* loaded from: classes2.dex */
public static final class Companion {
private Companion() {
}
public /* synthetic */ Companion(DefaultConstructorMarker defaultConstructorMarker) {
this();
}
}
/* compiled from: RepresentativeColors.kt */
/* loaded from: classes2.dex */
public static final class NoSwatchesFoundException extends RuntimeException {
}
/* compiled from: RepresentativeColors.kt */
/* loaded from: classes2.dex */
public static abstract class RepresentativeColorResult {
/* compiled from: RepresentativeColors.kt */
/* loaded from: classes2.dex */
public static final class Failure extends RepresentativeColorResult {
private final Exception exception;
/* JADX WARN: '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 R = a.R("Failure(exception=");
R.append(this.exception);
R.append(")");
return R.toString();
}
}
/* compiled from: RepresentativeColors.kt */
/* loaded from: classes2.dex */
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.A(a.R("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 = b.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);
List<d> list = new b.a.g.a(new b(iArr), 2, null).e;
if (!list.isEmpty()) {
return d0.t.m.listOf(Integer.valueOf(list.get(0).d));
}
throw new NoSwatchesFoundException();
}
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 f.C1(k0.a, new RepresentativeColors$getRepresentativeColorAsync$2(this, bitmap, null), continuation);
}
public final void handleBitmap(T t, Bitmap bitmap, String str) {
m.checkNotNullParameter(bitmap, "bitmap");
x0 x0Var = x0.j;
CoroutineDispatcher coroutineDispatcher = k0.a;
f.H0(x0Var, n.f3804b, null, new RepresentativeColors$handleBitmap$1(this, t, bitmap, str, null), 2, null);
}
public final Observable<Integer> observeRepresentativeColor(T t) {
Observable<Integer> q = this.representativeColorsSubject.F(new RepresentativeColors$observeRepresentativeColor$1(t)).q();
m.checkNotNullExpressionValue(q, "representativeColorsSubj… .distinctUntilChanged()");
return q;
}
}