fix(habits): surface impossible schedules and harden calendar edge cases
This commit is contained in:
@@ -31,12 +31,33 @@ export function scheduled(rule: Schedule, date: string): boolean {
|
||||
const monday = (n: number) => n - ((new Date(n * DAY).getUTCDay() + 6) % 7);
|
||||
return weekday === rule.weekday && (monday(day) - monday(anchor)) / 7 % rule.every === 0;
|
||||
}
|
||||
/** Each rule is a set of residues on a finite repeating period. */
|
||||
export function schedulesOverlap(a: Schedule, b: Schedule): boolean {
|
||||
const mod = (n: number, period: number) => ((n % period) + period) % period;
|
||||
function cycle(rule: Schedule): { period: number; residues: number[] } {
|
||||
if (rule.type === 'daily') return { period: 1, residues: [0] };
|
||||
// Unix day zero is Thursday (weekday 4).
|
||||
if (rule.type === 'weekdays') return { period: 7, residues: rule.days.map(day => mod(day - 4, 7)) };
|
||||
const anchor = dayNumber(rule.anchor);
|
||||
if (rule.type === 'interval') return { period: rule.every, residues: [mod(anchor, rule.every)] };
|
||||
const monday = anchor - ((new Date(anchor * DAY).getUTCDay() + 6) % 7);
|
||||
return { period: 7 * rule.every, residues: [mod(monday + (rule.weekday + 6) % 7, 7 * rule.every)] };
|
||||
}
|
||||
const left = cycle(a); const right = cycle(b);
|
||||
let x = left.period; let y = right.period;
|
||||
while (y) { const remainder = x % y; x = y; y = remainder; }
|
||||
// Congruences intersect iff their residues agree modulo the gcd. Anchors
|
||||
// only restrict the lower bound; a compatible repeating solution recurs.
|
||||
return left.residues.some(l => right.residues.some(r => mod(l - r, x) === 0));
|
||||
}
|
||||
export function shade(ratio: number | null, steps: number, settings: CalendarSettings, exactSteps: boolean) {
|
||||
if (ratio === null) return { level: null, color: settings.notDueColor };
|
||||
if (ratio <= 0) return { level: 0, color: settings.emptyColor };
|
||||
const total = steps;
|
||||
const level = exactSteps ? Math.min(total, Math.round(ratio * total)) : ratio >= 1 ? total : Math.min(total - (total > 1 ? 1 : 0), Math.max(1, Math.floor(ratio * total)));
|
||||
const intensity = total <= 1 ? 1 : 0.2 + 0.8 * (level - 1) / (total - 1);
|
||||
const color = '#' + [1, 3, 5].map(i => Math.round(255 * (1 - intensity) + parseInt(settings.mainColor.slice(i, i + 2), 16) * intensity).toString(16).padStart(2, '0')).join('');
|
||||
const luminance = (0.2126 * parseInt(settings.mainColor.slice(1, 3), 16) + 0.7152 * parseInt(settings.mainColor.slice(3, 5), 16) + 0.0722 * parseInt(settings.mainColor.slice(5, 7), 16)) / 255;
|
||||
const base = luminance > 0.75 ? 0 : 255;
|
||||
const color = '#' + [1, 3, 5].map(i => Math.round(base * (1 - intensity) + parseInt(settings.mainColor.slice(i, i + 2), 16) * intensity).toString(16).padStart(2, '0')).join('');
|
||||
return { level, color };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user