-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from holbrookdev/store-color-code-in-url
- Loading branch information
Showing
6 changed files
with
65 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
export function isHex(tokens: string[]) { | ||
return !tokens?.length ? false : true; | ||
} | ||
|
||
export function getHexTokensFromString(value: string) { | ||
let tokens = value.match( | ||
/^#*([a-f0-9]{1})([a-f0-9]{1})([a-f0-9]{1})$|^#*([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})$|^#*([a-f0-9]{1})([a-f0-9]{1})([a-f0-9]{1})([a-f0-9]{1})$|^#*([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})$/i | ||
); | ||
tokens ??= []; | ||
tokens &&= tokens.slice(1).filter((v) => !!v); // remove complete match and null matches | ||
return tokens; | ||
} | ||
|
||
export function getRgbaFromHex(tokens: string[]) { | ||
let [r, g, b, alpha] = tokens!.map((v: string) => | ||
v.length === 1 ? parseInt(v + v, 16) : parseInt(v, 16) | ||
); | ||
alpha = alpha === undefined ? 1 : alpha / 255; | ||
return [r, g, b, alpha]; | ||
} |