about summary refs log tree commit diff stats
path: root/lib/Octans/GenerateFrame.rakumod
blob: 9fe575471093544086b8ff36897d1e2ce8bb4610 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 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)};

sub generate-frame(
    :%canvas, :$out, :$side, :@puzzle, :@visited, :%meta, :$found
) is export {
    given Cairo::Image.create(
        Cairo::FORMAT_ARGB32, %canvas<width>, %canvas<height>
    ) {
        given Cairo::Context.new($_) {
            # Paint the entire canvas white.
            .rgb: |%C<bg-main>;
            .rectangle(0, 0, %canvas<width>, %canvas<height>);
            .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<rows> X ^%meta<cols>  -> ($r, $c) {
                my Int @target = $c * $side, $r * $side,
                                 $side, $side;
                .rectangle: |@target;

                if @visited[$r][$c] {
                    .rgba: |%C<cyan-subtle-bg>, 0.72;
                    .rgba: |%C<green-subtle-bg>, 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<fg-main>;
                .show_text: @puzzle[$r][$c].uc;
                .stroke;
            }
        }
        .write_png($out);
        .finish;
    }
}