diff --git a/apply/index.ts b/apply/index.ts index 3eb07e4..003076d 100644 --- a/apply/index.ts +++ b/apply/index.ts @@ -1,7 +1,7 @@ #!/usr/bin/env zx import { ImageAnalysisData } from "../analyze/analysis.ts" -import { cacheDir, configDir, loadConfig, map, mapEaseInExpo, mapEaseOutExpo } from "../utils.ts" +import { cacheDir, configDir, loadConfig, map, mapEaseOutQuint } from "../utils.ts" import { ApplicationConfig, defaultConfig, findMatchingImages, getOpenMeteoData } from "./application.ts" import * as SunCalc from "suncalc" import "zx/globals" @@ -23,28 +23,40 @@ const openMeteoData = await getOpenMeteoData( config.longitude, config.weatherModel, ) -const sunCalcData = SunCalc.getPosition( +const currentSunData = SunCalc.getPosition( new Date(), config.latitude, config.longitude, ) +const zenithSunData = SunCalc.getPosition( + SunCalc.getTimes(new Date(), config.latitude, config.longitude).solarNoon, + config.latitude, + config.longitude, +) +const nadirSunData = SunCalc.getPosition( + SunCalc.getTimes(new Date(), config.latitude, config.longitude).nadir, + config.latitude, + config.longitude, +) console.info(`Current cloud cover percentage is ${openMeteoData.cloudCover}%!`) console.info( `Current shortwave radiation is ${openMeteoData.shortwaveRadiation} W/m²!`, ) -console.info(`Current sun altitude is ${sunCalcData.altitude}°!`) +console.info(`Current sun altitude is ${currentSunData.altitude}°!`) -const hueValue = Number(sunCalcData.altitude) >= 0 - ? mapEaseOutExpo(Number(sunCalcData.altitude), 0, 90, 0, 264) - : mapEaseInExpo(Number(sunCalcData.altitude), 0, -90, 360, 264) +const hueValue = Number(currentSunData.altitude) >= -0.833 + ? mapEaseOutQuint(Number(currentSunData.altitude), -0.833, zenithSunData.altitude, 0, 264) + : mapEaseOutQuint(Number(currentSunData.altitude), nadirSunData.altitude, -0.833, 264, 360) + const chromaValue = map( - Number(openMeteoData.cloudCover), + 100 - Number(openMeteoData.cloudCover), // gotta invert this one 0, 100, - config.chromaRange.end, config.chromaRange.start, + config.chromaRange.end, ) + const lightnessValue = map( Number(openMeteoData.shortwaveRadiation), 0, @@ -64,6 +76,6 @@ console.debug(`Opened cache file ${pathToCache}!`) const targetFile = findMatchingImages(imagesData, hueValue, chromaValue, lightnessValue) -console.info(`Found target image ${targetFile.path}!`) console.info(`Setting wallpaper...`) await $`eval ${config.applyWallpaperCommand.replace("%s", targetFile.path)}` +console.info(`Success! Enjoy :)`) diff --git a/deno.json b/deno.json index e22fc59..7bd9097 100644 --- a/deno.json +++ b/deno.json @@ -2,8 +2,8 @@ "tasks": { "compile-analyze": "deno compile --output build/analyze -A analyze/index.ts", "compile-apply": "deno compile --output build/apply -A apply/index.ts", - "analyze": "deno run analyze/index.ts", - "apply": "deno run apply/index.ts" + "analyze": "deno run -A analyze/index.ts", + "apply": "deno run -A apply/index.ts" }, "imports": { "@std/assert": "jsr:@std/assert@1" diff --git a/utils.ts b/utils.ts index ebf6b7a..cf086e5 100644 --- a/utils.ts +++ b/utils.ts @@ -56,18 +56,10 @@ export function map(number: number, min: number, max: number, newMin: number, ne return ((number - min) / (max - min)) * (newMax - newMin) + newMin } -export function mapEaseInExpo(number: number, min: number, max: number, newMin: number, newMax: number) { - return easeInExpo((number - min) / (max - min)) * (newMax - newMin) + newMin +function easeOutQuint(x: number): number { + return 1 - Math.pow(1 - x, 5) } -export function mapEaseOutExpo(number: number, min: number, max: number, newMin: number, newMax: number) { - return easeOutExpo((number - min) / (max - min)) * (newMax - newMin) + newMin -} - -function easeInExpo(x: number): number { - return x === 0 ? 0 : Math.pow(2, 10 * x - 10) -} - -function easeOutExpo(x: number): number { - return x === 1 ? 1 : 1 - Math.pow(2, -10 * x) +export function mapEaseOutQuint(number: number, min: number, max: number, newMin: number, newMax: number) { + return easeOutQuint((number - min) / (max - min)) * (newMax - newMin) + newMin }