Skip to content

Commit

Permalink
Make roundToRealPixels round to 0 pixels on very small inputs
Browse files Browse the repository at this point in the history
GitHub: #130
  • Loading branch information
RedEpicness authored Nov 28, 2023
1 parent 3f43410 commit 22db718
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/main/kotlin/gg/essential/elementa/utils/extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import gg.essential.universal.UResolution
import gg.essential.universal.shader.BlendState
import gg.essential.universal.shader.UShader
import java.awt.Color
import kotlin.math.abs
import kotlin.math.round
import kotlin.math.sign

Expand All @@ -14,11 +15,11 @@ fun Double.guiHint(roundDown: Boolean) = UIComponent.guiHint(this, roundDown)

fun Float.roundToRealPixels(): Float {
val factor = UResolution.scaleFactor.toFloat()
return round(this * factor).let { if (it == 0f && this != 0f) sign(this) else it } / factor
return round(this * factor).let { if (it == 0f && abs(this) > 0.001f) sign(this) else it } / factor
}
fun Double.roundToRealPixels(): Double {
val factor = UResolution.scaleFactor
return round(this * factor).let { if (it == 0.0 && this != 0.0) sign(this) else it } / factor
return round(this * factor).let { if (it == 0.0 && abs(this) > 0.001) sign(this) else it } / factor
}

fun Color.withAlpha(alpha: Int) = Color(this.red, this.green, this.blue, alpha)
Expand Down

0 comments on commit 22db718

Please sign in to comment.