Skip to content

Commit 9ed3e04

Browse files
committed
Merge branch 'master' of https://github.com/rastasheep/jquery.adaptive-backgrounds.js into rastasheep-master
Conflicts: dist/jquery.adaptive-backgrounds.min.js
2 parents a7df228 + 81e14ca commit 9ed3e04

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ Default Options
3535
----------------
3636
- __selector__ String _(default: `'img[data-adaptive-background="1"]'`)_ a CSS selector which denotes which images to grab/process. Ideally, this selector would start with _img_, to ensure we only grab and try to process actual images.
3737
- __parent__ falsy _(default: `null`)_ a CSS selector which denotes which parent to apply the background color to. By default, the color is applied to the parent one level up the DOM tree.
38+
- __changeTxtColor__ boolean _(default: `false`)_ option to change color of parent text according generated background image
39+
- __parentTxtLight__ String _(default: `#fff`)_ text color used when background color is dark
40+
- __parentTxtDark__ String _(default: `#000`)_ text color used when background color is light
41+
3842

3943
__Example:__
4044
Call the `run` method, passing in any options you'd like to override.

src/jquery.adaptive-backgrounds.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
var DEFAULTS = {
1212
selector: 'img[data-adaptive-background="1"]',
1313
parent: null,
14+
changeTxtColor: false,
15+
parentTxtLight: "#fff",
16+
parentTxtDark: "#000",
1417
};
1518

1619
// Include RGBaster - https://github.com/briangonzalez/rgbaster.js
@@ -40,6 +43,7 @@
4043

4144
/* Subscribe to our color-found event. */
4245
$this.on( EVENT_CF, function(ev, data){
46+
var $data = data;
4347
var $parent;
4448
if ( $this.attr( DATA_PARENT ) ){
4549
$parent = $this.parents( $this.attr( DATA_PARENT ) );
@@ -52,6 +56,17 @@
5256
}
5357

5458
$parent.css({ backgroundColor: data.color });
59+
60+
if (opts.changeTxtColor){
61+
/* Small helper function for calculating color contrast */
62+
var getTxtColor = function (){
63+
var rgb = $data.color.match(/\d+/g);
64+
var yiq = ((rgb[0]*299)+(rgb[1]*587)+(rgb[2]*114))/1000;
65+
return yiq >= 128 ? opts.parentTxtDark : opts.parentTxtLight;
66+
};
67+
68+
$parent.css({ color: getTxtColor });
69+
}
5570
});
5671

5772
/* Handle the colors. */
@@ -61,4 +76,4 @@
6176
},
6277
};
6378

64-
})(jQuery);
79+
})(jQuery);

0 commit comments

Comments
 (0)