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