2
* # Semantic - Colorize
3
* http://github.com/jlukic/semantic-ui/
6
* Copyright 2014 Contributors
7
* Released under the MIT license
8
* http://opensource.org/licenses/MIT
12
;(function ( $, window, document, undefined ) {
14
$.fn.colorize = function(parameters) {
16
settings = $.extend(true, {}, $.fn.colorize.settings, parameters),
18
moduleArguments = arguments || false
21
.each(function(instanceIndex) {
26
mainCanvas = $('<canvas />')[0],
27
imageCanvas = $('<canvas />')[0],
28
overlayCanvas = $('<canvas />')[0],
30
backgroundImage = new Image(),
44
colors = settings.colors,
45
paths = settings.paths,
46
namespace = settings.namespace,
47
error = settings.error,
50
instance = $module.data('module-' + namespace),
56
checkPreconditions: function() {
57
module.debug('Checking pre-conditions');
59
if( !$.isPlainObject(colors) || $.isEmptyObject(colors) ) {
60
module.error(error.undefinedColors);
66
async: function(callback) {
68
setTimeout(callback, 0);
75
getMetadata: function() {
76
module.debug('Grabbing metadata');
77
image = $module.data('image') || settings.image || undefined;
78
imageName = $module.data('name') || settings.name || instanceIndex;
79
width = settings.width || $module.width();
80
height = settings.height || $module.height();
81
if(width === 0 || height === 0) {
82
module.error(error.undefinedSize);
86
initialize: function() {
87
module.debug('Initializing with colors', colors);
88
if( module.checkPreconditions() ) {
90
module.async(function() {
92
module.canvas.create();
94
module.draw.image(function() {
96
module.canvas.merge();
99
.data('module-' + namespace, module)
106
module.debug('Redrawing image');
107
module.async(function() {
108
module.canvas.clear();
109
module.draw.colors();
110
module.canvas.merge();
115
color: function(colorName, color) {
116
module.debug('Changing color', colorName);
117
if(colors[colorName] === undefined) {
118
module.error(error.missingColor);
121
colors[colorName] = color;
128
module.debug('Creating canvases');
130
mainCanvas.width = width;
131
mainCanvas.height = height;
132
imageCanvas.width = width;
133
imageCanvas.height = height;
134
overlayCanvas.width = width;
135
overlayCanvas.height = height;
137
mainContext = mainCanvas.getContext('2d');
138
imageContext = imageCanvas.getContext('2d');
139
overlayContext = overlayCanvas.getContext('2d');
142
.append( mainCanvas )
144
mainContext = $module.children('canvas')[0].getContext('2d');
146
clear: function(context) {
147
module.debug('Clearing canvas');
148
overlayContext.fillStyle = '#FFFFFF';
149
overlayContext.fillRect(0, 0, width, height);
152
if( !$.isFunction(mainContext.blendOnto) ) {
153
module.error(error.missingPlugin);
156
mainContext.putImageData( imageContext.getImageData(0, 0, width, height), 0, 0);
157
overlayContext.blendOnto(mainContext, 'multiply');
163
image: function(callback) {
164
module.debug('Drawing image');
165
callback = callback || function(){};
167
backgroundImage.src = image;
168
backgroundImage.onload = function() {
169
imageContext.drawImage(backgroundImage, 0, 0);
174
module.error(error.noImage);
180
module.debug('Drawing color overlays', colors);
181
$.each(colors, function(colorName, color) {
182
settings.onDraw(overlayContext, imageName, colorName, color);
188
debug: function(message, variableName) {
190
if(variableName !== undefined) {
191
console.info(settings.name + ': ' + message, variableName);
194
console.info(settings.name + ': ' + message);
198
error: function(errorMessage) {
199
console.warn(settings.name + ': ' + errorMessage);
201
invoke: function(methodName, context, methodArguments) {
205
methodArguments = methodArguments || Array.prototype.slice.call( arguments, 2 );
207
if(typeof methodName == 'string' && instance !== undefined) {
208
methodName = methodName.split('.');
209
$.each(methodName, function(index, name) {
210
if( $.isPlainObject( instance[name] ) ) {
211
instance = instance[name];
214
else if( $.isFunction( instance[name] ) ) {
215
method = instance[name];
218
module.error(settings.error.method);
222
return ( $.isFunction( method ) )
223
? method.apply(context, methodArguments)
229
if(instance !== undefined && moduleArguments) {
230
// simpler than invoke realizing to invoke itself (and losing scope due prototype.call()
231
if(moduleArguments[0] == 'invoke') {
232
moduleArguments = Array.prototype.slice.call( moduleArguments, 1 );
234
return module.invoke(moduleArguments[0], this, Array.prototype.slice.call( moduleArguments, 1 ) );
243
$.fn.colorize.settings = {
244
name : 'Image Colorizer',
246
namespace : 'colorize',
248
onDraw : function(overlayContext, imageName, colorName, color) {},
250
// whether to block execution while updating canvas
252
// object containing names and default values of color regions
261
noImage : 'No tracing image specified',
262
undefinedColors : 'No default colors specified.',
263
missingColor : 'Attempted to change color that does not exist',
264
missingPlugin : 'Blend onto plug-in must be included',
265
undefinedHeight : 'The width or height of image canvas could not be automatically determined. Please specify a height.'
270
})( jQuery, window , document );