UNPKG

gl2d

Version:

2D graphics package for WebGL

519 lines 16.6 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); var util_1 = require("./util"); var struct_1 = require("gulp-structify/struct"); var buffer_1 = require("gulp-structify/buffer"); var mixin_1 = require("gulp-structify/mixin"); /** * A 32-bit (r,g,b,a) color. */ var ColorF = (function () { /** * A 32-bit (r,g,b,a) color. */ function ColorF(r, g, b, a) { if (r === void 0) { r = 0; } if (g === void 0) { g = 0; } if (b === void 0) { b = 0; } if (a === void 0) { a = 0; } this.r = r; this.g = g; this.b = b; this.a = a; } ColorF.random = function () { var colorF = new ColorF(); colorF.setRandom(); return colorF; }; ColorF.fromColor = function (src) { var colorF = new ColorF(); colorF.setFromColor(src); return colorF; }; ColorF.fromArgbString = function (argb) { var colorF = new ColorF(); colorF.setFromArgbString(argb); return colorF; }; ColorF.fromRgbaInt = function (rgba) { var colorF = new ColorF(); colorF.setFromRgbaInt(rgba); return colorF; }; ColorF.create = function (other) { var colorF = new ColorF(); colorF.set(other); return colorF; }; /** * Checks if this ColorF is fully opaque. */ ColorF.prototype.isOpaque = function () { return this.a === 1; }; /** * Checks if this Color is fully transparent. */ ColorF.prototype.isTransparent = function () { return this.a === 0; }; /** * Randomly sets the (r,g,b) components of this Color. */ ColorF.prototype.setRandom = function () { this.r = Math.random(); this.g = Math.random(); this.b = Math.random(); }; /** * Extracts the (r,g,b,a) components of the specified Color into this ColorF. */ ColorF.prototype.setFromColor = function (src) { this.r = src.r / 0xff; this.g = src.g / 0xff; this.b = src.b / 0xff; this.a = src.a / 0xff; }; /** * Extracts the (r,g,b,a) components of the specified ARGB string into this ColorF. * @param argb hexadecimal string of the form #aarrggbb. */ ColorF.prototype.setFromArgbString = function (argb) { var result = util_1.ArgbRegex.exec(argb); this.a = parseInt(result[1], 16) / 0xff; this.r = parseInt(result[2], 16) / 0xff; this.g = parseInt(result[3], 16) / 0xff; this.b = parseInt(result[4], 16) / 0xff; }; /** * Creates an ARGB string from this specified ColorF's (r,g,b,a) components. * @returns string of the form #aarrggbb */ ColorF.prototype.toArgbString = function () { var r = util_1.pad((this.r * 0xff).toString(16)); var g = util_1.pad((this.g * 0xff).toString(16)); var b = util_1.pad((this.b * 0xff).toString(16)); var a = util_1.pad((this.a * 0xff).toString(16)); return '#' + a + r + g + b; }; /** * Extracts the (r,g,b,a) components of the specified RGBA int into this ColorF. * @param rgba integer of the form 0xrrggbbaa. */ ColorF.prototype.setFromRgbaInt = function (rgba) { this.r = ((rgba >> 24) & 0xff) / 0xff; this.g = ((rgba >> 16) & 0xff) / 0xff; this.b = ((rgba >> 8) & 0xff) / 0xff; this.a = ((rgba >> 0) & 0xff) / 0xff; }; /** * Creates an RGBA int with values extracted from this ColorF. * @returns int of the form 0xrrggbbaa */ ColorF.prototype.toRgbaInt = function () { var r = (this.r * 0xff) << 24; var g = (this.g * 0xff) << 16; var b = (this.b * 0xff) << 8; var a = (this.a * 0xff) << 0; return (r | g | b | a) >>> 0; }; /** * Premultiplies the (r,g,b) components of this ColorF by it's alpha component. */ ColorF.prototype.premultiplyAlpha = function () { var a = this.a; this.r *= a; this.g *= a; this.b *= a; }; /** * Sets each component of this ColorF to that of the other ColorF. */ ColorF.prototype.set = function (other) { this.r = other.r; this.g = other.g; this.b = other.b; this.a = other.a; }; /** * Sets each component of this ColorF. */ ColorF.prototype.set$ = function (r, g, b, a) { this.r = r; this.g = g; this.b = b; this.a = a; }; /** * Sets each component of this ColorF to the specified scalar. */ ColorF.prototype.setScalar = function (k) { this.r = k; this.g = k; this.b = k; this.a = k; }; /** * Adds the other ColorF to this ColorF componentwise. */ ColorF.prototype.add = function (other) { this.r += other.r; this.g += other.g; this.b += other.b; this.a += other.a; }; /** * Adds the specified values to this ColorF componentwise. */ ColorF.prototype.add$ = function (r, g, b, a) { this.r += r; this.g += g; this.b += b; this.a += a; }; /** * Subtracts the other ColorF from this ColorF componentwise. */ ColorF.prototype.subtract = function (other) { this.r -= other.r; this.g -= other.g; this.b -= other.b; this.a -= other.a; }; /** * Subtracts the specified values from this ColorF componentwise. */ ColorF.prototype.subtract$ = function (r, g, b, a) { this.r -= r; this.g -= g; this.b -= b; this.a -= a; }; /** * Multiplies each component of this ColorF by the specified scalar. */ ColorF.prototype.mulScalar = function (k) { this.r *= k; this.g *= k; this.b *= k; this.a *= k; }; /** * Divides each component of this ColorF by the specified scalar. */ ColorF.prototype.divScalar = function (k) { this.r /= k; this.g /= k; this.b /= k; this.a /= k; }; /** * Checks if each component of this ColorF is exactly equal to that of the other ColorF. */ ColorF.prototype.equals = function (other) { return this.r === other.r && this.g === other.g && this.b === other.b && this.a === other.a; }; /** * Checks if each component of this ColorF is exactly equal to the specified scalar. */ ColorF.prototype.equalsScalar = function (k) { return this.r === k && this.g === k && this.b === k && this.a === k; }; /** * Checks if each component of this ColorF is approximately equal to that of the other ColorF. */ ColorF.prototype.epsilonEquals = function (other, e) { return Math.abs(this.r - other.r) <= e && Math.abs(this.g - other.g) <= e && Math.abs(this.b - other.b) <= e && Math.abs(this.a - other.a) <= e; }; /** * Checks if each component of this ColorF is approximately equal to the specified scalar. */ ColorF.prototype.epsilonEqualsScalar = function (k, e) { return Math.abs(this.r - k) <= e && Math.abs(this.g - k) <= e && Math.abs(this.b - k) <= e && Math.abs(this.a - k) <= e; }; /** * Returns a string representation of this ColorF. */ ColorF.prototype.toString = function () { return "{ r: " + this.r + ", g: " + this.g + ", b: " + this.b + ", a: " + this.a + " }"; }; return ColorF; }()); exports.ColorF = ColorF; /** * A ColorF backed by a Float32Array. */ var ColorFStruct = (function (_super) { __extends(ColorFStruct, _super); /** * Creates a ColorF struct backed by the specified data. */ function ColorFStruct(data) { if (data === void 0) { data = new Float32Array(4); } return _super.call(this, data) || this; } ColorFStruct.random = function () { var colorF = new ColorFStruct(); colorF.setRandom(); return colorF; }; ColorFStruct.fromColor = function (src) { var colorF = new ColorFStruct(); colorF.setFromColor(src); return colorF; }; ColorFStruct.fromArgbString = function (argb) { var colorF = new ColorFStruct(); colorF.setFromArgbString(argb); return colorF; }; ColorFStruct.fromRgbaInt = function (rgba) { var colorF = new ColorFStruct(); colorF.setFromRgbaInt(rgba); return colorF; }; ColorFStruct.create = function (other) { var colorF = new ColorFStruct(); colorF.set(other); return colorF; }; ColorFStruct.create$ = function (r, g, b, a) { var colorF = new ColorFStruct(); colorF.set$(r, g, b, a); return colorF; }; Object.defineProperty(ColorFStruct.prototype, "r", { /** * The red component of this ColorF. */ get: function () { return this.data[0]; }, /** * The red component of this ColorF. */ set: function (value) { this.data[0] = value; }, enumerable: true, configurable: true }); Object.defineProperty(ColorFStruct.prototype, "g", { /** * The green component of this ColorF. */ get: function () { return this.data[1]; }, /** * The green component of this ColorF. */ set: function (value) { this.data[1] = value; }, enumerable: true, configurable: true }); Object.defineProperty(ColorFStruct.prototype, "b", { /** * The blue component of this ColorF. */ get: function () { return this.data[2]; }, /** * The blue component of this ColorF. */ set: function (value) { this.data[2] = value; }, enumerable: true, configurable: true }); Object.defineProperty(ColorFStruct.prototype, "a", { /** * The alpha component of this ColorF. */ get: function () { return this.data[3]; }, /** * The alpha component of this ColorF. */ set: function (value) { this.data[3] = value; }, enumerable: true, configurable: true }); return ColorFStruct; }(struct_1.Struct)); exports.ColorFStruct = ColorFStruct; mixin_1.applyMixins(ColorFStruct, ColorF); /** * A ColorF buffer backed by a Float32Array. */ var ColorFBuffer = (function (_super) { __extends(ColorFBuffer, _super); function ColorFBuffer() { return _super !== null && _super.apply(this, arguments) || this; } /** * Creates an empty ColorF buffer with the specified ColorF capacity. */ ColorFBuffer.create = function (capacity) { return new ColorFBuffer(new Float32Array(capacity * 4)); }; Object.defineProperty(ColorFBuffer.prototype, "r", { /** * The red component of the current ColorF. */ get: function () { return this.data[this.dataPosition + 0]; }, /** * The red component of the current ColorF. */ set: function (value) { this.data[this.dataPosition + 0] = value; }, enumerable: true, configurable: true }); Object.defineProperty(ColorFBuffer.prototype, "g", { /** * The green component of the current ColorF. */ get: function () { return this.data[this.dataPosition + 1]; }, /** * The green component of the current ColorF. */ set: function (value) { this.data[this.dataPosition + 1] = value; }, enumerable: true, configurable: true }); Object.defineProperty(ColorFBuffer.prototype, "b", { /** * The blue component of the current ColorF. */ get: function () { return this.data[this.dataPosition + 2]; }, /** * The blue component of the current ColorF. */ set: function (value) { this.data[this.dataPosition + 2] = value; }, enumerable: true, configurable: true }); Object.defineProperty(ColorFBuffer.prototype, "a", { /** * The alpha component of the current ColorF. */ get: function () { return this.data[this.dataPosition + 3]; }, /** * The alpha component of the current ColorF. */ set: function (value) { this.data[this.dataPosition + 3] = value; }, enumerable: true, configurable: true }); /** * Gets the number of properties in a ColorF, namely 4. */ ColorFBuffer.prototype.structLength = function () { return 4; }; /** * Gets the components of the ColorF at the specified position of this buffer. */ ColorFBuffer.prototype.aget = function (position, dst) { if (dst === void 0) { dst = new ColorF(); } ; var dataPos = position * this.structLength(); dst.r = this.data[dataPos++]; dst.g = this.data[dataPos++]; dst.b = this.data[dataPos++]; dst.a = this.data[dataPos++]; return dst; }; /** * Gets the components of the current ColorF, then moves to the next position of this buffer. */ ColorFBuffer.prototype.rget = function (dst) { if (dst === void 0) { dst = new ColorF(); } ; dst.r = this.data[this.dataPosition++]; dst.g = this.data[this.dataPosition++]; dst.b = this.data[this.dataPosition++]; dst.a = this.data[this.dataPosition++]; return dst; }; /** * Sets each component of the ColorF at the specified position to that of the src ColorF. */ ColorFBuffer.prototype.aset = function (position, src) { var dataPos = position * this.structLength(); this.data[dataPos++] = src.r; this.data[dataPos++] = src.g; this.data[dataPos++] = src.b; this.data[dataPos++] = src.a; }; /** * Sets each component of the ColorF at the specified position. */ ColorFBuffer.prototype.aset$ = function (position, r, g, b, a) { var dataPos = position * this.structLength(); this.data[dataPos++] = r; this.data[dataPos++] = g; this.data[dataPos++] = b; this.data[dataPos++] = a; }; /** * Sets each component of the current ColorF to that of the src ColorF, then moves to the next position of this buffer. */ ColorFBuffer.prototype.rset = function (src) { this.data[this.dataPosition++] = src.r; this.data[this.dataPosition++] = src.g; this.data[this.dataPosition++] = src.b; this.data[this.dataPosition++] = src.a; }; /** * Sets each component of the current ColorF, then moves to the next position of this buffer. */ ColorFBuffer.prototype.rset$ = function (r, g, b, a) { this.data[this.dataPosition++] = r; this.data[this.dataPosition++] = g; this.data[this.dataPosition++] = b; this.data[this.dataPosition++] = a; }; return ColorFBuffer; }(buffer_1.StructBuffer)); exports.ColorFBuffer = ColorFBuffer; mixin_1.applyMixins(ColorFBuffer, ColorF); //# sourceMappingURL=colorF.js.map