better color support

This commit is contained in:
Felix Roos 2022-12-26 22:13:41 +01:00
parent b8f0a1dd82
commit 36f837730a
4 changed files with 214 additions and 2 deletions

View file

@ -5,6 +5,7 @@ This program is free software: you can redistribute it and/or modify it under th
*/
import { Pattern, toMidi, getDrawContext, freqToMidi } from './index.mjs';
import { convertColorToNumber } from './color.mjs';
const scale = (normalized, min, max) => normalized * (max - min) + min;
const getValue = (e) => {
@ -20,6 +21,9 @@ const getValue = (e) => {
if (typeof value === 'string') {
value = toMidi(value);
}
if (typeof value === 'object' && value.color) {
return convertColorToNumber(value.color);
}
return value;
};
@ -236,8 +240,9 @@ export function pianoroll({
// .filter(inFrame)
.forEach((event) => {
const isActive = event.whole.begin <= time && event.whole.end > time;
ctx.fillStyle = event.context?.color || inactive;
ctx.strokeStyle = event.context?.color || active;
const color = event.value?.color || event.context?.color;
ctx.fillStyle = color || inactive;
ctx.strokeStyle = color || active;
ctx.globalAlpha = event.context.velocity ?? 1;
const timePx = scale((event.whole.begin - (flipTime ? to : from)) / timeExtent, ...timeRange);
let durationPx = scale(event.duration / timeExtent, 0, timeAxis);