-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpxgradient-1.0.3.js
157 lines (130 loc) · 5.26 KB
/
pxgradient-1.0.3.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
151
152
153
154
155
156
157
/**
* @author Sergey Nikitin (mrnix@ya.ru)
* @author Andrew Lechev
* @author Vasily Mamaevsky
*
* inspired by and used http://www.artlebedev.ru/tools/technogrette/js/gradient-text/
* @link mrnix.ru/pxgradient
* @version 1.0.2
* @date 30.03.2012
* @requires jQuery
*
* Description:
* pxgradient is a jQuery plugin that paints text in gradient colors by pixel
*
* Usage:
* $(selector).pxgradient(options);
*
* options is an object contents configuraton paramenters:
* {Number} step - Step Color. The smaller the number, the greater the load. Default: 10
* {Array} colors - array of hex colors. Default: ["#ffcc00", "#cc0000", "#000000"];
* {String} dir - gradient direction. "x" - horizontal, "y" - vertical
*/
(function($){
$('head').append('<style type="text/css">.sn-pxg .pxg-set{user-select:none;-moz-user-select:none;-webkit-user-select:none;}.sn-pxg span.pxg-source{position:relative;display:inline-block;z-index:2;}.sn-pxg U.pxg-set,.sn-pxg U.pxg-set S,.sn-pxg U.pxg-set S B{left:0;right:0;top:0;bottom:0;height:inherit;width:inherit;position:absolute;display:inline-block;text-decoration:none;font-weight:inherit;}.sn-pxg U.pxg-set S{overflow:hidden;}.sn-pxg U.pxg-set{text-decoration:none;z-index:1;display:inline-block;position:relative;}</style>')
$.fn.pxgradient=function(options){
var options = $.extend({
step: 10,
colors: ["#ffcc00","#cc0000","#000000"],
dir: "y"
}, options);
options.RGBcolors = [];
for(var i=0;i<options.colors.length;i++){
options.RGBcolors[i] = hex2Rgb(options.colors[i]);
}
return this.each(function(i,e){
var pxg = $(e);
if(!pxg.hasClass("sn-pxg")) {
var pxg_source = pxg.html();
pxg
.html('<span class="pxg-source" style="visibility: hidden;">'+pxg_source+'</span>')
.append('<u class="pxg-set"></u>');
var pxg_set = pxg.find(".pxg-set");
var pxg_text = pxg.find(".pxg-source");
var pxg_w = pxg_text.innerWidth();
var pxg_h = pxg_text.innerHeight();
pxg_text.hide();
pxg.addClass("sn-pxg");
if ( options.dir == "x" ) { var blocksize = pxg_w; }
else if ( options.dir =="y" ) { var blocksize = pxg_h; }
var fullsteps = Math.floor( blocksize/options.step );
var allsteps = fullsteps;
var laststep = ( blocksize-( fullsteps * options.step) );
if( laststep>0 ) { allsteps++; }
pxg_set.css({ width: pxg_w, height: pxg_h });
var offleft = 0;
var pxg_set_html = '';
if(options.dir == "x"){
for(var i=0; i<allsteps; i++){
var color = getColor ( offleft, blocksize );
pxg_set_html += '<s style="height:'+pxg_h+'px;width:'+options.step+'px;left:'+offleft+'px;color:'+color+'"><b style="left:-'+offleft+'px;width:'+pxg_w+'px;height:'+pxg_h+'px;">'+pxg_source+'</b></s>';
offleft = offleft + options.step;
}
}
else if(options.dir=="y"){
for(var i=0; i<allsteps; i++){
var color=getColor(offleft,blocksize);
pxg_set_html += '<s style="width:'+pxg_w+'px;height:'+options.step+'px;top:'+offleft+'px;color:'+color+'"><b style="top:-'+offleft+'px;height:'+pxg_w+'px;height:'+pxg_h+'px;">'+pxg_source+'</b></s>';
offleft = offleft + options.step;
}
}
pxg_set.append(pxg_set_html);
}
});
/**
* @authors Andrew Lechev and Vasily Mamaevsky
* Convert HEX to RGB.
* @param {String} hex
* @return {Array}
*/
function hex2Rgb(hex){
if('#'==hex.substr(0,1)){
hex=hex.substr(1);
}
if (3==hex.length){
hex=hex.substr(0,1)+hex.substr(0,1)+hex.substr(1,1)+hex.substr(1,1)+hex.substr(2,1)+hex.substr(2,1);
}
return [parseInt(hex.substr(0,2),16),parseInt(hex.substr(2,2),16),parseInt(hex.substr(4,2),16)];
}
/**
* @authors Andrew Lechev and Vasily Mamaevsky
* Convert RGB to HEX.
* @param {String} hex
* @return {Array}
*/
function rgb2Hex(rgb){
var s = '0123456789abcdef';
return '#'+s.charAt(parseInt(rgb[0]/16))+s.charAt(rgb[0]%16)+s.charAt(parseInt(rgb[1]/16))+s.charAt(rgb[1]%16)+s.charAt(parseInt(rgb[2]/16))+s.charAt(rgb[2]%16);
}
/**
* @authors Andrew Lechev and Vasily Mamaevsky
* Convert px to color
*/
function getColor(off,blocksize){
var fLeft=(off>0)?(off/blocksize):0;
for (var i=0;i<options.colors.length;i++){
fStopPosition=(i/(options.colors.length-1));
fLastPosition=(i>0)?((i-1)/(options.colors.length-1)):0;
if(fLeft==fStopPosition){
return options.colors[i];
}
else if(fLeft<fStopPosition){
fCurrentStop=(fLeft-fLastPosition)/(fStopPosition-fLastPosition);
return getMidColor(options.RGBcolors[i-1],options.RGBcolors[i],fCurrentStop);
}
}
return options.colors[options.colors.length-1];
}
/**
* @authors Andrew Lechev and Vasily Mamaevsky
* Get middle color from start to end
*/
function getMidColor(aStart,aEnd,fMidStop){
var aRGBColor=[];
for (var i=0;i<3;i++){
aRGBColor[i]=aStart[i]+Math.round((aEnd[i]-aStart[i])*fMidStop)
}
return rgb2Hex(aRGBColor)
}
}
})(jQuery);