Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ Used with `.init()`, `.set()` or `.get()`
+ **classes**: Classes added to targets. *Default:* `{ dark: 'background--dark', light: 'background--light', complex: 'background--complex' }`
+ **windowEvents**: Reprocess on window resize and scroll. *Default:* true.
+ **maxDuration**: Maximum processing time allowed. Killed if it takes longer. *Default:* 500 (ms).
+ **callback**: Callback function to be called after classes have been added to an element. *Example:* `function(elem, result, complex) {}`
+ **mask**: Used internally when checking if an element overlaps any of the images. *Default:* `{ r: 0, g: 255, b: 0 }`
+ **debug**: Enable or disable logs. *Default*: false.

Expand Down Expand Up @@ -163,4 +164,4 @@ Tested with the following units:

##Browser Support

Tested on IE 9-11, iOS 6/7 and the latest versions of Chrome, Firefox and Safari.
Tested on IE 9-11, iOS 6/7 and the latest versions of Chrome, Firefox and Safari.
15 changes: 11 additions & 4 deletions background-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
attrs.minOverlap = checkAttr(a.minOverlap, 50);
attrs.windowEvents = checkAttr(a.windowEvents, true);
attrs.maxDuration = checkAttr(a.maxDuration, 500);
attrs.callback = checkAttr(a.callback, function () {});

attrs.mask = checkAttr(a.mask, {
r: 0,
Expand Down Expand Up @@ -241,7 +242,7 @@
*/
function kill(start) {
var duration = new Date().getTime() - start;

log('Duration: ' + duration + 'ms');

if (duration > get('maxDuration')) {
Expand Down Expand Up @@ -540,7 +541,7 @@
for (var t = 0; t < targets.length; t++) {
target = targets[t];
target = get('changeParent') ? target.parentNode : target;

classList(target, get('classes').light, 'remove');
classList(target, get('classes').dark, 'remove');
classList(target, get('classes').complex, 'remove');
Expand All @@ -549,7 +550,7 @@


/*
* Calculate average pixel brightness of a region
* Calculate average pixel brightness of a region
* and add 'light' or 'dark' accordingly
*/
function calculatePixelBrightness(target) {
Expand All @@ -563,6 +564,7 @@
var variance;
var minOverlap = 0;
var mask = get('mask');
var isComplex = false;

if (dims.width > 0 && dims.height > 0) {
removeClasses(target);
Expand All @@ -586,12 +588,17 @@
if (minOverlap <= (data.length / 4) * (1 - (get('minOverlap') / 100))) {
variance = Math.sqrt(deltaSqr / pixels) / 255;
mean = mean / 255;

var result = mean <= (get('threshold') / 100) ? 'dark' : 'light';
log('Target: ' + target.className + ' lum: ' + mean + ' var: ' + variance);
classList(target, mean <= (get('threshold') / 100) ? get('classes').dark : get('classes').light, 'add');
classList(target, get('classes')[result], 'add');

if (variance > get('minComplexity') / 100) {
isComplex = true;
classList(target, get('classes').complex, 'add');
}

attrs.callback(target, result, isComplex);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion background-check.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading