-
Notifications
You must be signed in to change notification settings - Fork 0
/
colorUtils.js
66 lines (55 loc) · 1.88 KB
/
colorUtils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Importing chroma.js using ES6 import syntax
import chroma from "chroma-js"
export function interpolateColor(color1, color2, factor) {
const color1Obj = chroma.hex(color1)
const color2Obj = chroma.hex(color2)
const interpolatedColor = chroma.mix(color1Obj, color2Obj, factor)
return interpolatedColor.hex()
}
export function getRandomColor() {
const letters = "0123456789ABCDEF"
let color = ""
for (let i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)]
}
return color
}
import { readConfig } from "./configUtils.js"
export async function getRandomColorFromPalette(palette) {
const config = await readConfig()
var colors
if (palette) {
colors = config.palettes.find((p) => p.name === palette).colors
} else {
colors =
config.palettes[Math.floor(Math.random() * config.palettes.length)].colors
}
return colors[Math.floor(Math.random() * config.palettes[0].colors.length)]
}
export function resolveScheduledColor(schedule) {
const now = new Date()
const currentTime = now.getHours() * 60 + now.getMinutes()
console.log(`currentTime: ${currentTime}`)
console.log(`now: ${now}`)
let selectedColor = null
let selectedTime = -1
const scheduleEntries = Object.entries(schedule).map(([time, color]) => {
const [hours, minutes] = time.split(":").map(Number)
return { time: hours * 60 + minutes, color }
})
for (const { time, color } of scheduleEntries) {
if (time <= currentTime && time > selectedTime) {
selectedTime = time
selectedColor = color
}
}
// If no color was selected, it means current time is before the first scheduled time of the day
if (selectedColor === null) {
// Find the latest time in the previous day (i.e., the highest time value in the schedule)
const lastEntry = scheduleEntries.reduce((latest, entry) => {
return entry.time > latest.time ? entry : latest
})
selectedColor = lastEntry.color
}
return selectedColor
}