input TEX;

outputtemplate := "%j-%c.ps";

beginfig(1);
    u := 1.4mm;

    numeric N[], white_width, white_up, black_width, black_up, between_black;

    N[0] = 0;
    N[1] = 52;
    white_width = 2.3u;
    black_width = 1.2u;
    between_black_width = 1.3u;
    white_up = 13.7u;
    black_up = 8.7u;

    numeric black_down;
    black_down = white_up - black_up;

    draw (0, 0) -- (N[1] * white_width, 0) -- (N[1] * white_width, white_up) -- (0, white_up) -- cycle;
    for i := 1 upto N[1] - 1:
        draw (i * white_width, 0) -- (i * white_width, white_up);
        numeric key_index, black_group_size, black_group_center, black_group_center_note, black_group_center, black_group_index, black_group_start_note_before, note, black_key_center;
        key_index = N[0] + i;
        note = key_index mod 7;
        if (note >= 3) and (note <= 4):
            black_group_size = 2;
            black_group_start_note_before = 3;
        elseif (note >= 6) or (note <= 1):
            black_group_size = 3;
            black_group_start_note_before = 6;
        else:
            black_group_size = 0;
        fi;
        if black_group_size > 0:
            black_group_index = (note - black_group_start_note_before) mod 7;
            black_group_center_note = black_group_start_note_before + (black_group_size - 1) / 2;
            black_group_center = i - 3 + (black_group_center_note - i + 3) mod 7;
            black_key_center = black_group_center * white_width + (black_group_index - (black_group_size - 1) / 2) * (between_black_width + black_width);
            fill (black_key_center - .5black_width, black_down) -- (black_key_center - .5black_width, white_up) -- (black_key_center + .5black_width, white_up) -- (black_key_center + .5black_width, black_down) -- cycle withcolor black;
        fi;
    endfor;
    for i := 0 upto N[1] - 1:
        key_index := N[0] + i;
        note := key_index mod 7;
%        label.top(TEX(char(65 + note)), ((i + .5) * white_width, 0));
        label.top(char(65 + note), ((i + .5) * white_width, 0));
    endfor;
endfig;

beginfig(2);
    u := 1.4mm;

    numeric N[], white_width, white_up, black_width, black_up, between_black;

    N[0] = 0;
    N[1] = 52;
    white_width = 2.3u;
    black_width = 1.2u;
    between_black_width := 1.3u;
    white_up = 13.7u;
    black_up = 8.7u;

    numeric black_down;
    black_down = white_up - black_up;

    fill (21white_width, 0) -- (21white_width, white_up) -- (28white_width, white_up) -- (28white_width, 0) -- cycle withcolor .8white;
    fill (24white_width, 0) -- (24white_width, white_up) -- (25white_width, white_up) -- (25white_width, 0) -- cycle withcolor .6white;

    draw (0, 0) -- (N[1] * white_width, 0) -- (N[1] * white_width, white_up) -- (0, white_up) -- cycle;
    for i := 1 upto N[1] - 1:
        draw (i * white_width, 0) -- (i * white_width, white_up);
        numeric key_index, black_group_size, black_group_center, black_group_center_note, black_group_center, black_group_index, black_group_start_note_before, note, black_key_center;
        key_index = N[0] + i;
        note = key_index mod 7;
        if (note >= 3) and (note <= 4):
            black_group_size = 2;
            black_group_start_note_before = 3;
        elseif (note >= 6) or (note <= 1):
            black_group_size = 3;
            black_group_start_note_before = 6;
        else:
            black_group_size = 0;
        fi;
        if black_group_size > 0:
            black_group_index = (note - black_group_start_note_before) mod 7;
            black_group_center_note = black_group_start_note_before + (black_group_size - 1) / 2;
            black_group_center = i - 3 + (black_group_center_note - i + 3) mod 7;
            black_key_center = black_group_center * white_width + (black_group_index - (black_group_size - 1) / 2) * (between_black_width + black_width);
            fill (black_key_center - .5black_width, black_down) -- (black_key_center - .5black_width, white_up) -- (black_key_center + .5black_width, white_up) -- (black_key_center + .5black_width, black_down) -- cycle withcolor black;
        fi;
    endfor;
    for i := 0 upto N[1] - 1:
        key_index := N[0] + i;
        note := key_index mod 7;
%        label.top(TEX(char(65 + note)), ((i + .5) * white_width, 0));
        label.top(char(65 + note), ((i + .5) * white_width, 0));
    endfor;
endfig;

bye
