Scoop API

scoop:scoop.graphics:0.1.0

src/geometry/values.scoop

Selected production source · src/geometry/values.scoop

1package scoop.graphics
3import scoop.math.*
5/** A point in logical coordinates, with positive y directed downward. */
6@CLayout
7public struct Point(val x: Double, val y: Double)
9/** A nonnegative logical size, checked at operation boundaries. */
10public struct Size(val width: Double, val height: Double)
12/** A logical rectangle. Parent relationships do not imply clipping to this rectangle. */
13@CLayout
14public struct Rect(val x: Double, val y: Double, val width: Double, val height: Double) {
15 public val origin: Point get() = Point(x, y)
16 public val size: Size get() = Size(width, height)
17 public fun contains(point: Point): Boolean {
18 checkRect(this)
19 checkPoint(point)
20 return point.x >= x && point.x < x + width && point.y >= y && point.y < y + height
21 }
24/** Maps (x,y) to (a*x+c*y+tx,b*x+d*y+ty). */
25@CLayout
26public struct Transform(
27 val a: Double = 1.0,
28 val b: Double = 0.0,
29 val c: Double = 0.0,
30 val d: Double = 1.0,
31 val tx: Double = 0.0,
32 val ty: Double = 0.0
33) {
34 public fun map(point: Point): Point {
35 checkTransform(this)
36 checkPoint(point)
37 val result = Point(a * point.x + c * point.y + tx, b * point.x + d * point.y + ty)
38 checkPoint(result)
39 return result
40 }
41 /** Applies this transform, followed by other. */
42 public fun then(other: Transform): Transform {
43 checkTransform(this)
44 checkTransform(other)
45 val result =
46 Transform(
47 other.a * a + other.c * b,
48 other.b * a + other.d * b,
49 other.a * c + other.c * d,
50 other.b * c + other.d * d,
51 other.a * tx + other.c * ty + other.tx,
52 other.b * tx + other.d * ty + other.ty
53 )
54 checkTransform(result)
55 return result
56 }
57 public fun inverse(): Transform? {
58 checkTransform(this)
59 val determinant = a * d - b * c
60 if (determinant == 0.0)
61 return None
62 val result =
63 Transform(
64 d / determinant,
65 -b / determinant,
66 -c / determinant,
67 a / determinant,
68 (c * ty - d * tx) / determinant,
69 (b * tx - a * ty) / determinant
70 )
71 if (!finiteTransform(result))
72 return None
73 return Some(result)
74 }
75 public companion object {
76 public fun translation(x: Double, y: Double): Transform = Transform(tx = x, ty = y)
77 public fun scale(x: Double, y: Double): Transform = Transform(a = x, d = y)
78 /** Angles are radians; positive rotation is clockwise in the logical coordinate system. */
79 public fun rotation(radians: Double): Transform {
80 requireGraphics(radians.isFinite(), "non-finite angle")
81 val s = sin(radians)
82 val c = cos(radians)
83 return Transform(c, s, -s, c)
84 }
85 }
88/** Straight sRGB components in 0..1; surfaces use premultiplied alpha internally. */
89@CLayout
90public struct Color(val red: Double, val green: Double, val blue: Double, val alpha: Double = 1.0) {
91 public fun withAlpha(value: Double): Color = Color(red, green, blue, value)
92 public companion object {
93 public val Transparent: Color get() = Color(0.0, 0.0, 0.0, 0.0)
94 public val Black: Color get() = Color(0.0, 0.0, 0.0)
95 public val White: Color get() = Color(1.0, 1.0, 1.0)
96 }
99internal fun requireGraphics(condition: Boolean, message: String) {
100 if (!condition)
101 throw IllegalArgumentException(Some(message))
104internal fun checkScalar(value: Double) {
105 requireGraphics(value.isFinite() && value.toFloat().isFinite(), "graphics scalar overflow")
108internal fun checkPoint(point: Point) {
109 checkScalar(point.x);
110 checkScalar(point.y)
113internal fun checkSize(size: Size) {
114 checkScalar(size.width);
115 checkScalar(size.height)
116 requireGraphics(size.width >= 0.0 && size.height >= 0.0, "negative graphics size")
119internal fun checkRect(rect: Rect) {
120 checkPoint(rect.origin);
121 checkSize(rect.size)
122 checkScalar(rect.x + rect.width);
123 checkScalar(rect.y + rect.height)
126internal fun finiteTransform(value: Transform): Boolean =
127 value.a.isFinite() &&
128 value.b.isFinite() &&
129 value.c.isFinite() &&
130 value.d.isFinite() &&
131 value.tx.isFinite() &&
132 value.ty.isFinite()
134internal fun checkTransform(value: Transform) {
135 requireGraphics(finiteTransform(value), "non-finite transform")
138internal fun checkColor(value: Color) {
139 for (component in [value.red, value.green, value.blue, value.alpha])
140 requireGraphics(
141 component.isFinite() && component >= 0.0 && component <= 1.0,
142 "invalid sRGB color"
143 )