UNPKG

gl2d

Version:

2D graphics package for WebGL

519 lines 16.5 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"); /** * An 8-bit (r,g,b,a) color. */ var Color = (function () { /** * An 8-bit (r,g,b,a) color. */ function Color(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; } Color.random = function () { var color = new Color(); color.setRandom(); return color; }; Color.fromColorF = function (src) { var color = new Color(); color.setFromColorF(src); return color; }; Color.fromArgbString = function (argb) { var color = new Color(); color.setFromArgbString(argb); return color; }; Color.fromRgbaInt = function (rgba) { var color = new Color(); color.setFromRgbaInt(rgba); return color; }; Color.create = function (other) { var color = new Color(); color.set(other); return color; }; /** * Checks if this Color is fully opaque. */ Color.prototype.isOpaque = function () { return this.a === 0xff; }; /** * Checks if this Color is fully transparent. */ Color.prototype.isTransparent = function () { return this.a === 0; }; /** * Randomly sets the (r,g,b) components of this color. */ Color.prototype.setRandom = function () { this.r = util_1.randomInt(0, 0xff); this.g = util_1.randomInt(0, 0xff); this.b = util_1.randomInt(0, 0xff); }; /** * Extracts the (r,g,b,a) components of the specified ColorF into this color. */ Color.prototype.setFromColorF = function (src) { this.r = (src.r * 0xff) >> 0; this.g = (src.g * 0xff) >> 0; this.b = (src.b * 0xff) >> 0; this.a = (src.a * 0xff) >> 0; }; /** * Extracts the (r,g,b,a) components of the specified ARGB string into this Color. * @param argb hexadecimal string of the form #aarrggbb. */ Color.prototype.setFromArgbString = function (argb) { var result = util_1.ArgbRegex.exec(argb); this.a = parseInt(result[1], 16); this.r = parseInt(result[2], 16); this.g = parseInt(result[3], 16); this.b = parseInt(result[4], 16); }; /** * Creates an ARGB string from this Color's (r,g,b,a) components. * @returns string of the form #aarrggbb */ Color.prototype.toArgbString = function () { var r = util_1.pad(this.r.toString(16)); var g = util_1.pad(this.g.toString(16)); var b = util_1.pad(this.b.toString(16)); var a = util_1.pad(this.a.toString(16)); return '#' + a + r + g + b; }; /** * Extracts the (r,g,b,a) components of the specified RGBA int into this color. * @param rgba integer of the form 0xrrggbbaa. */ Color.prototype.setFromRgbaInt = function (rgba) { this.r = (rgba >> 24) & 0xff; this.g = (rgba >> 16) & 0xff; this.b = (rgba >> 8) & 0xff; this.a = (rgba >> 0) & 0xff; }; /** * Creates an RGBA int with this Color's (r,g,b,a) components. * @returns int of the form 0xrrggbbaa */ Color.prototype.toRgbaInt = function () { var r = this.r << 24; var g = this.g << 16; var b = this.b << 8; var a = this.a << 0; return (r | g | b | a) >>> 0; }; /** * Blends the source color into this color using (src.alpha, 1-src.alpha) blend mode. */ Color.prototype.blend = function (src) { var alpha = src.a + 1, invAlpha = 256 - src.a; this.r = (alpha * src.r + invAlpha * this.r) >> 8; this.g = (alpha * src.g + invAlpha * this.g) >> 8; this.b = (alpha * src.b + invAlpha * this.b) >> 8; }; /** * Sets each component of this Color to that of the other Color. */ Color.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 Color. */ Color.prototype.set$ = function (r, g, b, a) { this.r = r; this.g = g; this.b = b; this.a = a; }; /** * Sets each component of this Color to the specified scalar. */ Color.prototype.setScalar = function (k) { this.r = k; this.g = k; this.b = k; this.a = k; }; /** * Adds the other Color to this Color componentwise. */ Color.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 Color componentwise. */ Color.prototype.add$ = function (r, g, b, a) { this.r += r; this.g += g; this.b += b; this.a += a; }; /** * Subtracts the other Color from this Color componentwise. */ Color.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 Color componentwise. */ Color.prototype.subtract$ = function (r, g, b, a) { this.r -= r; this.g -= g; this.b -= b; this.a -= a; }; /** * Multiplies each component of this Color by the specified scalar. */ Color.prototype.mulScalar = function (k) { this.r *= k; this.g *= k; this.b *= k; this.a *= k; }; /** * Divides each component of this Color by the specified scalar. */ Color.prototype.divScalar = function (k) { this.r /= k; this.g /= k; this.b /= k; this.a /= k; }; /** * Checks if each component of this Color is exactly equal to that of the other Color. */ Color.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 Color is exactly equal to the specified scalar. */ Color.prototype.equalsScalar = function (k) { return this.r === k && this.g === k && this.b === k && this.a === k; }; /** * Checks if each component of this Color is approximately equal to that of the other Color. */ Color.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 Color is approximately equal to the specified scalar. */ Color.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 Color. */ Color.prototype.toString = function () { return "{ r: " + this.r + ", g: " + this.g + ", b: " + this.b + ", a: " + this.a + " }"; }; return Color; }()); exports.Color = Color; /** * A Color backed by a Uint8Array. */ var ColorStruct = (function (_super) { __extends(ColorStruct, _super); /** * Creates a Color struct backed by the specified data. */ function ColorStruct(data) { if (data === void 0) { data = new Uint8Array(4); } return _super.call(this, data) || this; } ColorStruct.random = function () { var color = new ColorStruct(); color.setRandom(); return color; }; ColorStruct.fromColorF = function (src) { var color = new ColorStruct(); color.setFromColorF(src); return color; }; ColorStruct.fromArgbString = function (argb) { var color = new ColorStruct(); color.setFromArgbString(argb); return color; }; ColorStruct.fromRgbaInt = function (rgba) { var color = new ColorStruct(); color.setFromRgbaInt(rgba); return color; }; ColorStruct.create = function (other) { var color = new ColorStruct(); color.set(other); return color; }; ColorStruct.create$ = function (r, g, b, a) { var color = new ColorStruct(); color.set$(r, g, b, a); return color; }; Object.defineProperty(ColorStruct.prototype, "r", { /** * The red component of this Color. */ get: function () { return this.data[0]; }, /** * The red component of this Color. */ set: function (value) { this.data[0] = value; }, enumerable: true, configurable: true }); Object.defineProperty(ColorStruct.prototype, "g", { /** * The green component of this Color. */ get: function () { return this.data[1]; }, /** * The green component of this Color. */ set: function (value) { this.data[1] = value; }, enumerable: true, configurable: true }); Object.defineProperty(ColorStruct.prototype, "b", { /** * The blue component of this Color. */ get: function () { return this.data[2]; }, /** * The blue component of this Color. */ set: function (value) { this.data[2] = value; }, enumerable: true, configurable: true }); Object.defineProperty(ColorStruct.prototype, "a", { /** * The alpha component of this Color. */ get: function () { return this.data[3]; }, /** * The alpha component of this Color. */ set: function (value) { this.data[3] = value; }, enumerable: true, configurable: true }); return ColorStruct; }(struct_1.Struct)); exports.ColorStruct = ColorStruct; mixin_1.applyMixins(ColorStruct, Color); /** * A Color buffer backed by a Uint8Array. */ var ColorBuffer = (function (_super) { __extends(ColorBuffer, _super); function ColorBuffer() { return _super !== null && _super.apply(this, arguments) || this; } /** * Creates an empty Color buffer with the specified Color capacity. */ ColorBuffer.create = function (capacity) { return new ColorBuffer(new Uint8Array(capacity * 4)); }; Object.defineProperty(ColorBuffer.prototype, "r", { /** * The red component of the current Color. */ get: function () { return this.data[this.dataPosition + 0]; }, /** * The red component of the current Color. */ set: function (value) { this.data[this.dataPosition + 0] = value; }, enumerable: true, configurable: true }); Object.defineProperty(ColorBuffer.prototype, "g", { /** * The green component of the current Color. */ get: function () { return this.data[this.dataPosition + 1]; }, /** * The green component of the current Color. */ set: function (value) { this.data[this.dataPosition + 1] = value; }, enumerable: true, configurable: true }); Object.defineProperty(ColorBuffer.prototype, "b", { /** * The blue component of the current Color. */ get: function () { return this.data[this.dataPosition + 2]; }, /** * The blue component of the current Color. */ set: function (value) { this.data[this.dataPosition + 2] = value; }, enumerable: true, configurable: true }); Object.defineProperty(ColorBuffer.prototype, "a", { /** * The alpha component of the current Color. */ get: function () { return this.data[this.dataPosition + 3]; }, /** * The alpha component of the current Color. */ set: function (value) { this.data[this.dataPosition + 3] = value; }, enumerable: true, configurable: true }); /** * Gets the number of properties in a Color, namely 4. */ ColorBuffer.prototype.structLength = function () { return 4; }; /** * Gets the components of the Color at the specified position of this buffer. */ ColorBuffer.prototype.aget = function (position, dst) { if (dst === void 0) { dst = new Color(); } ; 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 Color, then moves to the next position of this buffer. */ ColorBuffer.prototype.rget = function (dst) { if (dst === void 0) { dst = new Color(); } ; 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 Color at the specified position to that of the src Color. */ ColorBuffer.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 Color at the specified position. */ ColorBuffer.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 Color to that of the src Color, then moves to the next position of this buffer. */ ColorBuffer.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 Color, then moves to the next position of this buffer. */ ColorBuffer.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 ColorBuffer; }(buffer_1.StructBuffer)); exports.ColorBuffer = ColorBuffer; mixin_1.applyMixins(ColorBuffer, Color); //# sourceMappingURL=color.js.map