simplify the resolution aspect ratio calculations

This commit is contained in:
tibbi 2018-07-10 10:58:58 +02:00
parent 18909040c4
commit 4f1bfc3e2a
1 changed files with 13 additions and 50 deletions

View File

@ -2,66 +2,28 @@ package com.simplemobiletools.camera.models
import android.content.Context import android.content.Context
import com.simplemobiletools.camera.R import com.simplemobiletools.camera.R
import com.simplemobiletools.camera.helpers.RATIO_TOLERANCE
data class MySize(val width: Int, val height: Int) { data class MySize(val width: Int, val height: Int) {
fun isSixteenToNine(): Boolean { val ratio = width / height.toFloat()
val selectedRatio = Math.abs(width / height.toFloat()) fun isSixteenToNine() = ratio == 16 / 9f
val checkedRatio = 16 / 9.toFloat()
val diff = Math.abs(selectedRatio - checkedRatio)
return diff < RATIO_TOLERANCE
}
private fun isFiveToThree(): Boolean { private fun isFiveToThree() = ratio == 5 / 3f
val selectedRatio = Math.abs(width / height.toFloat())
val checkedRatio = 5 / 3.toFloat()
val diff = Math.abs(selectedRatio - checkedRatio)
return diff < RATIO_TOLERANCE
}
private fun isFourToThree(): Boolean { private fun isFourToThree() = ratio == 4 / 3f
val selectedRatio = Math.abs(width / height.toFloat())
val checkedRatio = 4 / 3.toFloat()
val diff = Math.abs(selectedRatio - checkedRatio)
return diff < RATIO_TOLERANCE
}
private fun isThreeToFour(): Boolean { private fun isTwoToOne() = ratio == 2f
val selectedRatio = Math.abs(width / height.toFloat())
val checkedRatio = 3 / 4.toFloat()
val diff = Math.abs(selectedRatio - checkedRatio)
return diff < RATIO_TOLERANCE
}
private fun isThreeToTwo(): Boolean { private fun isThreeToFour() = ratio == 3 / 4f
val selectedRatio = Math.abs(width / height.toFloat())
val checkedRatio = 3 / 2.toFloat()
val diff = Math.abs(selectedRatio - checkedRatio)
return diff < RATIO_TOLERANCE
}
private fun isSixToFive(): Boolean { private fun isThreeToTwo() = ratio == 3 / 2f
val selectedRatio = Math.abs(width / height.toFloat())
val checkedRatio = 6 / 5.toFloat()
val diff = Math.abs(selectedRatio - checkedRatio)
return diff < RATIO_TOLERANCE
}
private fun isNineteenToNine(): Boolean { private fun isSixToFive() = ratio == 6 / 5f
val selectedRatio = Math.abs(width / height.toFloat())
val checkedRatio = 19 / 9.toFloat()
val diff = Math.abs(selectedRatio - checkedRatio)
return diff < RATIO_TOLERANCE
}
private fun isNineteenToEight(): Boolean { private fun isNineteenToNine() = ratio == 19 / 9f
val selectedRatio = Math.abs(width / height.toFloat())
val checkedRatio = 19 / 8.toFloat()
val diff = Math.abs(selectedRatio - checkedRatio)
return diff < RATIO_TOLERANCE
}
private fun isOneNineToOne() = Math.abs(1.9 - (width / height.toFloat())) < RATIO_TOLERANCE private fun isNineteenToEight() = ratio == 19 / 8f
private fun isOneNineToOne() = ratio == 1.9f
private fun isSquare() = width == height private fun isSquare() = width == height
@ -76,6 +38,7 @@ data class MySize(val width: Int, val height: Int) {
isNineteenToNine() -> "19:9" isNineteenToNine() -> "19:9"
isNineteenToEight() -> "19:8" isNineteenToEight() -> "19:8"
isSquare() -> "1:1" isSquare() -> "1:1"
isTwoToOne() -> "2:1"
else -> context.resources.getString(R.string.other) else -> context.resources.getString(R.string.other)
} }
} }