Scoop API

scoop:scoop.surface:0.1.0

src/shared/animation/vector.scoop

Selected production source · src/shared/animation/vector.scoop

1package scoop.surface
3import scoop.graphics.*
4import scoop.math.abs
5import scoop.math.max
6import scoop.math.min
8/** Four scalar lanes cover the built-in scalar, geometry and RGBA animation values. */
9public struct AnimationVector(
10 val x: Double = 0.0,
11 val y: Double = 0.0,
12 val z: Double = 0.0,
13 val w: Double = 0.0
14) {
15 internal fun add(other: AnimationVector): AnimationVector =
16 AnimationVector(x + other.x, y + other.y, z + other.z, w + other.w)
17 internal fun subtract(other: AnimationVector): AnimationVector =
18 AnimationVector(x - other.x, y - other.y, z - other.z, w - other.w)
19 internal fun scale(factor: Double): AnimationVector =
20 AnimationVector(x * factor, y * factor, z * factor, w * factor)
21 internal fun maximum(): Double = max(max(abs(x), abs(y)), max(abs(z), abs(w)))
22 internal fun check() {
23 requireAnimation(
24 x.isFinite() && y.isFinite() && z.isFinite() && w.isFinite(),
25 "animation vector overflow"
26 )
27 }
30public interface AnimationConverter<T> {
31 public fun toVector(value: T): AnimationVector
32 public fun fromVector(value: AnimationVector): T
35public object AnimationConverters {
36 public object DoubleValue: AnimationConverter<Double> {
37 public override fun toVector(value: Double): AnimationVector {
38 requireAnimation(value.isFinite(), "non-finite animation value")
39 return AnimationVector(value)
40 }
41 public override fun fromVector(value: AnimationVector): Double {
42 value.check();
43 return value.x
44 }
45 }
46 public object PointValue: AnimationConverter<Point> {
47 public override fun toVector(value: Point): AnimationVector {
48 val vector = AnimationVector(value.x, value.y)
49 vector.check()
50 return vector
51 }
52 public override fun fromVector(value: AnimationVector): Point {
53 value.check();
54 return Point(value.x, value.y)
55 }
56 }
57 public object SizeValue: AnimationConverter<Size> {
58 public override fun toVector(value: Size): AnimationVector {
59 requireAnimation(value.width >= 0.0 && value.height >= 0.0, "negative animated size")
60 val vector = AnimationVector(value.width, value.height)
61 vector.check()
62 return vector
63 }
64 public override fun fromVector(value: AnimationVector): Size {
65 value.check();
66 return Size(max(0.0, value.x), max(0.0, value.y))
67 }
68 }
69 public object ColorValue: AnimationConverter<Color> {
70 public override fun toVector(value: Color): AnimationVector {
71 val vector = AnimationVector(value.red, value.green, value.blue, value.alpha)
72 vector.check()
73 requireAnimation(
74 value.red >=
75 0.0 &&
76 value.red <=
77 1.0 &&
78 value.green >=
79 0.0 &&
80 value.green <=
81 1.0 &&
82 value.blue >=
83 0.0 &&
84 value.blue <=
85 1.0 &&
86 value.alpha >=
87 0.0 &&
88 value.alpha <=
89 1.0,
90 "invalid animated color"
91 )
92 return vector
93 }
94 public override fun fromVector(value: AnimationVector): Color {
95 value.check()
96 return Color(
97 clampColor(value.x),
98 clampColor(value.y),
99 clampColor(value.z),
100 clampColor(value.w)
101 )
102 }
103 }
106private fun clampColor(value: Double): Double = min(1.0, max(0.0, value))
108internal fun requireAnimation(condition: Boolean, message: String) {
109 if (!condition)
110 throw IllegalArgumentException(Some(message))