scoop:scoop.graphics:0.1.0
src/geometry/rounded.scoop
Selected production source · src/geometry/rounded.scoop
1
package scoop.graphics3
import scoop.math.min4
import scoop.math.max6
/** Four physical elliptical radii, in clockwise order from top left. */7
public struct CornerRadii(8
val topLeft: Size = Size(0.0, 0.0),9
val topRight: Size = Size(0.0, 0.0),10
val bottomRight: Size = Size(0.0, 0.0),11
val bottomLeft: Size = Size(0.0, 0.0)12
) {13
public companion object {14
public fun all(radius: Double): CornerRadii = all(Size(radius, radius))15
public fun all(radius: Size): CornerRadii = CornerRadii(radius, radius, radius, radius)16
}17
/** Opposing sums use one factor so the whole outline retains its proportions. */18
public fun normalized(size: Size): CornerRadii {19
checkSize(size)20
val tl = cornerSize(topLeft)21
val tr = cornerSize(topRight)22
val br = cornerSize(bottomRight)23
val bl = cornerSize(bottomLeft)24
val scale =25
min(26
min(27
cornerScale(size.width, tl.width + tr.width),28
cornerScale(size.width, bl.width + br.width)29
),30
min(31
cornerScale(size.height, tl.height + bl.height),32
cornerScale(size.height, tr.height + br.height)33
)34
)35
return CornerRadii(36
scaleCorner(tl, scale),37
scaleCorner(tr, scale),38
scaleCorner(br, scale),39
scaleCorner(bl, scale)40
)41
}42
}44
/** Shared physical geometry for fill, border, clip, shadow and focus outlines. */45
public struct RoundedOutline(val bounds: Rect, val radii: CornerRadii = CornerRadii()) {46
public fun normalized(): RoundedOutline {47
checkRect(bounds)48
return RoundedOutline(bounds, radii.normalized(bounds.size))49
}50
public fun inset(left: Double, top: Double, right: Double, bottom: Double): RoundedOutline {51
val outer = normalized()52
val x = outlineInset(bounds.width, left, right)53
val y = outlineInset(bounds.height, top, bottom)54
val inner = Rect(bounds.x + x.before, bounds.y + y.before, x.remaining, y.remaining)55
val corners = outer.radii56
return RoundedOutline(57
inner,58
CornerRadii(59
insetCorner(corners.topLeft, x.before, y.before),60
insetCorner(corners.topRight, x.after, y.before),61
insetCorner(corners.bottomRight, x.after, y.after),62
insetCorner(corners.bottomLeft, x.before, y.after)63
)64
).normalized()65
}66
public fun expanded(amount: Double): RoundedOutline {67
requireGraphics(amount.isFinite() && amount >= 0.0, "invalid outline outset")68
val outer = normalized()69
val box = outer.bounds70
val corners = outer.radii71
return RoundedOutline(72
Rect(73
box.x - amount,74
box.y - amount,75
box.width + amount * 2.0,76
box.height + amount * 2.077
),78
CornerRadii(79
expandCorner(corners.topLeft, amount),80
expandCorner(corners.topRight, amount),81
expandCorner(corners.bottomRight, amount),82
expandCorner(corners.bottomLeft, amount)83
)84
).normalized()85
}86
}88
private fun cornerSize(value: Size): Size {89
checkSize(value)90
return if (value.width == 0.0 || value.height == 0.0)91
Size(0.0, 0.0)92
else93
value94
}96
private fun scaleCorner(value: Size, scale: Double): Size =97
Size(value.width * scale, value.height * scale)99
private fun cornerScale(edge: Double, sum: Double): Double =100
if (sum > edge)101
edge / sum102
else103
1.0105
private fun insetCorner(value: Size, x: Double, y: Double): Size =106
Size(max(0.0, value.width - x), max(0.0, value.height - y))108
private fun expandCorner(value: Size, amount: Double): Size =109
Size(value.width + amount, value.height + amount)111
private struct OutlineInset(val before: Double, val after: Double, val remaining: Double)113
private fun outlineInset(length: Double, before: Double, after: Double): OutlineInset {114
requireGraphics(115
before.isFinite() && after.isFinite() && before >= 0.0 && after >= 0.0,116
"invalid outline insets"117
)118
if (before <= length && after <= length - before)119
return OutlineInset(before, after, length - before - after)120
val maximum = max(before, after)121
val ratio =122
if (maximum > 0.0)123
(before / maximum) / (before / maximum + after / maximum)124
else125
0.0126
val leading = length * ratio127
return OutlineInset(leading, length - leading, 0.0)128
}130
internal fun cornerPoint(value: Size): Point = Point(value.width, value.height)