-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzPalette.js
260 lines (241 loc) · 9.13 KB
/
zPalette.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
var BASE64_MARKER = ';base64,';
var crcTableCache;
var makeCRCTable = function() {
var c;
var crcTable = [];
for (var n = 0; n < 256; n++) {
c = n;
for (var k = 0; k < 8; k++) {
c = ((c & 1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1));
}
crcTable[n] = c;
}
return crcTableCache = crcTable;
};
var crc32_gp = function(str) {
var crcTable = crcTableCache || (makeCRCTable());
var crc = 0 ^ (-1);
for (var i = 0; i < str.length; i++) {
crc = (crc >>> 8) ^ crcTable[(crc ^ str.charCodeAt(i)) & 0xFF];
}
return (crc ^ (-1)) >>> 0;
};
(function(){
var slice = Array.prototype.slice,
toString = Object.prototype.toString,
fromCharCode = String.fromCharCode,
apply = function( a, b ){
for( var i in b )
if( b.hasOwnProperty( i ) )
a[ i ] = b[ i ];
return a;
},
BASE64_MARKER = ';base64,',
crc = function( string ){
var crc = crc32_gp( string );
crc = fromCharCode( crc >>> 24 & 255 ) +
fromCharCode( crc >>> 16 & 255 ) +
fromCharCode( crc >>> 8 & 255 ) +
fromCharCode( crc & 255 );
return crc;
},
arrayToBinary = function( arr ){
var out = '', i, _i;
for( i = 0, _i = arr.length; i < _i; i++ )
out+= fromCharCode( arr[ i ] );
return out;
},
binaryToArray = function( string ){
var out, i, _i = string.length;
out = new Array( _i ); // initializing length is faster than create items (jsperf)
for( i = 0, _i; i < _i; i++ )
out[ i ] = string.charCodeAt( i );
return out;
},
toTwoHex = function( number ){
var hex = number.toString( 16 );
return hex.length < 2 ? '0' + hex : hex;
},
Chunk = function( _modifiedChunks ){
this._modifiedChunks = _modifiedChunks;
},
zPngology = function(){};
Chunk.prototype = {
set: function( n ){
var blockSize = arguments.length - 1,
offset = n * blockSize, i,
oldVal, newVal;
for( i = 1; i <= blockSize; i++ ){
oldVal = this.data[ offset ];
newVal = arguments[ i ];
if( oldVal !== newVal ){
this.data[ offset ] = newVal;
this._modifiedChunks[ this.name ] = true;
}
offset++;
}
},
_rawUpdate: function( ){
var chunkNameAndData = this.name + arrayToBinary( this.data ); // may be optimized, we don't have to do it with unmodified data
return this.rawData = this.rawLength + chunkNameAndData + crc( chunkNameAndData );
}
};
zPngology.prototype = {
ctor: function( cfg ){
if( typeof cfg === 'string' ){
// JUST MODIFY BASE64 DATA
this.TYPE = 'text';
this.base64 = cfg;
this.update = this.getBase64;
}else if(
toString.call(cfg) === '[object HTMLImageElement]' ||
(cfg.tagName && cfg.tagName === 'IMG')
){
// IMAGE CASE
this.TYPE = 'object';
this.update = this._imageUpdate;
this.el = cfg;
this.base64 = cfg.src;
}else if( typeof cfg === 'object' ){
apply( this, cfg );
return this.ctor( cfg.data || cfg.image || cfg.obj || cfg.el );
}else{
throw 'incorrect argument for zPngology';
}
this._initChunks();
return this;
},
// update image
_imageUpdate: function( ){
this.el.src = this.getBase64();
return this;
},
_getLength: function( string ){
var length = binaryToArray( string );
return length[ 3 ] +
length[ 2 ] * 256 +
length[ 1 ] * 65536 +
length[ 0 ] * 16777216;
},
_initChunks: function( ){
var base64Prefix = this.base64Prefix = this.base64.substr( 0, this.base64.indexOf( BASE64_MARKER ) + BASE64_MARKER.length),
rawData = this.rawData = atob( this.base64.substr( base64Prefix.length ) ),
data = rawData.substr( 8 ),
chunks = this.chunks = {},
chunk,
chunkLength,
_modifiedChunks = this._modifiedChunks = {},
interactiveChunks = { PLTE: true, tRNS: true },
_chunkOrder = this._chunkOrder = [];
this.prefix = rawData.substr( 0, 8 );
while( data.length ){
chunk = new Chunk( _modifiedChunks );
chunkLength = this._getLength( chunk.rawLength = data.substr( 0, 4 ) );
chunk.length = chunkLength;
chunk.name = data.substr( 4, 4 );
_chunkOrder.push( chunk );
chunk.rawData = data.substr( 0, chunkLength + 12 );
if( interactiveChunks[ chunk.name ] )
chunk.data = binaryToArray( chunk.rawData.substr(8, chunkLength) );
chunks[ chunk.name ] = chunk;
data = data.substr( chunk.rawData.length );
}
this._initOriginalColors();
},
_initOriginalColors: function( ){
this.originalColors = {};
var data = this.chunks.PLTE.data,
number = 0,
i, _i, tmp;
for( i = 0, _i = data.length; i < _i; i += 3 ){
this.originalColors[
toTwoHex( data[ i ] ) +
toTwoHex( data[ i + 1 ] ) +
toTwoHex( data[ i + 2 ] )
] = number;
number++;
}
},
_base64Update: function( ){
var chunks = this.chunks,
_modifiedChunks = this._modifiedChunks,
chunk, i, _i,
data = this.prefix || '',
_chunkOrder = this._chunkOrder;
for( i = 0, _i = _chunkOrder.length; i < _i; i++ ){
chunk = _chunkOrder[ i ];
if( _modifiedChunks[ chunk.name ] ){
data += chunk._rawUpdate();
delete _modifiedChunks[ chunk.name ];
}else
data += chunk.rawData;
}
return this.base64 = this.base64Prefix + btoa( data );
},
getBase64: function( ){
this._base64Update();
return this.base64;
},
parseColor: function( color, alpha ){
if( typeof color === 'string' ){
var tmp;
if( color.charAt(0) === '#' )
color = color.substr( 1 );
return color.length === 3 ?
[
parseInt( ( tmp = color.charAt(0) ) + tmp, 16 ),
parseInt( ( tmp = color.charAt(1) ) + tmp, 16 ),
parseInt( ( tmp = color.charAt(2) ) + tmp, 16 ),
alpha
]
:
[
parseInt( color.substr( 0, 2 ), 16 ),
parseInt( color.substr( 2, 2 ), 16 ),
parseInt( color.substr( 4, 2 ), 16 ),
alpha
];
}
},
setColorHex: function( number, color, alpha ){
return this.setColor(
number,
this.parseColor( color, alpha )
);
},
numberFromColor: function( ){
},
getColor: function( number ){
var PLTE = this.chunks.PLTE.data,
tRNS = (this.chunks.tRNS || {data:[]}).data,
offset = number * 3;
return [
PLTE[ offset ],
PLTE[ offset + 1 ],
PLTE[ offset + 2 ],
tRNS[ number ]
];
},
setOriginalColor: function( originalColor, color, hex, alpha ){
if( originalColor.charAt(0) === '#' )
originalColor = originalColor.substr( 1 );
var number = this.originalColors[ originalColor ];
number !== void 0 && ( hex ? this.setColorHex( number, color, alpha ) : this.setColor( number, color ) );
return this;
},
setOriginalColorHex: function( originalColor, color, alpha ){
return this.setOriginalColor( originalColor, color, true, alpha )
},
setColor: function( number, color ){
this.chunks.PLTE.set( number, color[ 0 ], color[ 1 ], color[ 2 ] );
color[ 3 ] !== void 0 && this.chunks.tRNS.set( number, color[ 3 ] );
return this;
}
};
// Factory
window.Pngology = function(){
var obj = new zPngology();
obj.ctor.apply(obj, slice.call(arguments));
return obj;
};
})();