Coloring Pitches
code
d3
April 01, 2020
Let's play with colors. Vizualization loves color. To map musical notes to color, we can do various things.
rainbow with hsl
One simple approach is using the hsl color, dividing the 360 degrees through 12 pitches:
import { TinyColor } from '@ctrl/tinycolor';
import { Note } from '@tonaljs/tonal';
export function noteColor(note: string, rotate = 0): TinyColor {
return new TinyColor({
h: Math.floor((rotate + (Note.chroma(note) / 12) * 360) % 360),
s: 100,
l: 50,
});
}
If we throw that into the svg-piano:
import { Range } from '@tonaljs/tonal';
export function ColorKeyboard({ options, colorizer = noteColor }) {
options = { range: ['C3', 'C4'], ...options };
return (
<Keyboard
options={{
...options,
colorize: Range.chromatic(options.range).map((note) => ({
keys: [note],
color: colorizer(note).toHexString(),
})),
}}
/>
);
}
If this is too aggressive for you, read on..
Using d3-scale-chromatic
After just scratching the surface of color research, I found out that there is a universe of color nerds out there, most of them coming from the data viz world. The best color scales for this purpose I found so far are the cyclical scales of d3-scale-chromatic.
sinebow
Less angry rainbow
I like this one the most
Note height as lightening factor
angry rainbow
sinebow
less angry rainbow
Next Steps
Thats enough for today. More ideas for the future:
- colorize based on the circle of fifths instead of chromatically
- mix chord colors out of individual note colors
- use non cyclic color scales for things like interval "spice" or voicing diff
Links from the colorsphere
- https://krazydad.com/tutorials/makecolors.php
- http://basecase.org/env/on-rainbows
- https://mycarta.wordpress.com/2013/02/21/perceptual-rainbow-palette-the-method/
- https://github.com/gka/chroma.js
- https://github.com/d3/d3-color
- https://bl.ocks.org/mbostock/310c99e53880faec2434
- https://bl.ocks.org/mbostock/310c99e53880faec2434
- http://whitneymusicbox.org/