mirror of
https://github.com/sparrowwallet/sparrow.git
synced 2025-11-05 11:56:37 +00:00
fix selection of nearest supported resolution where chosen resolution is not available
This commit is contained in:
parent
33e043fd9a
commit
cec7eac9ac
1 changed files with 21 additions and 4 deletions
|
|
@ -444,10 +444,27 @@ public class WebcamService extends ScheduledService<Image> {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T extends Enum<T>> T getNearestEnum(T target, T[] values) {
|
public static <T extends Enum<T>> T getNearestEnum(T target, T[] values) {
|
||||||
int ordinal = target.ordinal();
|
if(values == null || values.length == 0) {
|
||||||
return Stream.concat(ordinal > 0 ? Stream.of(values[ordinal - 1]) : Stream.empty(), ordinal < values.length - 1 ? Stream.of(values[ordinal + 1]) : Stream.empty())
|
return null;
|
||||||
.findFirst()
|
}
|
||||||
.orElse(null);
|
|
||||||
|
int targetOrdinal = target.ordinal();
|
||||||
|
if(values.length == 1) {
|
||||||
|
return values[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int i = 0; i < values.length; i++) {
|
||||||
|
if(targetOrdinal < values[i].ordinal()) {
|
||||||
|
if(i == 0) {
|
||||||
|
return values[0];
|
||||||
|
}
|
||||||
|
int diffToPrev = Math.abs(targetOrdinal - values[i - 1].ordinal());
|
||||||
|
int diffToNext = Math.abs(targetOrdinal - values[i].ordinal());
|
||||||
|
return diffToPrev <= diffToNext ? values[i - 1] : values[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return values[values.length - 1];
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class CroppedDimension {
|
private static class CroppedDimension {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue