Scoop API

scoop:scoop.graphics:0.1.0

src/geometry/rounded.scoop

Selected production source · src/geometry/rounded.scoop

1package scoop.graphics
3import scoop.math.min
4import scoop.math.max
6/** Four physical elliptical radii, in clockwise order from top left. */
7public 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 }
44/** Shared physical geometry for fill, border, clip, shadow and focus outlines. */
45public 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.radii
56 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.bounds
70 val corners = outer.radii
71 return RoundedOutline(
72 Rect(
73 box.x - amount,
74 box.y - amount,
75 box.width + amount * 2.0,
76 box.height + amount * 2.0
77 ),
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 }
88private 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 else
93 value
96private fun scaleCorner(value: Size, scale: Double): Size =
97 Size(value.width * scale, value.height * scale)
99private fun cornerScale(edge: Double, sum: Double): Double =
100 if (sum > edge)
101 edge / sum
102 else
103 1.0
105private fun insetCorner(value: Size, x: Double, y: Double): Size =
106 Size(max(0.0, value.width - x), max(0.0, value.height - y))
108private fun expandCorner(value: Size, amount: Double): Size =
109 Size(value.width + amount, value.height + amount)
111private struct OutlineInset(val before: Double, val after: Double, val remaining: Double)
113private 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 else
125 0.0
126 val leading = length * ratio
127 return OutlineInset(leading, length - leading, 0.0)
130internal fun cornerPoint(value: Size): Point = Point(value.width, value.height)