aboutsummaryrefslogtreecommitdiff
path: root/src/Utils.ts
diff options
context:
space:
mode:
authorbobzel <zzzman@gmail.com>2021-01-11 11:33:46 -0500
committerbobzel <zzzman@gmail.com>2021-01-11 11:33:46 -0500
commit6c1857193aa51ccfec081aaa4372de164e152355 (patch)
tree8ae584d0c0a262df63153fec097f32ab9ef80441 /src/Utils.ts
parent768ab508529ab2b7f43a44592e5dc2c9f15a68a5 (diff)
parente80e0e3938f1f7c5f740553eb5cb7b152f2598e8 (diff)
Merge branch 'master' into ink_edits
Diffstat (limited to 'src/Utils.ts')
-rw-r--r--src/Utils.ts51
1 files changed, 51 insertions, 0 deletions
diff --git a/src/Utils.ts b/src/Utils.ts
index daacca51d..f160df6f7 100644
--- a/src/Utils.ts
+++ b/src/Utils.ts
@@ -549,6 +549,57 @@ export function simulateMouseClick(element: Element | null | undefined, x: numbe
}));
}
+export function lightOrDark(color: any) {
+
+ // Variables for red, green, blue values
+ var r, g, b, hsp;
+
+ // Check the format of the color, HEX or RGB?
+ if (color.match(/^rgb/)) {
+
+ // If RGB --> store the red, green, blue values in separate variables
+ color = color.match(/^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d+(?:\.\d+)?))?\)$/);
+
+ r = color[1];
+ g = color[2];
+ b = color[3];
+ }
+ else {
+
+ // If hex --> Convert it to RGB: http://gist.github.com/983661
+ color = +("0x" + color.slice(1).replace(
+ color.length < 5 && /./g, '$&$&'));
+
+ r = color >> 16;
+ g = color >> 8 & 255;
+ b = color & 255;
+ }
+
+ // HSP (Highly Sensitive Poo) equation from http://alienryderflex.com/hsp.html
+ hsp = Math.sqrt(
+ 0.299 * (r * r) +
+ 0.587 * (g * g) +
+ 0.114 * (b * b)
+ );
+
+ // Using the HSP value, determine whether the color is light or dark
+ if (hsp > 127.5) {
+ return 'light';
+ }
+ else {
+
+ return 'dark';
+ }
+}
+
+export function hasDescendantTarget(x: number, y: number, target: HTMLDivElement | null) {
+ let entered = false;
+ for (let child = document.elementFromPoint(x, y); !entered && child; child = child.parentElement) {
+ entered = entered || child === target;
+ }
+ return entered;
+}
+
export function setupMoveUpEvents(
target: object,
e: React.PointerEvent,