From 35ed0223e7bb0551d1d69c6daa2ef0cd4e9a2a78 Mon Sep 17 00:00:00 2001 From: Valentin Villenave Date: Thu, 16 Oct 2008 21:46:56 +0200 Subject: [PATCH] New snippet from a previous discussion on the list (... aaand the LSR is down again...) --- ...coloring-notes-depending-on-their-pitch.ly | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 input/new/coloring-notes-depending-on-their-pitch.ly diff --git a/input/new/coloring-notes-depending-on-their-pitch.ly b/input/new/coloring-notes-depending-on-their-pitch.ly new file mode 100644 index 0000000000..dd29c94f77 --- /dev/null +++ b/input/new/coloring-notes-depending-on-their-pitch.ly @@ -0,0 +1,52 @@ +\version "2.11.61" +\header { + lsrtags = "pitches,editorial-annotations,really-cool" + texidoc = " +It is possible to color note heads depending on their pitch and/or their names: +the function used in this example even makes it possible to distinguish enharmonics. +" + doctitle = "Coloring notes depending on their pitch" +} + +%LSR Thanks a LOT to Damian leGassick, Steven Weber and Jay Anderson for this snippet + +%Association list of pitches to colors. +#(define color-mapping + (list + (cons (ly:make-pitch 0 0 0) (x11-color 'red)) + (cons (ly:make-pitch 0 0 1/2) (x11-color 'green)) + (cons (ly:make-pitch 0 1 -1/2) (x11-color 'green)) + (cons (ly:make-pitch 0 2 0) (x11-color 'red)) + (cons (ly:make-pitch 0 2 1/2) (x11-color 'green)) + (cons (ly:make-pitch 0 3 -1/2) (x11-color 'red)) + (cons (ly:make-pitch 0 3 0) (x11-color 'green)) + (cons (ly:make-pitch 0 4 1/2) (x11-color 'red)) + (cons (ly:make-pitch 0 5 0) (x11-color 'green)) + (cons (ly:make-pitch 0 5 -1/2) (x11-color 'red)) + (cons (ly:make-pitch 0 6 1/2) (x11-color 'red)) + (cons (ly:make-pitch 0 1 0) (x11-color 'blue)) + (cons (ly:make-pitch 0 3 1/2) (x11-color 'blue)) + (cons (ly:make-pitch 0 4 -1/2) (x11-color 'blue)) + (cons (ly:make-pitch 0 5 1/2) (x11-color 'blue)) + (cons (ly:make-pitch 0 6 -1/2) (x11-color 'blue)) + )) + +%Compare pitch and alteration (not octave). +#(define (pitch-equals? p1 p2) + (and (= (ly:pitch-alteration p1) (ly:pitch-alteration p2)) + (= (ly:pitch-notename p1) (ly:pitch-notename p2)))) + +#(define (pitch-to-color pitch) + (let ((color (assoc pitch color-mapping pitch-equals?))) + (if color (cdr color)))) + +#(define (color-notehead grob) + (pitch-to-color (ly:event-property (ly:grob-property grob 'cause) 'pitch))) + + +\score { + \new Staff \relative c' { + \override NoteHead #'color = #color-notehead + c8 b d dis ees f g aes + } +} -- 2.39.2