Skip to content

Commit

Permalink
feature: store color code in url
Browse files Browse the repository at this point in the history
  • Loading branch information
holbrookdev committed Jul 1, 2022
1 parent 5188f3d commit f078bcc
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/components/addressBar/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import state, { SLIDER_MODES } from "../../state";
import { oklabToOkhsl, oklabToOkhsv, sRgbToOklab } from "../../utils/color";
import { isHex, getHexTokensFromString, getRgbaFromHex } from "../../utils/hex";
import { updateSliderRangeInputs } from "../sliders/sliderRangeInputs";

export default function initAddressBar() {
var hash = window.location.hash.substring(1);
let hex = getHexTokensFromString(hash);

if (isHex(hex)) {
let [r, g, b, alpha] = getRgbaFromHex(hex);

const fn =
state.sliderMode === SLIDER_MODES.OKHSV ? oklabToOkhsv : oklabToOkhsl;

const color = fn(...sRgbToOklab(r, g, b, alpha));
const values = color.map((v, i: number) => v * state.currentSliders[i].max);
updateSliderRangeInputs(values);
}
}
6 changes: 6 additions & 0 deletions src/components/sliders/sliderRangeInputs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { updateSliderTextInput } from "../sliderTextInputs";
import { okhsvToOklab, okhslToOklab } from "../../../utils/color";
import { updateHexInput } from "../../hexInput";
import { updateColors } from "../../../utils/dom";
import debounce from "../.././../utils/debounce";

export const sliderRangeLabelEls = <NodeListOf<HTMLLabelElement>>(
document.querySelectorAll(".sliderLabel")
Expand Down Expand Up @@ -83,13 +84,18 @@ export function getValue(value: number, key: number) {
: value;
}

const updateAddressBar = debounce((hex: string) => {
window.location.hash = hex;
}, 300);

function handleSync(skipHexInput: boolean = false) {
const oklab = getOklab();
const hex = oklabToHex(oklab);
if (!skipHexInput) {
updateHexInput(hex);
}
updateDomColors(hex, oklab);
updateAddressBar(hex);
}

function handleAsync() {}
Expand Down
2 changes: 2 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import initActionBar from "./components/actionBar";
import initSliders from "./components/sliders";
import initPWA from "./pwa";
import initGitHubButton from "./components/githubButton";
import initAddressBar from "./components/addressBar";

initSliders();
initHexInput();
initActionBar();
initAddressBar();
initPWA();
initGitHubButton();
13 changes: 13 additions & 0 deletions src/utils/debounce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export default function debounce(fn: Function, delay = 300, immediate = false) {
let timeout: number | undefined;
return function (this: any, ...args: any) {
function delayed(this: any) {
timeout = undefined;
if (!immediate) fn.apply(this, args);
}
const callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(delayed, delay);
if (callNow) fn.apply(this, args);
};
}

0 comments on commit f078bcc

Please sign in to comment.