]> git.donarmstrong.com Git - lilypond.git/blob - Documentation/snippets/coloring-notes-depending-on-their-pitch.ly
Imported Upstream version 2.14.2
[lilypond.git] / Documentation / snippets / coloring-notes-depending-on-their-pitch.ly
1 %% DO NOT EDIT this file manually; it is automatically
2 %% generated from LSR http://lsr.dsi.unimi.it
3 %% Make any changes in LSR itself, or in Documentation/snippets/new/ ,
4 %% and then run scripts/auxiliar/makelsr.py
5 %%
6 %% This file is in the public domain.
7 \version "2.14.0"
8
9 \header {
10   lsrtags = "pitches, editorial-annotations"
11
12   texidoc = "
13 It is possible to color note heads depending on their pitch and/or
14 their names: the function used in this example even makes it possible
15 to distinguish enharmonics.
16
17 "
18   doctitle = "Coloring notes depending on their pitch"
19 } % begin verbatim
20
21 %Association list of pitches to colors.
22 #(define color-mapping
23    (list
24     (cons (ly:make-pitch 0 0 NATURAL) (x11-color 'red))
25     (cons (ly:make-pitch 0 0 SHARP) (x11-color 'green))
26     (cons (ly:make-pitch 0 1 FLAT) (x11-color 'green))
27     (cons (ly:make-pitch 0 2 NATURAL) (x11-color 'red))
28     (cons (ly:make-pitch 0 2 SHARP) (x11-color 'green))
29     (cons (ly:make-pitch 0 3 FLAT) (x11-color 'red))
30     (cons (ly:make-pitch 0 3 NATURAL) (x11-color 'green))
31     (cons (ly:make-pitch 0 4 SHARP) (x11-color 'red))
32     (cons (ly:make-pitch 0 5 NATURAL) (x11-color 'green))
33     (cons (ly:make-pitch 0 5 FLAT) (x11-color 'red))
34     (cons (ly:make-pitch 0 6 SHARP) (x11-color 'red))
35     (cons (ly:make-pitch 0 1 NATURAL) (x11-color 'blue))
36     (cons (ly:make-pitch 0 3 SHARP) (x11-color 'blue))
37     (cons (ly:make-pitch 0 4 FLAT) (x11-color 'blue))
38     (cons (ly:make-pitch 0 5 SHARP) (x11-color 'blue))
39     (cons (ly:make-pitch 0 6 FLAT) (x11-color 'blue))))
40
41 %Compare pitch and alteration (not octave).
42 #(define (pitch-equals? p1 p2)
43    (and
44     (= (ly:pitch-alteration p1) (ly:pitch-alteration p2))
45     (= (ly:pitch-notename p1) (ly:pitch-notename p2))))
46
47 #(define (pitch-to-color pitch)
48    (let ((color (assoc pitch color-mapping pitch-equals?)))
49      (if color
50          (cdr color))))
51
52 #(define (color-notehead grob)
53    (pitch-to-color
54     (ly:event-property (event-cause grob) 'pitch)))
55
56 \score {
57   \new Staff \relative c' {
58     \override NoteHead #'color = #color-notehead
59     c8 b d dis ees f g aes
60   }
61 }
62