From 678a2921b81c4c1384f69e63d8d100e71be1faed Mon Sep 17 00:00:00 2001
From: 14ROVI <29734170+14ROVI@users.noreply.github.com>
Date: Wed, 21 Dec 2022 03:38:28 +0000
Subject: [PATCH] tried to make the text colour changer working but it just
wont. changed to having dark backgrop + white text for everything.
---
background-check-new.js | 122 ++++++
background-check.js | 878 ++++++++++++++++++++++++++++++++++++++++
background-check.min.js | 5 -
index.css | 10 +-
index.html | 2 +-
index.js | 25 +-
6 files changed, 1019 insertions(+), 23 deletions(-)
create mode 100644 background-check-new.js
create mode 100644 background-check.js
delete mode 100644 background-check.min.js
diff --git a/background-check-new.js b/background-check-new.js
new file mode 100644
index 0000000..c12e859
--- /dev/null
+++ b/background-check-new.js
@@ -0,0 +1,122 @@
+class BackgroundCheck {
+ constructor(targetClass, backgroundClass) {
+ this.targetClass = targetClass;
+ this.backgroundClass = backgroundClass;
+
+ window.addEventListener("resize", () => {
+ this.check();
+ });
+
+ window.addEventListener("scroll", () => {
+ this.check();
+ });
+
+ this.check();
+ }
+
+ check() {
+ // load target elements + bounding area
+ // load background elements
+ // load background images + bounding areas
+ // create canvas the size of html element and draw images onto them in their bounding areas
+ // on each target check average background lightness
+ // add class to target based on lightness
+
+ const canvas = document.createElement("canvas");
+ const context = canvas.getContext("2d", {willReadFrequently: true});
+ const body = document.body;
+ context.canvas.width = body.clientWidth;
+ context.canvas.height = body.clientHeight;
+
+ let backgroundEls = document.getElementsByClassName(this.backgroundClass);
+ let backgroundImages = []; // {url, x, y, z}
+ for (let bgEl of backgroundEls) {
+ let img = this.getImg(bgEl);
+ let cssImg = this.getCssImg(bgEl);
+ if (img) backgroundImages.push(img);
+ if (cssImg) backgroundImages.push(cssImg);
+ }
+ backgroundImages.sort((a, b) => (a.z || 0) - (b.z || 0))
+ let promises = []
+ for (let bgImg of backgroundImages) {
+ let promise = new Promise((resolve, reject) => {
+ let img = new Image();
+ img.onload = () => {
+ context.drawImage(img, bgImg.x, bgImg.y);
+ resolve();
+ }
+ img.crossOrigin = "Anonymous";
+ img.src = bgImg.url;
+ });
+ promises.push(promise);
+ }
+
+ Promise.all(promises).then(() => {
+ console.log(canvas.toDataURL());
+ let targetEls = document.getElementsByClassName(this.targetClass);
+ for (let targetEl of targetEls) {
+ let rect = targetEl.getBoundingClientRect();
+
+ if (rect.width === 0 || rect.height === 0) {
+ continue;
+ }
+
+ let x = rect.x + window.scrollX;
+ let y = rect.y + window.scrollY;
+ let imgd = context.getImageData(x, y, rect.width, rect.height);
+ let pix = imgd.data;
+ let totalRed = 0;
+ let totalGreen = 0;
+ let totalBlue = 0;
+ for (let i = 0, n = pix.length; i < n; i += 4) {
+ totalRed += pix[i]; // red
+ totalBlue += pix[i + 1]; // green
+ totalGreen += pix[i + 2]; // blue
+ // i+3 is alpha (the fourth element)
+ }
+ let brightness = totalRed * 0.299 + totalGreen * 0.587 + totalBlue * 0.114;
+ brightness /= rect.width * rect.height;
+ console.log(brightness)
+
+ if (brightness > 100) {
+ targetEl.classList.add("light-background");
+ } else {
+ targetEl.classList.add("dark-background");
+ }
+ }
+
+ });
+ }
+
+ getImg(element) {
+ if (element.tagName === "img") {
+ let rect = element.getBoundingClientRect();
+ let x = rect.x + window.scrollX;
+ let y = rect.y + window.scrollY;
+ return {
+ url: element.src,
+ x: x,
+ y: y,
+ z: element.style.zIndex || 0,
+ }
+ }
+
+ return null;
+ }
+
+ getCssImg(element) {
+ let style = element.currentStyle || window.getComputedStyle(element, false);
+ let imageUrl = style.backgroundImage.substring(4).replaceAll('"', '').split(")")[0];
+ // let px, py = window.getComputedStyle(temp1).backgroundPosition.split();
+ // toPx(element, )
+ let rect = element.getBoundingClientRect();
+ let x = rect.x + window.scrollX;
+ let y = rect.y + window.scrollY;
+ return {
+ url: imageUrl,
+ x: x,
+ y: y,
+ z: element.style.zIndex || 0,
+ }
+ }
+}
\ No newline at end of file
diff --git a/background-check.js b/background-check.js
new file mode 100644
index 0000000..b132a21
--- /dev/null
+++ b/background-check.js
@@ -0,0 +1,878 @@
+
+/*
+ * BackgroundCheck
+ * http://kennethcachia.com/background-check
+ *
+ * v1.2.2
+ */
+
+(function (root, factory) {
+
+ if (typeof define === 'function' && define.amd) {
+ define(factory);
+ } else {
+ root.BackgroundCheck = factory(root);
+ }
+
+ }(this, function () {
+
+ 'use strict';
+
+ var resizeEvent = window.orientation !== undefined ? 'orientationchange' : 'resize';
+ var supported;
+ var canvas;
+ var context;
+ var throttleDelay;
+ var viewport;
+ var attrs = {};
+
+
+ /*
+ * Initializer
+ */
+ function init(a) {
+
+ if (a === undefined || a.targets === undefined) {
+ throw 'Missing attributes';
+ }
+
+ // Default values
+ attrs.debug = checkAttr(a.debug, false);
+ attrs.debugOverlay = checkAttr(a.debugOverlay, false);
+ attrs.targets = getElements(a.targets);
+ attrs.images = getElements(a.images || 'img', true);
+ attrs.changeParent = checkAttr(a.changeParent, false);
+ attrs.threshold = checkAttr(a.threshold, 50);
+ attrs.minComplexity = checkAttr(a.minComplexity, 30);
+ attrs.minOverlap = checkAttr(a.minOverlap, 50);
+ attrs.windowEvents = checkAttr(a.windowEvents, true);
+ attrs.maxDuration = checkAttr(a.maxDuration, 500);
+
+ attrs.mask = checkAttr(a.mask, {
+ r: 0,
+ g: 255,
+ b: 0
+ });
+
+ attrs.classes = checkAttr(a.classes, {
+ dark: 'background--dark',
+ light: 'background--light',
+ complex: 'background--complex'
+ });
+
+ if (supported === undefined) {
+ checkSupport();
+
+ if (supported) {
+ canvas.style.position = 'fixed';
+ canvas.style.top = '0px';
+ canvas.style.left = '0px';
+ canvas.style.width = '100%';
+ canvas.style.height = '100%';
+
+ window.addEventListener(resizeEvent, throttle.bind(null, function () {
+ resizeCanvas();
+ check();
+ }));
+
+ window.addEventListener('scroll', throttle.bind(null, check));
+
+ resizeCanvas();
+ check();
+ }
+ }
+ }
+
+
+ /*
+ * Destructor
+ */
+ function destroy() {
+ supported = null;
+ canvas = null;
+ context = null;
+ attrs = {};
+
+ if (throttleDelay) {
+ clearTimeout(throttleDelay);
+ }
+ }
+
+
+ /*
+ * Output debug logs
+ */
+ function log(msg) {
+
+ if (get('debug')) {
+ console.log(msg);
+ }
+ }
+
+
+ /*
+ * Get attribute value, use a default
+ * when undefined
+ */
+ function checkAttr(value, def) {
+ checkType(value, typeof def);
+ return (value === undefined) ? def : value;
+ }
+
+
+ /*
+ * Reject unwanted types
+ */
+ function checkType(value, type) {
+
+ if (value !== undefined && typeof value !== type) {
+ throw 'Incorrect attribute type';
+ }
+ }
+
+
+ /*
+ * Convert elements with background-image
+ * to Images
+ */
+ function checkForCSSImages(els) {
+ var el;
+ var url;
+ var list = [];
+
+ for (var e = 0; e < els.length; e++) {
+ el = els[e];
+ list.push(el);
+
+ if (el.tagName !== 'IMG') {
+ url = window.getComputedStyle(el).backgroundImage;
+
+ // Ignore multiple backgrounds
+ if (url.split(/,url|, url/).length > 1) {
+ throw 'Multiple backgrounds are not supported';
+ }
+
+ if (url && url !== 'none') {
+ list[e] = {
+ img: new Image(),
+ el: list[e]
+ };
+
+ url = url.slice(4, -1);
+ url = url.replace(/"/g, '');
+
+ list[e].img.src = url;
+ log('CSS Image - ' + url);
+ } else {
+ throw 'Element is not an but does not have a background-image';
+ }
+ }
+ }
+
+ return list;
+ }
+
+
+ /*
+ * Check for String, Element or NodeList
+ */
+ function getElements(selector, convertToImages) {
+ var els = selector;
+
+ if (typeof selector === 'string') {
+ els = document.querySelectorAll(selector);
+ } else if (selector && selector.nodeType === 1) {
+ els = [selector];
+ }
+
+ if (!els || els.length === 0 || els.length === undefined) {
+ throw 'Elements not found';
+ } else {
+
+ if (convertToImages) {
+ els = checkForCSSImages(els);
+ }
+
+ els = Array.prototype.slice.call(els);
+ }
+
+ return els;
+ }
+
+
+ /*
+ * Check if browser supports