From 25f65f212c210638d15d73c0c8dfe1fc08bd96a2 Mon Sep 17 00:00:00 2001 From: Andinus Date: Wed, 12 Jan 2022 12:42:38 +0530 Subject: Add visualize feature using Cairo This takes parts from Fornax: https://github.com/andinus/fornax Each step is visualized and a video solution is generated. --- lib/Octans/GenerateFrame.rakumod | 63 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 lib/Octans/GenerateFrame.rakumod (limited to 'lib/Octans/GenerateFrame.rakumod') diff --git a/lib/Octans/GenerateFrame.rakumod b/lib/Octans/GenerateFrame.rakumod new file mode 100644 index 0000000..ba7fc5b --- /dev/null +++ b/lib/Octans/GenerateFrame.rakumod @@ -0,0 +1,63 @@ +# This module has been adapted from Fornax::GenerateFrame. + +use Cairo; +use Octans::Hex2RGB; + +# Colors. +constant %C = ( + bg-main => "#ffffff", + + red-subtle-bg => "#f2b0a2", + blue-subtle-bg => "#b5d0ff", + cyan-subtle-bg => "#c0efff", + green-subtle-bg => "#aecf90", + + fg-main => "#000000", + + fg-special-cold => "#093060", + fg-special-warm => "#5d3026", + fg-special-mild => "#184034", + fg-special-calm => "#61284f", +).map: {.key => hex2rgb(.value)}; + +enum IterStatus ; + +sub generate-frame( + :%canvas, :$out, :$side, :@puzzle, :@visited, :%meta, :$found +) is export { + given Cairo::Image.create( + Cairo::FORMAT_ARGB32, %canvas, %canvas + ) { + given Cairo::Context.new($_) { + # Paint the entire canvas white. + .rgb: |%C; + .rectangle(0, 0, %canvas, %canvas); + .fill; + + # This seems to be slower than creating an intermediate + # variable and assigning from that. Difference is not much + # so we'll ignore it. + for ^%meta X ^%meta -> ($r, $c) { + my Int @target = $c * $side, $r * $side, + $side, $side; + .rectangle: |@target; + + if @visited[$r][$c] { + .rgba: |%C, 0.72; + .rgba: |%C, 0.84 if $found; + .fill :preserve; + } + + .select_font_face("Mono", Cairo::FONT_SLANT_NORMAL, Cairo::FONT_WEIGHT_NORMAL); + .set_font_size(72.0); + .move_to($c * $side + 32, ($r + 1) * $side - 28); + + .rgb: |%C; + .show_text: @puzzle[$r][$c].uc; + .stroke; + } + } + .write_png($out); + .finish; + } +} -- cgit 1.4.1-2-gfad0