MCPcopy Index your code
hub / github.com/endbasic/endbasic / draw_logo_internal

Function draw_logo_internal

repl/src/logo.rs:23–111  ·  view source on GitHub ↗

Support logic for `draw_logo` that doesn't handle color save/restore.

(
    console: &mut dyn Console,
    x1y1: PixelsXY,
    x2y2: Option<PixelsXY>,
)

Source from the content-addressed store, hash-verified

21
22/// Support logic for `draw_logo` that doesn't handle color save/restore.
23fn draw_logo_internal(
24 console: &mut dyn Console,
25 x1y1: PixelsXY,
26 x2y2: Option<PixelsXY>,
27) -> io::Result<()> {
28 const SOURCE_W: i32 = 90;
29 const SOURCE_H: i32 = 93;
30 const SOURCE_WF: f32 = SOURCE_W as f32;
31 const SOURCE_HF: f32 = SOURCE_H as f32;
32
33 let x2y2 = x2y2.unwrap_or(PixelsXY::new(
34 x1y1.x.saturating_add(i16::try_from(SOURCE_W).unwrap()),
35 x1y1.y.saturating_add(i16::try_from(SOURCE_H).unwrap()),
36 ));
37 let x1 = i32::from(x1y1.x);
38 let y1 = i32::from(x1y1.y);
39 let x2 = i32::from(x2y2.x);
40 let y2 = i32::from(x2y2.y);
41 let (x1, x2) = if x1 <= x2 { (x1, x2) } else { (x2, x1) };
42 let (y1, y2) = if y1 <= y2 { (y1, y2) } else { (y2, y1) };
43 let target_w = x2 - x1;
44 let target_h = y2 - y1;
45 let scale = (target_w as f32 / SOURCE_WF).min(target_h as f32 / SOURCE_HF);
46 let offset_x = x1 as f32 + (target_w as f32 - SOURCE_WF * scale) / 2.0;
47 let offset_y = y1 as f32 + (target_h as f32 - SOURCE_HF * scale) / 2.0;
48
49 let tx = |x: i32| -> i16 { (offset_x + x as f32 * scale).round() as i16 };
50 let ty = |y: i32| -> i16 { (offset_y + y as f32 * scale).round() as i16 };
51 let xy = |x: i32, y: i32| PixelsXY::new(tx(x), ty(y));
52
53 // Outline.
54 console.set_color(Some(202), None)?;
55 console.draw_poly_filled(&[
56 xy(0, 0),
57 xy(9, 0),
58 xy(9, 1),
59 xy(22, 1),
60 xy(22, 0),
61 xy(69, 0),
62 xy(69, 1),
63 xy(81, 1),
64 xy(81, 0),
65 xy(85, 0),
66 xy(90, 5),
67 xy(90, 93),
68 xy(0, 93),
69 ])?;
70
71 // Bottom holes.
72 console.set_color(Some(0), None)?;
73 console.draw_rect_filled(xy(3, 83), xy(7, 86))?;
74 console.draw_rect_filled(xy(83, 83), xy(87, 86))?;
75
76 // Top indent.
77 console.set_color(Some(208), None)?;
78 console.draw_rect_filled(xy(9, 1), xy(81, 32))?;
79 console.draw_rect_filled(xy(9, 40), xy(81, 93))?;
80

Callers 1

draw_logoFunction · 0.85

Calls 5

xyFunction · 0.50
set_colorMethod · 0.45
draw_poly_filledMethod · 0.45
draw_rect_filledMethod · 0.45
draw_lineMethod · 0.45

Tested by

no test coverage detected