-
Notifications
You must be signed in to change notification settings - Fork 103
/
jquery.flatshadow.js
152 lines (135 loc) · 5.18 KB
/
jquery.flatshadow.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/* ===========================================================
* jquery-flatshadow.js v1
* ===========================================================
* Copyright 2013 Pete Rojwongsuriya.
* http://www.thepetedesign.com
*
* A small jQuery plugin that will automatically
* cast a shadow creating depth for your flat UI elements
* https://github.com/peachananr/flat-shadow
*
* ========================================================== */
!function($){
var colors = new Array("#1ABC9C","#2ecc71","#3498db","#9b59b6","#34495e","#f1c40f","#e67e22", "#e74c3c");
var angles = new Array("NE","SE","SW", "NW");
var defaults = {
fade: false,
color: "random",
boxShadow: false,
angle: "random"
};
function convertHex(hex,opacity){
hex = hex.replace('#','');
r = parseInt(hex.substring(0,2), 16);
g = parseInt(hex.substring(2,4), 16);
b = parseInt(hex.substring(4,6), 16);
result = 'rgba('+r+','+g+','+b+','+opacity/100+')';
return result;
}
function colorLuminance(hex, lum) {
// validate hex string
hex = String(hex).replace(/[^0-9a-f]/gi, '');
if (hex.length < 6) {
hex = hex[0]+hex[0]+hex[1]+hex[1]+hex[2]+hex[2];
}
lum = lum || 0;
// convert to decimal and change luminosity
var rgb = "#", c, i;
for (i = 0; i < 3; i++) {
c = parseInt(hex.substr(i*2,2), 16);
c = Math.round(Math.min(Math.max(0, c + (c * lum)), 255)).toString(16);
rgb += ("00"+c).substr(c.length);
}
return rgb;
}
$.fn.flatshadow = function(options){
var settings = $.extend({}, defaults, options);
return this.each(function(){
el = $(this);
if (settings.fade == true) {
width = Math.round(el.outerWidth() / 3);
height = Math.round(el.outerHeight() / 3);
} else {
width = Math.round(el.outerWidth());
height = Math.round(el.outerHeight());
}
if (settings.boxShadow != false) {
var bg_color = settings.boxShadow;
}
if (el.attr('data-color')) {
var color = el.attr('data-color');
} else {
var color = settings.color;
}
if (color == "random") {
color = colors[Math.floor(Math.random() * colors.length)];
}
if (el.attr('data-angle')) {
var angle = el.attr('data-angle');
} else {
var angle = settings.angle;
}
if (angle == "random") {
angle = angles[Math.floor(Math.random() * angles.length)];
}
var darkercolor = colorLuminance(color, -0.3);
var text_shadow = "";
if (settings.boxShadow != false) {
var box_shadow = "";
} else {
var box_shadow = "none";
}
var iLimit = angle == 'E' ? width : height;
var text_color = darkercolor;
for( var i=1; i <= iLimit; i++ ) {
if (settings.fade != false) {
text_color = convertHex( darkercolor, 100 - i/iLimit * 100 );
}
switch (angle) {
case 'N':
if (settings.boxShadow != false) box_shadow += "0px " + (i * 2) * -1 + "px 0px " + convertHex( bg_color, (50 - i/ height * 100) );
text_shadow += "0px " + i * -1 + "px 0px " + text_color;
break;
case 'NE':
if (settings.boxShadow != false) box_shadow += i * 2 + "px " + (i * 2) * -1 + "px 0px " + convertHex( bg_color, (50 - i/ height * 100) );
text_shadow += i + "px " + i * -1 + "px 0px " + text_color;
break;
case 'E':
if (settings.boxShadow != false) box_shadow += i * 2 + "px " + "0px 0px " + convertHex( bg_color, (50 - i/ width * 100) );
text_shadow += i + "px " + "0px 0px " + text_color;
break;
case 'SE':
if (settings.boxShadow != false) box_shadow += i * 2 + "px " + i * 2 + "px 0px " + convertHex( bg_color, (50 - i/ height * 100) );
text_shadow += i + "px " + i + "px 0px " + text_color;
break;
case 'S':
if (settings.boxShadow != false) box_shadow += "0px " + i * 2 + "px 0px " + convertHex( bg_color, (50 - i/ height * 100) );
text_shadow += "0px " + i + "px 0px " + text_color;
break;
case 'SW':
if (settings.boxShadow != false) box_shadow += (i * 2) * -1 + "px " + i * 2 + "px 0px " + convertHex( bg_color, (50 - i/ height * 100) );
text_shadow += i * -1 + "px " + i + "px 0px " + text_color;
break;
case 'W':
if (settings.boxShadow != false) box_shadow += (i * 2) * -1 + "px " + "0px 0px " + convertHex( bg_color, (50 - i/ height * 100) );
text_shadow += i * -1 + "px " + "0px 0px " + text_color;
break;
case 'NW':
if (settings.boxShadow != false) box_shadow += (i * 2) * -1 + "px " + (i * 2) * -1 + "px 0px " + convertHex( bg_color, (50 - i/ height * 100) );
text_shadow += i * -1 + "px " + i * -1 + "px 0px " + text_color;
break;
}
if (i != iLimit) {
text_shadow += ", ";
box_shadow += ", ";
}
}
el.css({
"background": color,
"color": colorLuminance(color, 1),
"text-shadow": text_shadow,
"box-shadow": box_shadow
});
});
}
}(window.jQuery);