///| Shared drawing helpers for components. Pure functions over the WIT types. ///| pub fn color(r : Int, g : Int, b : Int, a : Int) -> @types.Color { { r: r.to_byte(), g: g.to_byte(), b: b.to_byte(), a: a.to_byte() } } ///| fn chan(b : Byte) -> Int { b.to_int() } ///| Linear interpolation of two colours; `num/den` is the weight of `b`. pub fn lerp_color( a : @types.Color, b : @types.Color, num : Int, den : Int, ) -> @types.Color { guard den > 0 else { abort("lerp_color: den must be positive") } let n = if num < 0 { 0 } else if num > den { den } else { num } fn mix(x : Byte, y : Byte, n : Int, den : Int) -> Int { (chan(x) * (den - n) + chan(y) * n) / den } color( mix(a.r, b.r, n, den), mix(a.g, b.g, n, den), mix(a.b, b.b, n, den), mix(a.a, b.a, n, den), ) } ///| pub fn with_alpha(c : @types.Color, a : Int) -> @types.Color { color(chan(c.r), chan(c.g), chan(c.b), a) } ///| pub fn rect(x : Int, y : Int, w : Int, h : Int) -> @types.Rect { { x, y, w: w.reinterpret_as_uint(), h: h.reinterpret_as_uint() } } ///| pub fn size(w : Int, h : Int) -> @types.Size { { w: w.reinterpret_as_uint(), h: h.reinterpret_as_uint() } } ///| pub fn rect_w(r : @types.Rect) -> Int { r.w.reinterpret_as_int() } ///| pub fn rect_h(r : @types.Rect) -> Int { r.h.reinterpret_as_int() } ///| pub fn contains(r : @types.Rect, p : @types.Point) -> Bool { p.x >= r.x && p.y >= r.y && p.x < r.x + rect_w(r) && p.y < r.y + rect_h(r) } ///| pub fn fill_rect( r : @types.Rect, c : @types.Color, radius~ : Int = 0, ) -> @types.DrawOp { @types.DrawOp::FillRect({ rect: r, color: c, radius: radius.reinterpret_as_uint() }) } ///| pub fn stroke_rect( r : @types.Rect, c : @types.Color, width~ : Int = 1, radius~ : Int = 0, ) -> @types.DrawOp { @types.DrawOp::StrokeRect({ rect: r, color: c, width: width.reinterpret_as_uint(), radius: radius.reinterpret_as_uint(), }) } ///| pub fn line( x0 : Int, y0 : Int, x1 : Int, y1 : Int, c : @types.Color, width~ : Int = 1, ) -> @types.DrawOp { @types.DrawOp::Line({ start: { x: x0, y: y0 }, end: { x: x1, y: y1 }, color: c, width: width.reinterpret_as_uint(), }) } ///| pub fn circle( cx : Int, cy : Int, radius : Int, c : @types.Color, filled~ : Bool = true, ) -> @types.DrawOp { @types.DrawOp::Circle({ center: { x: cx, y: cy }, radius: radius.reinterpret_as_uint(), color: c, filled, }) } ///| Draw `s` with `origin` at the vertical middle of the line box. pub fn text( x : Int, y : Int, s : String, size : Int, c : @types.Color, align~ : @types.TextAlign = LEFT, bold~ : Bool = false, ) -> @types.DrawOp { @types.DrawOp::Text({ origin: { x, y }, text: s, style: { size: size.reinterpret_as_uint(), color: c, align, bold }, }) } ///| The text-metrics contract of the PoC host: every glyph advances by /// `ceil(0.6 * size)` pixels (monospace). Replaced by a real shaping /// interface in a later milestone. pub fn text_width(s : String, size : Int) -> Int { s.length() * ((size * 6 + 9) / 10) } ///| test "lerp_color endpoints and midpoint" { let a = color(0, 0, 0, 255) let b = color(200, 100, 50, 255) assert_eq(lerp_color(a, b, 0, 10).r.to_int(), 0) assert_eq(lerp_color(a, b, 10, 10).r.to_int(), 200) assert_eq(lerp_color(a, b, 5, 10).g.to_int(), 50) assert_eq(lerp_color(a, b, 99, 10).b.to_int(), 50) } ///| test "contains is half-open" { let r = rect(10, 10, 5, 5) assert_true(contains(r, { x: 10, y: 10 })) assert_true(contains(r, { x: 14, y: 14 })) assert_false(contains(r, { x: 15, y: 10 })) assert_false(contains(r, { x: 9, y: 10 })) } ///| test "text_width follows the 0.6em contract" { assert_eq(text_width("12:30", 20), 60) assert_eq(text_width("", 20), 0) }