Skip to content

Commit

Permalink
Add Color Matrix filter class (#3139)
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Kunz authored and asturur committed Aug 3, 2016
1 parent ebb2552 commit 03d2768
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
1 change: 1 addition & 0 deletions build.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ var filesToInclude = [
ifSpecifiedInclude('image_filters', 'src/filters/multiply_filter.class.js'),
ifSpecifiedInclude('image_filters', 'src/filters/blend_filter.class.js'),
ifSpecifiedInclude('image_filters', 'src/filters/resize_filter.class.js'),
ifSpecifiedInclude('image_filters', 'src/filters/colormatrix_filter.class.js'),

ifSpecifiedInclude('text', 'src/shapes/text.class.js'),

Expand Down
107 changes: 107 additions & 0 deletions src/filters/colormatrix_filter.class.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
(function(global) {

'use strict';

var fabric = global.fabric || (global.fabric = { }),
extend = fabric.util.object.extend;

/**
* Color Matrix filter class
* @class fabric.Image.filters.ColorMatrix
* @memberOf fabric.Image.filters
* @extends fabric.Image.filters.BaseFilter
* @see {@link fabric.Image.filters.ColorMatrix#initialize} for constructor definition
* @see {@link http://fabricjs.com/image-filters|ImageFilters demo}
* @see {@Link http://www.webwasp.co.uk/tutorials/219/Color_Matrix_Filter.php}
* @see {@Link http://phoboslab.org/log/2013/11/fast-image-filters-with-webgl}
* @example <caption>Kodachrome filter</caption>
* var filter = new fabric.Image.filters.ColorMatrix({
* matrix: [
1.1285582396593525, -0.3967382283601348, -0.03992559172921793, 0, 63.72958762196502,
-0.16404339962244616, 1.0835251566291304, -0.05498805115633132, 0, 24.732407896706203,
-0.16786010706155763, -0.5603416277695248, 1.6014850761964943, 0, 35.62982807460946,
0, 0, 0, 1, 0
]
* });
* object.filters.push(filter);
* object.applyFilters(canvas.renderAll.bind(canvas));
*/
fabric.Image.filters.ColorMatrix = fabric.util.createClass(fabric.Image.filters.BaseFilter, /** @lends fabric.Image.filters.ColorMatrix.prototype */ {

/**
* Filter type
* @param {String} type
* @default
*/
type: 'ColorMatrix',

/**
* Constructor
* @memberOf fabric.Image.filters.ColorMatrix.prototype
* @param {Object} [options] Options object
* @param {Array} [options.matrix] Color Matrix to modify the image data with
*/
initialize: function( options ) {
options || ( options = {} );
this.matrix = options.matrix || [
1, 0, 0, 0, 0,
0, 1, 0, 0, 0,
0, 0, 1, 0, 0,
0, 0, 0, 1, 0
];
},

/**
* Applies filter to canvas element
* @param {Object} canvasEl Canvas element to apply filter to
*/
applyTo: function( canvasEl ) {
var context = canvasEl.getContext( '2d' ),
imageData = context.getImageData( 0, 0, canvasEl.width, canvasEl.height ),
data = imageData.data,
iLen = data.length,
i,
r,
g,
b,
a,
m = this.matrix;

for ( i = 0; i < iLen; i += 4 ) {
r = data[ i ];
g = data[ i + 1 ];
b = data[ i + 2 ];
a = data[ i + 3 ];

data[ i ] = r * m[ 0 ] + g * m[ 1 ] + b * m[ 2 ] + a * m[ 3 ] + m[ 4 ];
data[ i + 1 ] = r * m[ 5 ] + g * m[ 6 ] + b * m[ 7 ] + a * m[ 8 ] + m[ 9 ];
data[ i + 2 ] = r * m[ 10 ] + g * m[ 11 ] + b * m[ 12 ] + a * m[ 13 ] + m[ 14 ];
data[ i + 3 ] = r * m[ 15 ] + g * m[ 16 ] + b * m[ 17 ] + a * m[ 18 ] + m[ 19 ];
}

context.putImageData( imageData, 0, 0 );
},

/**
* Returns object representation of an instance
* @return {Object} Object representation of an instance
*/
toObject: function() {
return extend(this.callSuper('toObject'), {
type: this.type,
matrix: this.matrix
});
}
});

/**
* Returns filter instance from an object representation
* @static
* @param {Object} object Object to create an instance from
* @return {fabric.Image.filters.ColorMatrix} Instance of fabric.Image.filters.ColorMatrix
*/
fabric.Image.filters.ColorMatrix.fromObject = function( object ) {
return new fabric.Image.filters.ColorMatrix( object );
};

})(typeof exports !== 'undefined' ? exports : this);

0 comments on commit 03d2768

Please sign in to comment.