diff --git a/src/Color.js b/src/Color.js new file mode 100644 index 0000000..29a2cff --- /dev/null +++ b/src/Color.js @@ -0,0 +1,23 @@ +import FastAverageColor from 'fast-average-color' + +var fac = new FastAverageColor(), + def = document.documentElement.style.getPropertyValue('--deg') + +function setColor(target, lvl) { + fac + .getColorAsync(target) + .then(({ value: [r, g, b] }) => { + let color = + Math.atan2(1.732050808 * (g - b), 2 * r - g - b) * 57.295779513 + + return color + }) + .catch((err) => { + return def + }) + .then((color) => { + document.documentElement.style.setProperty('--deg', color + 'deg') + }) +} + +export { setColor } diff --git a/src/components/Status/Status.js b/src/components/Status/Status.js index b0b6db0..d6a56e3 100644 --- a/src/components/Status/Status.js +++ b/src/components/Status/Status.js @@ -1,11 +1,13 @@ import { useContext, useEffect, useState } from 'react' import { Link } from 'react-router-dom' +import { setColor } from '../../Color' + import PlayingContext from '../../contexts/Playing' import './Status.css' -function Status({ progress, paused, onEnd }) { +function Status({ progress, paused }) { const { id, title = '-', @@ -18,6 +20,10 @@ function Status({ progress, paused, onEnd }) { const step = 500, tread = step / duration + useEffect(() => { + if (album.art) setColor(album.art, 1) + }, [album.art]) + useEffect(() => { setElapsed(progress ? progress / duration : 0) }, [progress, duration]) diff --git a/src/index.css b/src/index.css index c02e785..4088e73 100644 --- a/src/index.css +++ b/src/index.css @@ -8,7 +8,7 @@ body { } :root { - --deg: 200deg; + --deg: 0deg; --white: hsl(0, 0%, 100%); --light: hsl(var(--deg) 50% 88%); --main: hsl(var(--deg), 57%, 52%);