diff --git a/grade.mts b/analyze/analysis.mts similarity index 79% rename from grade.mts rename to analyze/analysis.mts index 09482fd..da6d053 100755 --- a/grade.mts +++ b/analyze/analysis.mts @@ -19,13 +19,11 @@ export class ImageAnalysisData { this.chromaData = [] this.hueData = [] - for (let i = 0; i < 1; i += lightnessStep) { - i = roundToStep(i, lightnessStep) // mitigate floating point precision errors + for (let i = 0; i < 100; i += lightnessStep) { this.lightnessData.push({ lightness: i, paths: [] }) } - for (let i = 0; i < 1; i += chromaStep) { - i = roundToStep(i, chromaStep) + for (let i = 0; i < 100; i += chromaStep) { this.chromaData.push({ chroma: i, paths: [] }) } for (let i = 0; i < 360; i += hueStep) { @@ -34,32 +32,31 @@ export class ImageAnalysisData { } } -export function setupConfig(configDir: string) { +export async function setupConfig(configDir: string) { const defaultConfig : Config = { imageDir: `${os.homedir()}/Pictures`, - lightnessStep: 0.1, - chromaStep: 0.01, + lightnessStep: 10, + chromaStep: 1, hueStep: 90, preAnalysisCommands: [] } - fs.writeFile(`${configDir}/config.jsonc`, JSON.stringify(defaultConfig)) - console.info(`Generated default config at ${configDir}/config.jsonc!`) - return defaultConfig + await fs.writeFile(`${configDir}/config.json`, JSON.stringify(defaultConfig)) + console.info(`Generated default config at ${configDir}/config.json!`) + return JSON.stringify(defaultConfig) } -export function runPreAnalysis(hooks: string[]) { - hooks.forEach(async (hook) => { - await $`${hook}` // Scary! - }) +export async function runPreAnalysis(hooks: string[]) { + for await (const hook of hooks) { + await $`eval ${hook}` // Scary! + } } export async function analyzeImages(imageDir: string, analysisOutput: ImageAnalysisData, outputPath: string, lightnessStep: number, chromaStep: number, hueStep: number) { const images = await fs.promises.opendir(`${imageDir}`) - // await fs.rm(`${}/cache.json`) for await (const image of images) { - const path = imageDir + image.name + const path = `${imageDir}/${image.name}` const stat = await fs.promises.stat(path) if (stat.isFile()) { let output = @@ -104,8 +101,8 @@ export async function analyzeImages(imageDir: string, analysisOutput: ImageAnaly console.log( `Got oklch(${dominantColour}) as dominant colour of ${image.name}!`, ) - const lightness = Number(dominantColour[0]) - const chroma = Number(dominantColour[1]) + const lightness = Number(dominantColour[0]) * 100 + const chroma = Number(dominantColour[1]) * 100 const hue = Number(dominantColour[2]) analysisOutput.lightnessData.forEach((n) => { @@ -133,7 +130,7 @@ export async function analyzeImages(imageDir: string, analysisOutput: ImageAnaly }) // flush every time we finish analyzing a file, because it just takes so long - fs.writeFile(outputPath, JSON.stringify(analysisOutput), (err: Error) => { + fs.writeFile(outputPath, JSON.stringify(analysisOutput, null, 4), (err: Error) => { if (err) throw new Error(err.message) }) } @@ -142,8 +139,4 @@ export async function analyzeImages(imageDir: string, analysisOutput: ImageAnaly function floorToStep(num: number, step: number) { return Math.floor(num / step) * step -} - -function roundToStep(num: number, step: number) { - return Math.round(num / step) * step } \ No newline at end of file diff --git a/analyze/index.mts b/analyze/index.mts new file mode 100755 index 0000000..90142cd --- /dev/null +++ b/analyze/index.mts @@ -0,0 +1,49 @@ +#!/usr/bin/env zx +/// + +import { analyzeImages, Config, ImageAnalysisData, runPreAnalysis, setupConfig } from "./analysis.mts"; + +const configDir = process.env.XDG_CONFIG_HOME ? `${process.env.XDG_CONFIG_HOME}/rainwall` : `${os.homedir()}/.config/rainwall` +const cacheDir = process.env.XDG_CACHE_HOME ? `${process.env.XDG_CACHE_HOME}/rainwall` : `${os.homedir()}/.cache/rainwall` + +if (!fs.statSync(configDir)){ + fs.mkdirSync(configDir); +} + +if (!fs.statSync(cacheDir)){ + fs.mkdirSync(cacheDir); +} + +let config: Config | undefined + +console.debug(`Trying to load config from ${configDir}/analyze-config.json...`) + +let fsConfig: string | undefined + +try { + fsConfig = await fs.promises.readFile(`${configDir}/analyze-config.json`, { encoding: 'utf8' }) +} catch (err) { + console.warn(`Got ${(err as Error).message} when trying to load config, generating a new one... you may need to stop this process and edit the config file.`) + fsConfig = await setupConfig(configDir) +} finally { + if (fsConfig == undefined) { + console.warn(`Current config file blank, generating a new one... you may need to stop this process and edit the config file.`) + fsConfig = await setupConfig(configDir) + } + config = JSON.parse(fsConfig) + console.info(`Loaded config from ${configDir}/analyze-config.json!`) +} + +if (config == undefined) { + throw new Error("Couldn't initialize config!") +} + +console.debug(`Config: \n${JSON.stringify(config, null, 4)}`) + +fs.rm(`${cacheDir}/analysis.json`, { force: true }) + +const imageData = new ImageAnalysisData(config.lightnessStep, config.chromaStep, config.hueStep) + +await runPreAnalysis(config.preAnalysisCommands) + +await analyzeImages(config.imageDir, imageData, `${cacheDir}/analysis.json`, config.lightnessStep, config.chromaStep, config.hueStep) \ No newline at end of file diff --git a/index.mts b/index.mts deleted file mode 100755 index 9c6db6e..0000000 --- a/index.mts +++ /dev/null @@ -1,32 +0,0 @@ -#!/usr/bin/env zx -/// - -import { analyzeImages, Config, ImageAnalysisData, runPreAnalysis, setupConfig } from "./grade.mts"; - -const configDir = process.env.XDG_CONFIG_HOME ? `${process.env.XDG_CONFIG_HOME}/rainwall` : `${os.homedir()}/.config/rainwall` -const cacheDir = process.env.XDG_CACHE_HOME ? `${process.env.XDG_CACHE_HOME}/rainwall` : `${os.homedir()}/.cache/rainwall` - -fs.mkdir(configDir) -fs.mkdir(cacheDir) - -let config : Config | undefined - - -fs.readFile(`${configDir}/config.jsonc`, (err: Error, data: string) => { - if (err) { - console.warn(`Got ${err.message} when trying to load config, generating a new one...`) - config = setupConfig(configDir) - } else { - config = JSON.parse(data) - } -}) - -if (config == undefined) { - throw new Error("Couldn't initialize config!") -} - -let imageData = new ImageAnalysisData(config.lightnessStep, config.chromaStep, config.hueStep) - -runPreAnalysis(config.preAnalysisCommands) - -await analyzeImages(config.imageDir, imageData, `${cacheDir}/cache.json`, config.lightnessStep, config.chromaStep, config.hueStep) \ No newline at end of file