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