@bigfishtv/cockpit
Version:
179 lines (153 loc) • 6.01 kB
JavaScript
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
import { curvesHashTable, getContrastCurve, getVibranceMatrix, getTemperatureRGB, interpolateGradient } from './colorUtils';
import isCrossDomain from './isCrossDomain';
export function init() {
var Caman = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : window.Caman;
if (!Caman) {
console.warn('Caman object does not exist. Cannot add custom Tank filters.');
return;
}
var INITED = '__tankCamanInited__';
if (Caman[INITED]) {
return;
}
Caman[INITED] = true;
// replace Caman's isURLRemote with our own
Caman.IO.isURLRemote = isCrossDomain;
// Created my own color matrix filter
// http://developer.android.com/reference/android/graphics/ColorMatrix.html
Caman.Filter.register('colorMatrix', function (matrix) {
return this.process('matrix', function (rgba) {
rgba.r /= 255;
rgba.g /= 255;
rgba.b /= 255;
rgba.a /= 255;
rgba.r = matrix[0] * rgba.r + matrix[1] * rgba.g + matrix[2] * rgba.b + matrix[3] * rgba.a + matrix[4];
rgba.g = matrix[5] * rgba.r + matrix[6] * rgba.g + matrix[7] * rgba.b + matrix[8] * rgba.a + matrix[9];
rgba.b = matrix[10] * rgba.r + matrix[11] * rgba.g + matrix[12] * rgba.b + matrix[13] * rgba.a + matrix[14];
rgba.a = matrix[15] * rgba.r + matrix[16] * rgba.g + matrix[17] * rgba.b + matrix[18] * rgba.a + matrix[19];
rgba.r = Math.min(Math.max(rgba.r * 255, 0), 255);
rgba.g = Math.min(Math.max(rgba.g * 255, 0), 255);
rgba.b = Math.min(Math.max(rgba.b * 255, 0), 255);
rgba.a = Math.min(Math.max(rgba.a * 255, 0), 255);
return rgba;
});
});
// We overwrite caman's contrast filter in favour of our custom one
Caman.Filter.register('contrast', function (adjust) {
this.curves.apply(this, ['rgb'].concat(getContrastCurve(adjust), [curvesHashTable]));
});
// We overwrite caman's contrast filter in favour of our custom one
Caman.Filter.register('exposure', function (adjust) {
var p = Math.abs(adjust) / 100;
var ctrl1 = [0, 255 * p];
var ctrl2 = [255 - 255 * p, 255];
if (adjust < 0) {
ctrl1.reverse();
ctrl2.reverse();
}
this.curves.apply(this, ['rgb', ctrl1, ctrl2, curvesHashTable]);
});
Caman.Filter.register('fade', function (adjust) {
var ctrl1 = [0, adjust];
var ctrl2 = [255, 255 - adjust / 2];
this.curves.apply(this, ['rgb', ctrl1, ctrl2, curvesHashTable]);
});
// We overwrite caman's vibrance filter in favour of our custom one
Caman.Filter.register('vibrance', function (adjust) {
var matrix = getVibranceMatrix(adjust);
this.colorMatrix.apply(this, [matrix]);
});
// We overwrite caman's channels (tint) filter in favour of our custom one
Caman.Filter.register('channels', function (options) {
var red = options.red ? options.red / 100 : 0;
var green = options.green ? options.green / 100 : 0;
var blue = options.blue ? options.blue / 100 : 0;
/* prettier-ignore */
var matrix = [1, 0, 0, 0, red, 0, 1, 0, 0, green, 0, 0, 1, 0, blue, 0, 0, 0, 1, 0];
this.colorMatrix.apply(this, [matrix]);
});
Caman.Filter.register('temperature', function (adjust) {
var _getTemperatureRGB = getTemperatureRGB(adjust),
red = _getTemperatureRGB.red,
green = _getTemperatureRGB.green,
blue = _getTemperatureRGB.blue;
/* prettier-ignore */
var matrix = [red / 255, 0, 0, 0, 0, 0, green / 255, 0, 0, 0, 0, 0, blue / 255, 0, 0, 0, 0, 0, 1, 0];
this.colorMatrix.apply(this, [matrix]);
});
Caman.Filter.register('curves', function () {
var algo = void 0,
i = void 0,
_i = void 0,
_j = void 0,
_ref = void 0,
_ref1 = void 0;
var isBezier = false;
var channels = arguments[0];
var ctrlPts = arguments.length >= 2 ? Array.prototype.slice.call(arguments, 1) : [];
var last = ctrlPts[ctrlPts.length - 1];
switch (typeof last === 'undefined' ? 'undefined' : _typeof(last)) {
case 'function':
algo = last;
ctrlPts.pop();
break;
case 'string':
algo = Caman.Calculate[last];
ctrlPts.pop();
break;
default:
isBezier = true;
algo = Caman.Calculate.bezier;
break;
}
if (typeof channels === 'string') {
channels = channels.split('');
}
if (channels[0] === 'v') {
channels = ['r', 'g', 'b'];
}
if (ctrlPts.length < 2) {
console.warn(ctrlPt);
throw 'Invalid number of arguments to curves filter';
}
var bezier = isBezier ? algo.apply(this, [].concat(ctrlPts, [0, 255])) : algo(ctrlPts, 0, 255);
var start = ctrlPts[0];
if (start[0] > 0) {
for (i = _i = 0, _ref = start[0]; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) {
bezier[i] = start[1];
}
}
var end = ctrlPts[ctrlPts.length - 1];
if (end[0] < 255) {
for (i = _j = _ref1 = end[0]; _ref1 <= 255 ? _j <= 255 : _j >= 255; i = _ref1 <= 255 ? ++_j : --_j) {
bezier[i] = end[1];
}
}
return this.process('curves', function (rgba) {
var _k = void 0,
_ref2 = void 0;
for (i = _k = 0, _ref2 = channels.length; 0 <= _ref2 ? _k < _ref2 : _k > _ref2; i = 0 <= _ref2 ? ++_k : --_k) {
rgba[channels[i]] = bezier[rgba[channels[i]]];
}
return rgba;
});
});
Caman.Filter.register('blur', function () {
/* prettier-ignore */
this.processKernel("Blur", [1, 1, 1, 1, 1, 1, 1, 1, 1]);
});
Caman.Filter.register('gradientMap', function () {
for (var _len = arguments.length, markers = Array(_len), _key = 0; _key < _len; _key++) {
markers[_key] = arguments[_key];
}
var gradientMap = interpolateGradient(markers, 256);
return this.process('matrix', function (rgba) {
// const avg = Caman.Calculate.luminance(rgba)
rgba.r = Math.floor(gradientMap[rgba.r * 4]);
rgba.g = Math.floor(gradientMap[rgba.g * 4 + 1]);
rgba.b = Math.floor(gradientMap[rgba.b * 4 + 2]);
return rgba;
});
});
}