///| Moon-phase "toy" widget: a pure example plasmoid (tokens + date in, ops out) /// with two interchangeable renderings, so the shell can offer them as /// alternatives of one another. ///| Julian day number of a Gregorian calendar date (noon). pub fn julian_day(year : Int, month : Int, day : Int) -> Int { let a = (14 - month) / 12 let y = year + 4800 - a let m = month + 12 * a - 3 day + (153 * m + 2) / 5 + 365 * y + y / 4 - y / 100 + y / 400 - 32045 } ///| Phase in [0, 1): 0 = new moon, 0.25 = first quarter, 0.5 = full, 0.75 = last quarter. /// Mean synodic month from the new moon of 2000-01-06; good to about a day. pub fn phase(year : Int, month : Int, day : Int) -> Double { let days = (julian_day(year, month, day) - 2451550).to_double() - 0.1 let p = days / 29.530588853 let f = p - p.floor() if f < 0.0 { f + 1.0 } else { f } } ///| Percent of the disc that is lit, 0..100. pub fn illumination(p : Double) -> Int { ((1.0 - @math.cos(2.0 * @math.pi * p)) / 2.0 * 100.0).round().to_int() } ///| pub fn phase_name(p : Double) -> String { let i = ((p * 8.0) + 0.5).floor().to_int() % 8 ["New Moon", "Waxing Crescent", "First Quarter", "Waxing Gibbous", "Full Moon", "Waning Gibbous", "Last Quarter", "Waning Crescent"][i] } ///| pub fn disc_info() -> (String, String, Int, Int, Int) { // id, name, preferred w, preferred h, min w ("org.chandra.moon.disc", "Moon Phase (disc)", 36, 36, 36) } ///| pub fn text_info() -> (String, String, Int, Int, Int) { ("org.chandra.moon.text", "Moon Phase (text)", 110, 36, 90) } ///| The moon drawn as a disc with its lit part shaded row by row. pub fn render_disc( size : @types.Size, t : @theme.Tokens, p : Double, ) -> Array[@types.DrawOp] { let w = size.w.reinterpret_as_int() let h = size.h.reinterpret_as_int() let r = (if w < h { w } else { h }) / 2 - 4 let cx = w / 2 let cy = h / 2 let lit = @ui.color(244, 236, 206, 255) let shade = @ui.lerp_color(t.palette.surface_hover, @ui.color(10, 14, 28, 255), 1, 2) // Glow grows with illumination, then the dark side (faint earthshine). let glow = 10 + illumination(p) / 4 let ops = [ @ui.circle(cx, cy, r + 3, @ui.with_alpha(lit, glow / 2)), @ui.circle(cx, cy, r + 2, @ui.with_alpha(lit, glow)), @ui.circle(cx, cy, r, shade), ] let k = @math.cos(2.0 * @math.pi * p) let waxing = p < 0.5 // Lit area as 1px rows with fractional end pixels, so both the limb and the terminator are smooth. for dy = -r; dy <= r; dy = dy + 1 { let hw = (r * r - dy * dy).to_double().sqrt() let term = hw * k let (x0, x1) = if waxing { (term, hw) } else { (-hw, -term) } if x1 - x0 > 0.05 { let xl = x0.floor().to_int() let xr = x1.ceil().to_int() for x = xl; x < xr; x = x + 1 { let a = (if x1 < (x + 1).to_double() { x1 } else { (x + 1).to_double() }) - (if x0 > x.to_double() { x0 } else { x.to_double() }) if a > 0.04 { // Slight shading toward the limb gives the disc some roundness. let edge = (x.to_double() + 0.5).abs() / (r.to_double() + 0.001) let tone = @ui.lerp_color(lit, @ui.color(214, 204, 172, 255), (edge * 100.0).to_int(), 220) ops.push(@ui.fill_rect(@ui.rect(cx + x, cy + dy, 1, 1), @ui.with_alpha(tone, (a * 255.0).to_int()))) } } } } ops.push(@ui.circle(cx, cy, r, @ui.with_alpha(t.palette.border, 160), filled=false)) ops } ///| Phase name over illumination percent. pub fn render_text( size : @types.Size, t : @theme.Tokens, p : Double, ) -> Array[@types.DrawOp] { let w = size.w.reinterpret_as_int() let h = size.h.reinterpret_as_int() let fs = t.font_size.reinterpret_as_int() [ @ui.text(w / 2, h / 2 - fs / 2 + 1, phase_name(p), fs - 2, t.palette.foreground, align=CENTER, bold=true), @ui.text(w / 2, h / 2 + fs / 2 + 4, illumination(p).to_string() + "% lit", fs - 3, t.palette.foreground_muted, align=CENTER), ] } ///| test "known dates" { // New moon 2000-01-06, full moon 2000-01-21, new moon 2024-01-11. assert_true(phase(2000, 1, 6) < 0.05 || phase(2000, 1, 6) > 0.95) assert_eq(phase_name(phase(2000, 1, 21)), "Full Moon") assert_eq(illumination(phase(2024, 1, 11)) < 5, true) assert_eq(julian_day(2000, 1, 1), 2451545) } ///| test "illumination extremes" { assert_eq(illumination(0.0), 0) assert_eq(illumination(0.5), 100) assert_eq(illumination(0.25), 50) }