HSVTest.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. namespace fphammerle\helpers\tests\colors;
  3. use \fphammerle\helpers\colors\HSV;
  4. class HSVTest extends \PHPUnit_Framework_TestCase
  5. {
  6. const comparison_precision = 0.00001;
  7. public function testConstruct0()
  8. {
  9. $c = new HSV();
  10. $this->assertEquals(0.0, $c->hue, '', self::comparison_precision);
  11. $this->assertEquals(0.0, $c->saturation, '', self::comparison_precision);
  12. $this->assertEquals(0.0, $c->value, '', self::comparison_precision);
  13. }
  14. public function testConstruct1()
  15. {
  16. $c = new HSV(pi());
  17. $this->assertEquals(pi(), $c->hue, '', self::comparison_precision);
  18. $this->assertEquals(0.0, $c->saturation, '', self::comparison_precision);
  19. $this->assertEquals(0.0, $c->value, '', self::comparison_precision);
  20. }
  21. public function testConstruct2()
  22. {
  23. $c = new HSV(pi(), 0.2);
  24. $this->assertEquals(pi(), $c->hue, '', self::comparison_precision);
  25. $this->assertEquals(0.2, $c->saturation, '', self::comparison_precision);
  26. $this->assertEquals(0.0, $c->value, '', self::comparison_precision);
  27. }
  28. public function testConstruct3()
  29. {
  30. $c = new HSV(pi(), 0.2, 1);
  31. $this->assertEquals(pi(), $c->hue, '', self::comparison_precision);
  32. $this->assertEquals(0.2, $c->saturation, '', self::comparison_precision);
  33. $this->assertEquals(1.0, $c->value, '', self::comparison_precision);
  34. }
  35. public function equalsProvider()
  36. {
  37. return [
  38. [new HSV(0, 0, 0.0 ), new HSV(0.0, 0.0, 0 )],
  39. [new HSV(0.0, 0.0, 0.0 ), new HSV(0.0, 0.0, 0.0 )],
  40. [new HSV(0.0, 0.4, 0.99), new HSV(0.0, 0.4, 0.99 )],
  41. [new HSV(2.21, 0.4, 0.99), new HSV(2.21, 0.4, 0.99 )],
  42. [new HSV(2.21, 0.9, 0.0 ), new HSV(2.21, 0.9, pow(10, -20))],
  43. [new HSV(3, 0.3, 1 ), new HSV(1/2*6, 0.3, 1.0 )],
  44. [new HSV(pi(), 0.0, 0.0 ), new HSV(deg2rad(180), 0.0, 0.0 )],
  45. [new HSV(pi(), 0.7, 0.0 ), new HSV(deg2rad(180), 0.7, 0.0 )],
  46. [new HSV(sqrt(pi()), sqrt(0.7), sqrt(1/33) ), new HSV(sqrt(deg2rad(180)), sqrt(7)/sqrt(10), 1/sqrt(33))],
  47. ];
  48. }
  49. /**
  50. * @dataProvider equalsProvider
  51. */
  52. public function testEquals($a, $b)
  53. {
  54. $this->assertTrue($a->equals($b));
  55. $this->assertTrue($b->equals($a));
  56. }
  57. public function unequalsProvider()
  58. {
  59. return [
  60. [new HSV(0.0, 0.0, 0.0), new HSV(0.0, 0.0, pow(10, -4) )],
  61. [new HSV(0.0, 0.0, 0.0), new HSV(0.0, pow(10, -4), 0.0 )],
  62. [new HSV(0.0, 0.0, 0.0), new HSV(pow(10, -4), 0.0, 0.0 )],
  63. [new HSV(0.5, 0.5, 0.5), new HSV(0.5+1.0/9999, 0.5, 0.5 )],
  64. [new HSV(0.5, 0.5, 0.5), new HSV(0.5, 0.5+1.0/9999, 0.5 )],
  65. [new HSV(0.5, 0.5, 0.5), new HSV(0.5, 0.5, 0.5+1.0/9999)],
  66. [new HSV(pi(), 1/33, 0.1), new HSV(1.53, pow(10, -4), 0.32 )],
  67. [new HSV(pi(), 1/33, 0.1), new HSV(2.32, 0.32, pow(10, -4) )],
  68. [new HSV(pi(), 1/33, 0.1), new HSV(2.32, 1/33, pow(10, -4) )],
  69. [new HSV(pi(), 1/33, 0.1), new HSV(pi(), 1/33, pow(10, -4) )],
  70. [new HSV(pi(), 1/33, 0.1), new HSV(pi(), pow(10, -4), 0.1 )],
  71. [new HSV(pi(), 1/33, 0.1), new HSV(pi(), pow(10, -4), 0.32 )],
  72. [new HSV(pi(), 1/33, 0.1), new HSV(pow(10, -4), 0.92, 0.1 )],
  73. [new HSV(pi(), 1/33, 0.1), new HSV(pow(10, -4), 0.92, 0.4 )],
  74. [new HSV(pi(), 1/33, 0.1), new HSV(pow(10, -4), 1/33, 0.1 )],
  75. ];
  76. }
  77. /**
  78. * @dataProvider unequalsProvider
  79. */
  80. public function testUnequals($a, $b)
  81. {
  82. $this->assertFalse($a->equals($b));
  83. $this->assertFalse($b->equals($a));
  84. }
  85. public function getHueProvider()
  86. {
  87. return [
  88. [new HSV(0, 0, 0.0 ), 0.0, ],
  89. [new HSV(0.0, 0.0, 0.0 ), 0.0, ],
  90. [new HSV(2.21, 0.4, 0.99 ), 2.21, ],
  91. [new HSV(3, 0.3, 1 ), 1/2*6, ],
  92. [new HSV(pi(), 0.7, 0.0 ), deg2rad(180) ],
  93. [new HSV(sqrt(pi()), sqrt(0.7), 1), sqrt(deg2rad(180))],
  94. ];
  95. }
  96. /**
  97. * @dataProvider getHueProvider
  98. */
  99. public function testGetHue($c, $h)
  100. {
  101. $this->assertEquals($h, $c->hue, '', self::comparison_precision);
  102. $this->assertEquals($h, $c->getHue(), '', self::comparison_precision);
  103. }
  104. public function setHueProvider()
  105. {
  106. return [
  107. [0, 0.0, ],
  108. [0.0, 0.0, ],
  109. [2.21, 2.21, ],
  110. [3, 1/2*6, ],
  111. [pi(), deg2rad(180) ],
  112. [sqrt(pi()), sqrt(deg2rad(180))],
  113. ];
  114. }
  115. /**
  116. * @dataProvider setHueProvider
  117. */
  118. public function testSetHue($s, $g)
  119. {
  120. $c = new HSV(0, 0, 0);
  121. $c->setHue($s);
  122. $this->assertEquals($g, $c->hue, '', self::comparison_precision);
  123. $c = new HSV(0, 0, 0);
  124. $c->hue = $s;
  125. $this->assertEquals($g, $c->hue, '', self::comparison_precision);
  126. }
  127. public function getSaturationProvider()
  128. {
  129. return [
  130. [new HSV(0, 0, 0.0 ), 0.0, ],
  131. [new HSV(0.0, 0.0, 0.0 ), 0.0, ],
  132. [new HSV(2.21, 0.4, 0.99 ), 0.4, ],
  133. [new HSV(3, pi()/2/pi(), 1 ), 0.5, ],
  134. [new HSV(pi(), 1, 0.0 ), 1.0 ],
  135. [new HSV(pi(), sqrt(0.7), 1 ), sqrt(0.7)],
  136. ];
  137. }
  138. /**
  139. * @dataProvider getSaturationProvider
  140. */
  141. public function testGetSaturation($c, $h)
  142. {
  143. $this->assertEquals($h, $c->saturation, '', self::comparison_precision);
  144. $this->assertEquals($h, $c->getSaturation(), '', self::comparison_precision);
  145. }
  146. public function setSaturationProvider()
  147. {
  148. return [
  149. [0, 0.0, ],
  150. [0.0, 0.0, ],
  151. [0.4, 0.4, ],
  152. [pi()/2/pi(), 0.5, ],
  153. [1, 1.0 ],
  154. [sqrt(0.7), sqrt(0.7)],
  155. ];
  156. }
  157. /**
  158. * @dataProvider setSaturationProvider
  159. */
  160. public function testSetSaturation($s, $g)
  161. {
  162. $c = new HSV(0, 0, 0);
  163. $c->setSaturation($s);
  164. $this->assertEquals($g, $c->saturation, '', self::comparison_precision);
  165. $c = new HSV(0, 0, 0);
  166. $c->saturation = $s;
  167. $this->assertEquals($g, $c->saturation, '', self::comparison_precision);
  168. }
  169. public function getValueProvider()
  170. {
  171. return [
  172. [new HSV(0, 0.0, 0 ), 0.0 ],
  173. [new HSV(0.0, 0, 0.0 ), 0.0 ],
  174. [new HSV(2.21, 0.3, 0.4 ), 0.4 ],
  175. [new HSV(3, 1.0, pi()/2/pi()), 0.5 ],
  176. [new HSV(pi(), 0.4, 1 ), 1.0 ],
  177. [new HSV(pi(), 1, sqrt(0.7) ), sqrt(0.7)],
  178. ];
  179. }
  180. /**
  181. * @dataProvider getValueProvider
  182. */
  183. public function testGetValue($c, $h)
  184. {
  185. $this->assertEquals($h, $c->value, '', self::comparison_precision);
  186. $this->assertEquals($h, $c->getValue(), '', self::comparison_precision);
  187. }
  188. public function setValueProvider()
  189. {
  190. return [
  191. [0, 0.0, ],
  192. [0.0, 0.0, ],
  193. [0.4, 0.4, ],
  194. [pi()/2/pi(), 0.5, ],
  195. [1, 1.0 ],
  196. [sqrt(0.7), sqrt(0.7)],
  197. ];
  198. }
  199. /**
  200. * @dataProvider setValueProvider
  201. */
  202. public function testSetValue($s, $g)
  203. {
  204. $c = new HSV(0, 0, 0);
  205. $c->setValue($s);
  206. $this->assertEquals($g, $c->value, '', self::comparison_precision);
  207. $c = new HSV(0, 0, 0);
  208. $c->value = $s;
  209. $this->assertEquals($g, $c->value, '', self::comparison_precision);
  210. }
  211. public function getTupleProvider()
  212. {
  213. return [
  214. [new HSV(0, 0, 0.0 ), [0.0, 0.0, 0.0 ]],
  215. [new HSV(0.0, 0.0, 0.0 ), [0.0, 0.0, 0.0 ]],
  216. [new HSV(2.21, 0.4, 0.99 ), [2.21, 0.4, 0.99 ]],
  217. [new HSV(3, 0.3, 1 ), [3.0, 0.3, 1.0 ]],
  218. [new HSV(pi(), 0.7, 0.0 ), [pi(), 0.7, 0.0 ]],
  219. [new HSV(sqrt(pi()), sqrt(0.7), 1), [sqrt(pi()), sqrt(0.7), 1.0]],
  220. ];
  221. }
  222. /**
  223. * @dataProvider getTupleProvider
  224. */
  225. public function testGetTuple($c, $h)
  226. {
  227. $this->assertEquals($h, $c->tuple, '', self::comparison_precision);
  228. $this->assertEquals($h, $c->getTuple(), '', self::comparison_precision);
  229. }
  230. }