ColorTracker.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. 'use strict';
  2. var tracking = require('./utils/sandbox.js');
  3. module.exports = {
  4. setUp: function(done) {
  5. done();
  6. },
  7. tearDown: function(done) {
  8. done();
  9. },
  10. testConstructorEmpty: function(test) {
  11. var colors;
  12. var tracker;
  13. test.doesNotThrow(function() {
  14. tracker = new tracking.ColorTracker();
  15. });
  16. colors = tracker.getColors();
  17. test.equal(1, colors.length, 'Colors array should have a single value');
  18. test.equal('magenta', colors[0], 'Default color is magenta');
  19. test.done();
  20. },
  21. testConstructorString: function(test) {
  22. var colors;
  23. var tracker;
  24. test.doesNotThrow(function() {
  25. tracker = new tracking.ColorTracker('yellow');
  26. });
  27. colors = tracker.getColors();
  28. test.equal(1, colors.length, 'Colors array should have a single value');
  29. test.equal('yellow', colors[0], 'The colors array should be set to value in the constructor');
  30. test.throws(function() {
  31. tracker = new tracking.ColorTracker('notvalid');
  32. });
  33. test.done();
  34. },
  35. testConstructorArray: function(test) {
  36. var colors;
  37. var tracker;
  38. test.doesNotThrow(function() {
  39. tracker = new tracking.ColorTracker([]);
  40. });
  41. colors = tracker.getColors();
  42. test.equal(0, colors.length, 'Colors array should be empty');
  43. test.doesNotThrow(function() {
  44. tracker = new tracking.ColorTracker(['magenta', 'cyan', 'yellow']);
  45. });
  46. colors = tracker.getColors();
  47. test.equal(3, colors.length, 'Colors array have 3 values');
  48. test.equal('magenta', colors[0], 'The colors array should be set to values in the constructor');
  49. test.equal('cyan', colors[1], 'The colors array should be set to values in the constructor');
  50. test.equal('yellow', colors[2], 'The colors array should be set to values in the constructor');
  51. test.throws(function() {
  52. tracker = new tracking.ColorTracker(['magenta', null, 'yellow']);
  53. });
  54. test.done();
  55. },
  56. testFindColor: function(test) {
  57. var colors;
  58. var pixels;
  59. var tracker;
  60. tracking.ColorTracker.registerColor('black', function(r, g, b) {
  61. return r === 0 && g === 0 && b === 0;
  62. });
  63. tracker = new tracking.ColorTracker('black');
  64. colors = tracker.getColors();
  65. test.equal(1, colors.length, 'Colors array have a single value');
  66. test.equal('black', colors[0], 'The colors array should be set to values in the constructor');
  67. tracker.setMinDimension(2);
  68. tracker.setMinGroupSize(6);
  69. pixels = [
  70. 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0,
  71. 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
  72. 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1,
  73. 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
  74. ];
  75. tracker.on('track', function(event) {
  76. test.equal(1, event.data.length, 'There should only be one result rectangle');
  77. test.equal(1, event.data[0].x, 'The first rectangle should be at x = 1');
  78. test.equal(0, event.data[0].y, 'The first rectangle should be at y = 0');
  79. test.equal(2, event.data[0].width, 'The first rectangle\'s width should be 2');
  80. test.equal(3, event.data[0].height, 'The first rectangle\'s height should be 3');
  81. test.done();
  82. });
  83. tracker.track(pixels, 5, 4);
  84. },
  85. testMergedRectangles: function(test) {
  86. var pixels;
  87. var tracker;
  88. tracking.ColorTracker.registerColor('black', function(r, g, b) {
  89. return r === 0 && g === 0 && b === 0;
  90. });
  91. tracker = new tracking.ColorTracker('black');
  92. tracker.setMinDimension(1);
  93. tracker.setMinGroupSize(6);
  94. pixels = [
  95. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  96. 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
  97. 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0,
  98. 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0,
  99. 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0,
  100. 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
  101. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  102. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  103. 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0,
  104. 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0,
  105. 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0
  106. ];
  107. tracker.on('track', function(event) {
  108. test.equal(2, event.data.length, 'There should be 2 result rectangles');
  109. test.equal(0, event.data[0].x, 'The first rectangle should be at x = 0');
  110. test.equal(0, event.data[0].y, 'The first rectangle should be at y = 0');
  111. test.equal(5, event.data[0].width, 'The first rectangle\'s width should be 5');
  112. test.equal(6, event.data[0].height, 'The first rectangle\'s height should be 6');
  113. test.equal(2, event.data[1].x, 'The second rectangle should be at x = 2');
  114. test.equal(8, event.data[1].y, 'The second rectangle should be at y = 8');
  115. test.equal(1, event.data[1].width, 'The second rectangle\'s width should be 1');
  116. test.equal(2, event.data[1].height, 'The second rectangle\'s height should be 2');
  117. test.done();
  118. });
  119. tracker.track(pixels, 6, 11);
  120. },
  121. testDimensionConstraints: function(test) {
  122. var pixels;
  123. var tracker;
  124. tracking.ColorTracker.registerColor('black', function(r, g, b) {
  125. return r === 0 && g === 0 && b === 0;
  126. });
  127. tracker = new tracking.ColorTracker('black');
  128. tracker.setMinDimension(1);
  129. tracker.setMaxDimension(2);
  130. tracker.setMinGroupSize(6);
  131. pixels = [
  132. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  133. 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
  134. 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0,
  135. 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0,
  136. 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0,
  137. 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
  138. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  139. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  140. 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0,
  141. 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0,
  142. 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0
  143. ];
  144. tracker.on('track', function(event) {
  145. test.equal(1, event.data.length, 'There should be 1 result rectangle');
  146. test.equal(1, event.data[0].width, 'The rectangle\'s width should be 1');
  147. test.equal(2, event.data[0].height, 'The rectangle\'s height should be 2');
  148. test.done();
  149. });
  150. tracker.track(pixels, 6, 11);
  151. }
  152. };