]> git.donarmstrong.com Git - lilypond.git/blob - input/new/coloring-notes-depending-on-their-pitch.ly
Merge master into nested-bookparts
[lilypond.git] / input / new / coloring-notes-depending-on-their-pitch.ly
1 \version "2.11.61"
2 \header {
3   lsrtags = "pitches,editorial-annotations,really-cool"
4   texidoc = "
5 It is possible to color note heads depending on their pitch and/or their names:
6 the function used in this example even makes it possible to distinguish enharmonics.
7 "
8   doctitle = "Coloring notes depending on their pitch"
9 }
10
11 %LSR Thanks a LOT to Damian leGassick, Steven Weber and Jay Anderson for this snippet
12
13 %Association list of pitches to colors.
14 #(define color-mapping
15  (list
16    (cons (ly:make-pitch 0 0 0) (x11-color 'red))
17    (cons (ly:make-pitch 0 0 1/2) (x11-color 'green))
18    (cons (ly:make-pitch 0 1 -1/2) (x11-color 'green))
19    (cons (ly:make-pitch 0 2 0) (x11-color 'red))
20    (cons (ly:make-pitch 0 2 1/2) (x11-color 'green))
21    (cons (ly:make-pitch 0 3 -1/2) (x11-color 'red))
22    (cons (ly:make-pitch 0 3 0) (x11-color 'green))
23    (cons (ly:make-pitch 0 4 1/2) (x11-color 'red))
24    (cons (ly:make-pitch 0 5 0) (x11-color 'green))
25    (cons (ly:make-pitch 0 5 -1/2) (x11-color 'red))
26    (cons (ly:make-pitch 0 6 1/2) (x11-color 'red))
27    (cons (ly:make-pitch 0 1 0) (x11-color 'blue))
28    (cons (ly:make-pitch 0 3 1/2) (x11-color 'blue))
29    (cons (ly:make-pitch 0 4 -1/2) (x11-color 'blue))
30    (cons (ly:make-pitch 0 5 1/2) (x11-color 'blue))
31    (cons (ly:make-pitch 0 6 -1/2) (x11-color 'blue))
32    ))
33
34 %Compare pitch and alteration (not octave).
35 #(define (pitch-equals? p1 p2)
36  (and (= (ly:pitch-alteration p1) (ly:pitch-alteration p2))
37       (= (ly:pitch-notename p1) (ly:pitch-notename p2))))
38
39 #(define (pitch-to-color pitch)
40  (let ((color (assoc pitch color-mapping pitch-equals?)))
41    (if color (cdr color))))
42
43 #(define (color-notehead grob)
44  (pitch-to-color (ly:event-property (ly:grob-property grob 'cause) 'pitch)))
45
46
47 \score {
48  \new Staff \relative c' {
49    \override NoteHead #'color = #color-notehead
50    c8 b d dis ees f g aes
51  }
52 }