-
Notifications
You must be signed in to change notification settings - Fork 0
/
wallpaperUtils.js
94 lines (81 loc) · 2.69 KB
/
wallpaperUtils.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import fs from "fs"
import os from "os"
import path from "path"
import { setWallpaper } from "wallpaper"
import { interpolateColor } from "./colorUtils.js"
import { readConfig } from "./configUtils.js"
export async function runPinwheel(palette, duration, forever = false) {
const config = await readConfig()
const colors = config.palettes.find((p) => p.name === palette).colors
do {
for (var i = 0; i < colors.length; i++) {
const color = colors[i]
await transitionToColor(color, duration)
}
} while (forever)
}
export async function transitionToColor(endColor, duration) {
const startColor = await readCurrentColor()
if (!startColor) {
console.log("No start color found, ignoring transition")
duration = 0
}
if (startColor === endColor) {
console.log(`No transition needed from ${startColor} to ${endColor}`)
return
}
if (!duration) {
console.log(`Instant transition to ${endColor}`)
const imagePath = await createSolidColorImage(endColor)
await setWallpaper(imagePath)
await writeCurrentColor(endColor)
console.log("Transition complete")
return
}
console.log(`Starting transition from ${startColor} to ${endColor}`)
const minInterval = 100 // Minimum interval between steps in milliseconds
const steps = Math.max(1, Math.floor(duration / minInterval))
const interval = duration / steps
console.log(
`Starting transition from ${startColor} to ${endColor} over ${duration}ms with ${steps} steps`
)
for (let i = 0; i < steps; i++) {
const factor = i / steps
const intermediateColor = interpolateColor(startColor, endColor, factor)
console.log(`Step ${i}: Color ${intermediateColor}`)
const imagePath = await createSolidColorImage(intermediateColor)
await setWallpaper(imagePath)
await new Promise((resolve) => setTimeout(resolve, interval))
}
await writeCurrentColor(endColor)
console.log("Transition complete")
}
const currentColorPath = path.join(os.homedir(), ".wallslappercurrent")
export async function readCurrentColor() {
try {
if (fs.existsSync(currentColorPath)) {
const data = await fs.promises.readFile(currentColorPath, "utf8")
return data.trim()
} else {
console.warn(`${currentColorPath} not found`)
return
}
} catch (error) {
console.error("Error reading current color:", error)
return
}
}
export async function writeCurrentColor(color) {
try {
await fs.promises.writeFile(currentColorPath, color, "utf8")
} catch (error) {
console.error("Error writing current color:", error)
}
}
import Jimp from "jimp"
export async function createSolidColorImage(color) {
const image = new Jimp(256, 256, color)
const imagePath = path.join(os.tmpdir(), `solid_color_${Date.now()}.png`)
await image.writeAsync(imagePath)
return imagePath
}