|
| 1 | +/** |
| 2 | + * @author Richard Davey <rich@photonstorm.com> |
| 3 | + * @copyright 2020 Photon Storm Ltd. |
| 4 | + * @license {@link https://opensource.org/licenses/MIT|MIT License} |
| 5 | + */ |
| 6 | + |
| 7 | +var Class = require('../utils/Class'); |
| 8 | +var NOOP = require('../utils/NOOP'); |
| 9 | + |
| 10 | +/** |
| 11 | + * @classdesc |
| 12 | + * The RGB class holds a single color value and allows for easy modification and reading of it, |
| 13 | + * with optional on-change callback notification and a dirty flag. |
| 14 | + * |
| 15 | + * @class RGB |
| 16 | + * @memberof Phaser.Display |
| 17 | + * @constructor |
| 18 | + * @since 3.50.0 |
| 19 | + * |
| 20 | + * @param {number} [red=0] - The red color value. A number between 0 and 1. |
| 21 | + * @param {number} [green=0] - The green color value. A number between 0 and 1. |
| 22 | + * @param {number} [blue=0] - The blue color value. A number between 0 and 1. |
| 23 | + */ |
| 24 | +var RGB = new Class({ |
| 25 | + |
| 26 | + initialize: |
| 27 | + |
| 28 | + function RGB (red, green, blue) |
| 29 | + { |
| 30 | + /** |
| 31 | + * Cached RGB values. |
| 32 | + * |
| 33 | + * @name Phaser.Display.RGB#_rgb |
| 34 | + * @type {number[]} |
| 35 | + * @private |
| 36 | + * @since 3.50.0 |
| 37 | + */ |
| 38 | + this._rgb = [ 0, 0, 0 ]; |
| 39 | + |
| 40 | + /** |
| 41 | + * This callback will be invoked each time one of the RGB color values change. |
| 42 | + * |
| 43 | + * The callback is sent the new color values as the parameters. |
| 44 | + * |
| 45 | + * @name Phaser.Display.RGB#onChangeCallback |
| 46 | + * @type {function} |
| 47 | + * @since 3.50.0 |
| 48 | + */ |
| 49 | + this.onChangeCallback = NOOP; |
| 50 | + |
| 51 | + /** |
| 52 | + * Is this color dirty? |
| 53 | + * |
| 54 | + * @name Phaser.Display.RGB#dirty |
| 55 | + * @type {boolean} |
| 56 | + * @since 3.50.0 |
| 57 | + */ |
| 58 | + this.dirty = false; |
| 59 | + |
| 60 | + this.set(red, green, blue); |
| 61 | + }, |
| 62 | + |
| 63 | + /** |
| 64 | + * Sets the red, green and blue values of this RGB object, flags it as being |
| 65 | + * dirty and then invokes the `onChangeCallback`, if set. |
| 66 | + * |
| 67 | + * @method Phaser.Display.RGB#set |
| 68 | + * @since 3.50.0 |
| 69 | + * |
| 70 | + * @param {number} [red=0] - The red color value. A number between 0 and 1. |
| 71 | + * @param {number} [green=0] - The green color value. A number between 0 and 1. |
| 72 | + * @param {number} [blue=0] - The blue color value. A number between 0 and 1. |
| 73 | + * |
| 74 | + * @return {this} This RGB instance. |
| 75 | + */ |
| 76 | + set: function (red, green, blue) |
| 77 | + { |
| 78 | + if (red === undefined) { red = 0; } |
| 79 | + if (green === undefined) { green = 0; } |
| 80 | + if (blue === undefined) { blue = 0; } |
| 81 | + |
| 82 | + this._rgb = [ red, green, blue ]; |
| 83 | + |
| 84 | + this.onChange(); |
| 85 | + |
| 86 | + return this; |
| 87 | + }, |
| 88 | + |
| 89 | + /** |
| 90 | + * Compares the given rgb parameters with those in this object and returns |
| 91 | + * a boolean `true` value if they are equal, otherwise it returns `false`. |
| 92 | + * |
| 93 | + * @method Phaser.Display.RGB#equals |
| 94 | + * @since 3.50.0 |
| 95 | + * |
| 96 | + * @param {number} red - The red value to compare with this object. |
| 97 | + * @param {number} green - The green value to compare with this object. |
| 98 | + * @param {number} blue - The blue value to compare with this object. |
| 99 | + * |
| 100 | + * @return {boolean} `true` if the given values match those in this object, otherwise `false`. |
| 101 | + */ |
| 102 | + equals: function (red, green, blue) |
| 103 | + { |
| 104 | + var rgb = this._rgb; |
| 105 | + |
| 106 | + return (rgb.r === red && rgb.g === green && rgb.b === blue); |
| 107 | + }, |
| 108 | + |
| 109 | + /** |
| 110 | + * Internal on change handler. Sets this object as being dirty and |
| 111 | + * then invokes the `onChangeCallback`, if set, passing in the |
| 112 | + * new RGB values. |
| 113 | + * |
| 114 | + * @method Phaser.Display.RGB#onChange |
| 115 | + * @since 3.50.0 |
| 116 | + */ |
| 117 | + onChange: function () |
| 118 | + { |
| 119 | + this.dirty = true; |
| 120 | + |
| 121 | + var rgb = this._rgb; |
| 122 | + |
| 123 | + this.onChangeCallback.call(this, rgb[0], rgb[1], rgb[2]); |
| 124 | + }, |
| 125 | + |
| 126 | + /** |
| 127 | + * The red color value. Between 0 and 1. |
| 128 | + * |
| 129 | + * Changing this property will flag this RGB object as being dirty |
| 130 | + * and invoke the `onChangeCallback` , if set. |
| 131 | + * |
| 132 | + * @name Phaser.Display.RGB#r |
| 133 | + * @type {number} |
| 134 | + * @since 3.50.0 |
| 135 | + */ |
| 136 | + r: { |
| 137 | + |
| 138 | + get: function () |
| 139 | + { |
| 140 | + return this._rgb[0]; |
| 141 | + }, |
| 142 | + |
| 143 | + set: function (value) |
| 144 | + { |
| 145 | + this._rgb[0] = value; |
| 146 | + this.onChange(); |
| 147 | + } |
| 148 | + |
| 149 | + }, |
| 150 | + |
| 151 | + /** |
| 152 | + * The green color value. Between 0 and 1. |
| 153 | + * |
| 154 | + * Changing this property will flag this RGB object as being dirty |
| 155 | + * and invoke the `onChangeCallback` , if set. |
| 156 | + * |
| 157 | + * @name Phaser.Display.RGB#g |
| 158 | + * @type {number} |
| 159 | + * @since 3.50.0 |
| 160 | + */ |
| 161 | + g: { |
| 162 | + |
| 163 | + get: function () |
| 164 | + { |
| 165 | + return this._rgb[1]; |
| 166 | + }, |
| 167 | + |
| 168 | + set: function (value) |
| 169 | + { |
| 170 | + this._rgb[1] = value; |
| 171 | + this.onChange(); |
| 172 | + } |
| 173 | + |
| 174 | + }, |
| 175 | + |
| 176 | + /** |
| 177 | + * The blue color value. Between 0 and 1. |
| 178 | + * |
| 179 | + * Changing this property will flag this RGB object as being dirty |
| 180 | + * and invoke the `onChangeCallback` , if set. |
| 181 | + * |
| 182 | + * @name Phaser.Display.RGB#b |
| 183 | + * @type {number} |
| 184 | + * @since 3.50.0 |
| 185 | + */ |
| 186 | + b: { |
| 187 | + |
| 188 | + get: function () |
| 189 | + { |
| 190 | + return this._rgb[2]; |
| 191 | + }, |
| 192 | + |
| 193 | + set: function (value) |
| 194 | + { |
| 195 | + this._rgb[2] = value; |
| 196 | + this.onChange(); |
| 197 | + } |
| 198 | + |
| 199 | + }, |
| 200 | + |
| 201 | + /** |
| 202 | + * Nulls any external references this object contains. |
| 203 | + * |
| 204 | + * @method Phaser.Display.RGB#destroy |
| 205 | + * @since 3.50.0 |
| 206 | + */ |
| 207 | + destroy: function () |
| 208 | + { |
| 209 | + this.onChangeCallback = null; |
| 210 | + } |
| 211 | + |
| 212 | +}); |
| 213 | + |
| 214 | +module.exports = RGB; |
0 commit comments