Coloring Music Sheet
alphaTab supports individual styling of music notation elements via a special style container which is available on
most levels of the Data Model. This means you can customize the colors of individual notes being rendered
base on custom logic.
The best place to apply custom coloring is the api.scoreLoaded event which is triggered whenever
a new score is detected. But you can modify the styles later at any time by accessing the score and then
initiating a re-render via render.
alphaTab does not detect dynamically any changes made to the data model. Rendering happens in a background worker, and calling render will ensure
all updated information is passed to this renderer and the display is updated. Calling render will initiate a full re-layout and drawing of
the music sheet to apply any style changes accordingly, hence it is a quite performance intense operation.
The example codes in this Guide are in JavaScript but can be adapted to the other platforms accordingly.
Quick Startโ
This code example here should give you a good first idea how you apply custom colors to elements. Depending on the elements you want to color,
you have to traverse the tree of elements. Then on the style container you can fill the colors with the particular elements
const api = new alphaTab.AlphaTabApi(...);
api.scoreLoaded.on(score => applyColors(score));
function applyColors(score) {
// create custom style on score level
score.style = new alphaTab.model.ScoreStyle();
score.style.colors.set(
alphaTab.model.ScoreSubElement.Title,
alphaTab.model.Color.fromJson("#426d9d")
);
score.style.colors.set(
alphaTab.model.ScoreSubElement.SubTitle,
alphaTab.model.Color.fromJson("#4cb3d4")
);
const fretColors = {
3: alphaTab.model.Color.fromJson("#d13a3a"),
4: alphaTab.model.Color.fromJson("#e17722"),
5: alphaTab.model.Color.fromJson("#e1a90e"),
7: alphaTab.model.Color.fromJson("#6abd45"),
8: alphaTab.model.Color.fromJson("#70ccd6"),
10: alphaTab.model.Color.fromJson("#3953a5"),
12: alphaTab.model.Color.fromJson("#ab519f"),
13: alphaTab.model.Color.fromJson("#d047a3")
};
// traverse hierarchy and apply colors as desired
for (const track of score.tracks) {
for (const staff of track.staves) {
for (const bar of staff.bars) {
for (const voice of bar.voices) {
for (const beat of voice.beats) {
// on tuplets colors beam and tuplet bracket
if (beat.hasTuplet) {
beat.style = new alphaTab.model.BeatStyle();
const color = alphaTab.model.Color.fromJson("#00DD00");
beat.style.colors.set(
alphaTab.model.BeatSubElement.StandardNotationTuplet,
color
);
beat.style.colors.set(
alphaTab.model.BeatSubElement.StandardNotationBeams,
color
);
}
for (const note of beat.notes) {
// color notes based on the fret
note.style = new alphaTab.model.NoteStyle();
note.style.colors.set(alphaTab.model.NoteSubElement.StandardNotationNoteHead,
fretColors[note.fret]
);
note.style.colors.set(alphaTab.model.NoteSubElement.GuitarTabFretNumber,
fretColors[note.fret]
);
}
}
}
}
}
}
}
Styleable elementsโ
alphaTab tries to give fine grained access to coloring music notation elements. Only when it comes to individual effects and annotations things get more coarse.
Score levelโ
On Score level initialize the score.style = new alphaTab.model.ScoreStyle() and then select the element to color via alphaTab.model.ScoreSubElement.
Lists all graphical sub elements within a Score which can be styled via style
Enum Membersโ
Track levelโ
On Track level initialize the track.style = new alphaTab.model.TrackStyle() and then select the element to color via alphaTab.model.TrackSubElement.
Lists all graphical sub elements within a Track which can be styled via style
Enum Membersโ
Bar levelโ
On Bar level initialize the bar.style = new alphaTab.model.BarStyle() and then select the element to color via alphaTab.model.BarSubElement.
Lists all graphical sub elements within a Bar which can be styled via style
Enum Membersโ
Voice levelโ
On Bar level initialize the voice.style = new alphaTab.model.VoiceStyle() and then select the element to color via alphaTab.model.VoiceSubElement.
Lists all graphical sub elements within a Voice which can be styled via style
Enum Membersโ
| Name | Numeric Value | Description |
|---|---|---|
Glyphs | 0 | All general glyphs (like notes heads and rests). |
Beat levelโ
On Beat level initialize the beat.style = new alphaTab.model.BeatStyle() and then select the element to color via alphaTab.model.BeatSubElement.
Lists all graphical sub elements within a Beat which can be styled via style
Enum Membersโ
Note levelโ
On Note level initialize the note.style = new alphaTab.model.NoteStyle() and then select the element to color via alphaTab.model.NoteSubElement.
Lists all graphical sub elements within a Note which can be styled via style