UNPKG

libcolor

Version:

Basic color classes implementation

147 lines (128 loc) 5.76 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ColorMatrixFilter = exports.ColorMatrix = undefined; var _classCallCheck2 = require("babel-runtime/helpers/classCallCheck"); var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); var _createClass2 = require("babel-runtime/helpers/createClass"); var _createClass3 = _interopRequireDefault(_createClass2); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /** @babel */ var LUMA_R = 0.212671; var LUMA_G = 0.71516; var UMA_B = 0.072169; var LUMA_R2 = 0.3086; var LUMA_G2_R2 = 0.6094; var LUMA_B2 = 0.0820; var ONETHIRD = 1 / 3; var IDENTITY = [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0]; var RAD = Math.PI / 180; var ColorMatrix = exports.ColorMatrix = function () { function ColorMatrix() { var mat = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null; (0, _classCallCheck3.default)(this, ColorMatrix); if (mat instanceof ColorMatrix) { this.matrix = mat.matrix.concat(); } else if (Array.isArray(mat)) { this.matrix = mat.concat(); } else { this.reset(); } } (0, _createClass3.default)(ColorMatrix, [{ key: "adjustBrightness", value: function adjustBrightness() { var r = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0; var g = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : NaN; var b = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : NaN; if (isNaN(g)) g = r; if (isNaN(b)) b = r; this.concat([1, 0, 0, 0, r, 0, 1, 0, 0, g, 0, 0, 1, 0, b, 0, 0, 0, 1, 0]); } }, { key: "adjustContrast", value: function adjustContrast(r0) { var g = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : NaN; var b = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : NaN; if (isNaN(g)) g = r; if (isNaN(b)) b = r; r += 1; g += 1; b += 1; this.concat([r, 0, 0, 0, 128 * (1 - r), 0, g, 0, 0, 128 * (1 - g), 0, 0, b, 0, 128 * (1 - b), 0, 0, 0, 1, 0]); } }, { key: "adjustHue", value: function adjustHue() { var degrees = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0; degrees *= RAD; var cos = Math.cos(degrees); var sin = Math.sin(degrees); this.concat([LUMA_R + cos * (1 - LUMA_R) + sin * -LUMA_R, LUMA_G + cos * -LUMA_G + sin * -LUMA_G, UMA_B + cos * -UMA_B + sin * (1 - UMA_B), 0, 0, LUMA_R + cos * -LUMA_R + sin * 0.143, LUMA_G + cos * (1 - LUMA_G) + sin * 0.14, UMA_B + cos * -UMA_B + sin * -0.283, 0, 0, LUMA_R + cos * -LUMA_R + sin * -(1 - LUMA_R), LUMA_G + cos * -LUMA_G + sin * LUMA_G, UMA_B + cos * (1 - UMA_B) + sin * UMA_B, 0, 0, 0, 0, 0, 1, 0]); } }, { key: "adjustSaturation", value: function adjustSaturation() { var s = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0; var sInv = 1 - s; var irlum = sInv * LUMA_R; var iglum = sInv * LUMA_G; var iblum = sInv * UMA_B; this.concat([irlum + s, iglum, iblum, 0, 0, irlum, iglum + s, iblum, 0, 0, irlum, iglum, iblum + s, 0, 0, 0, 0, 0, 1, 0]); } }, { key: "colorize", value: function colorize() { var rgb = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0; var amount = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : NaN; if (isNaN(amount)) { amount = 1; } var r = (rgb >> 16 & 0xFF) / 0xFF; var g = (rgb >> 8 & 0xFF) / 0xFF; var b = (rgb & 0xFF) / 0xFF; var inv_amount = 1 - amount; this.concat([inv_amount + amount * r * LUMA_R, amount * r * LUMA_G, amount * r * UMA_B, 0, 0, amount * g * LUMA_R, inv_amount + amount * g * LUMA_G, amount * g * UMA_B, 0, 0, amount * b * LUMA_R, amount * b * LUMA_G, inv_amount + amount * b * UMA_B, 0, 0, 0, 0, 0, 1, 0]); } }, { key: "concat", value: function concat() { var mat = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; var temp = []; var i = 0; for (var y = 0; y < 4; y++) { for (var x = 0; x < 5; x++) { temp[i + x] = mat[i] * this.matrix[x] + mat[i + 1] * this.matrix[x + 5] + mat[i + 2] * this.matrix[x + 10] + mat[i + 3] * this.matrix[x + 15] + (x === 4 ? mat[i + 4] : 0); } i += 5; } this.matrix = temp; } }, { key: "filter", value: function filter() { return new ColorMatrixFilter(this.matrix); } }, { key: "reset", value: function reset() { this.matrix = IDENTITY.concat(); } }]); return ColorMatrix; }(); var ColorMatrixFilter = exports.ColorMatrixFilter = function () { function ColorMatrixFilter() { var matrix = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null; (0, _classCallCheck3.default)(this, ColorMatrixFilter); this.matrix = matrix || IDENTITY.concat(); } (0, _createClass3.default)(ColorMatrixFilter, [{ key: "clone", value: function clone() { return new ColorMatrixFilter(this.matrix); } }]); return ColorMatrixFilter; }();