topical media & game development

talk show tell print

mobile-graphic-enchant-dev-src-DetectColorManager.js / js



  enchant.DetectColorManager = enchant.Class.create({
      initialize: function(reso, max) {
          this.reference = [];
          this.colorResolution = reso || 16;
          this.max = max || 1;
          this.capacity = Math.pow(this.colorResolution, 3);
          for (var i = 1, l = this.capacity; i < l; i++) {
              this.reference[i] = null;
          }
      },
      attachDetectColor: function(sprite) {
          var i = this.reference.indexOf(null);
          if (i === -1) {
              i = 1;
          }
          this.reference[i] = sprite;
          return this._getColor(i);
      },
      detachDetectColor: function(sprite) {
          var i = this.reference.indexOf(sprite);
          if (i !== -1) {
              this.reference[i] = null;
          }
      },
      _getColor: function(n) {
          var C = this.colorResolution;
          var d = C / this.max;
          return [
              parseInt((n / C / C) % C, 10) / d,
              parseInt((n / C) % C, 10) / d,
              parseInt(n % C, 10) / d,
              1.0
          ];
      },
      _decodeDetectColor: function(color) {
          var C = this.colorResolution;
          return ~~(color[0] * C * C * C / 256) +
              ~~(color[1] * C * C / 256) +
              ~~(color[2] * C / 256);
      },
      getSpriteByColor: function(color) {
          return this.reference[this._decodeDetectColor(color)];
      }
  });
  


(C) Æliens 04/09/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.