Home Reference Source

stdlib/obj/color.js

"use strict";

Object.defineProperty(exports, "__esModule", {
	value: true
});

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

/**
 * Represents a color in the Source Engine
 */
var Color = exports.Color = function () {
	/**
  * Creates an empty color
  */
	function Color() {
		_classCallCheck(this, Color);

		this._r = 0;
		this._g = 0;
		this._b = 0;
		this._a = 255;
		this._schemaKey = undefined;
	}

	/**
  * Parse a Color object from a string
  * @param {string} data - The data to parse
  * @return {Color}
  */


	_createClass(Color, [{
		key: "toString",
		value: function toString() {
			if (this._schemaKey == undefined) {
				return this.r + " " + this.g + " " + this.b + " " + this.a;
			}
			return this._schemaKey;
		}
	}, {
		key: "schemaKey",


		/**
   * The name of the lookup key in the schema file
   * @return {string}
   */
		get: function get() {
			return this._schemaKey;
		}

		/**
   * @type {string}
   */
		,
		set: function set(value) {
			this._schemaKey = value;
		}

		/**
   * The red component
   * @return {number}
   */

	}, {
		key: "r",
		get: function get() {
			if (this._schemaKey != undefined) throw new Error("Unable to fetch schema from javascript, unknown");
			return this._r;
		}
		/**
   * The green component
   * @return {number}
   */
		,


		/**
   * Sets the red component
   * @param {number} value
   */
		set: function set(value) {
			this._r = Color.clamp(value);
		}
		/**
   * Sets the green component
   * @param {number} value
   */

	}, {
		key: "g",
		get: function get() {
			if (this._schemaKey != undefined) throw new Error("Unable to fetch schema from javascript, unknown");
			return this._g;
		}
		/**
   * The blue component
   * @return {number}
   */
		,
		set: function set(value) {
			this._g = Color.clamp(value);
		}
		/**
   * Sets the blue component
   * @param {number} value
   */

	}, {
		key: "b",
		get: function get() {
			if (this._schemaKey != undefined) throw new Error("Unable to fetch schema from javascript, unknown");
			return this._b;
		}
		/**
   * The alpha component
   * @return {number}
   */
		,
		set: function set(value) {
			this._b = Color.clamp(value);
		}
		/**
   * Sets the alpha component
   * @param {number} value
   */

	}, {
		key: "a",
		get: function get() {
			if (this._schemaKey != undefined) throw new Error("Unable to fetch schema from javascript, unknown");
			return this._a;
		},
		set: function set(value) {
			this._a = Color.clamp(value);
		}
	}], [{
		key: "parse",
		value: function parse(data) {
			if (!data) throw new Error("No data given");

			var result = new Color();

			var split = data.split(' ');
			if (split.length == 4) {
				result.r = parseInt(split[0]);
				result.g = parseInt(split[1]);
				result.b = parseInt(split[2]);
				result.a = parseInt(split[3]);
			} else {
				result._schemaKey = data;
			}
			return result;
		}

		/**
   * Claps the value to the valid range [0-255]
   * @param {number} i
   * @return {number}
   */

	}, {
		key: "clamp",
		value: function clamp(i) {
			var result = Math.round(i);
			if (result < 0) result = 0;
			if (result > 255) result = 255;
			return result;
		}
	}]);

	return Color;
}();