HSVTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. <?php
  2. namespace fphammerle\helpers\tests\colors;
  3. use \fphammerle\helpers\colors\HSV;
  4. use \fphammerle\helpers\colors\RGB;
  5. class HSVTest extends \PHPUnit_Framework_TestCase
  6. {
  7. const comparison_precision = 0.00001;
  8. public function testConstruct0()
  9. {
  10. $c = new HSV();
  11. $this->assertEquals(0.0, $c->hue, '', self::comparison_precision);
  12. $this->assertEquals(0.0, $c->saturation, '', self::comparison_precision);
  13. $this->assertEquals(0.0, $c->value, '', self::comparison_precision);
  14. }
  15. public function testConstruct1()
  16. {
  17. $c = new HSV(pi());
  18. $this->assertEquals(pi(), $c->hue, '', self::comparison_precision);
  19. $this->assertEquals(0.0, $c->saturation, '', self::comparison_precision);
  20. $this->assertEquals(0.0, $c->value, '', self::comparison_precision);
  21. }
  22. public function testConstruct2()
  23. {
  24. $c = new HSV(pi(), 0.2);
  25. $this->assertEquals(pi(), $c->hue, '', self::comparison_precision);
  26. $this->assertEquals(0.2, $c->saturation, '', self::comparison_precision);
  27. $this->assertEquals(0.0, $c->value, '', self::comparison_precision);
  28. }
  29. public function testConstruct3()
  30. {
  31. $c = new HSV(pi(), 0.2, 1);
  32. $this->assertEquals(pi(), $c->hue, '', self::comparison_precision);
  33. $this->assertEquals(0.2, $c->saturation, '', self::comparison_precision);
  34. $this->assertEquals(1.0, $c->value, '', self::comparison_precision);
  35. }
  36. public function equalsProvider()
  37. {
  38. return [
  39. [new HSV(0, 0, 0.0 ), new HSV(0.0, 0.0, 0 )],
  40. [new HSV(0.0, 0.0, 0.0 ), new HSV(0.0, 0.0, 0.0 )],
  41. [new HSV(0.0, 0.4, 0.99), new HSV(0.0, 0.4, 0.99 )],
  42. [new HSV(2.21, 0.4, 0.99), new HSV(2.21, 0.4, 0.99 )],
  43. [new HSV(2.21, 0.9, 0.0 ), new HSV(2.21, 0.9, pow(10, -20))],
  44. [new HSV(3, 0.3, 1 ), new HSV(1/2*6, 0.3, 1.0 )],
  45. [new HSV(pi(), 0.0, 0.0 ), new HSV(deg2rad(180), 0.0, 0.0 )],
  46. [new HSV(pi(), 0.7, 0.0 ), new HSV(deg2rad(180), 0.7, 0.0 )],
  47. [new HSV(sqrt(pi()), sqrt(0.7), sqrt(1/33) ), new HSV(sqrt(deg2rad(180)), sqrt(7)/sqrt(10), 1/sqrt(33))],
  48. ];
  49. }
  50. /**
  51. * @dataProvider equalsProvider
  52. */
  53. public function testEquals($a, $b)
  54. {
  55. $this->assertTrue($a->equals($b));
  56. $this->assertTrue($b->equals($a));
  57. }
  58. public function unequalsProvider()
  59. {
  60. return [
  61. [new HSV(0.0, 0.0, 0.0), new HSV(0.0, 0.0, pow(10, -4) )],
  62. [new HSV(0.0, 0.0, 0.0), new HSV(0.0, pow(10, -4), 0.0 )],
  63. [new HSV(0.0, 0.0, 0.0), new HSV(pow(10, -4), 0.0, 0.0 )],
  64. [new HSV(0.5, 0.5, 0.5), new HSV(0.5+1.0/9999, 0.5, 0.5 )],
  65. [new HSV(0.5, 0.5, 0.5), new HSV(0.5, 0.5+1.0/9999, 0.5 )],
  66. [new HSV(0.5, 0.5, 0.5), new HSV(0.5, 0.5, 0.5+1.0/9999)],
  67. [new HSV(pi(), 1/33, 0.1), new HSV(1.53, pow(10, -4), 0.32 )],
  68. [new HSV(pi(), 1/33, 0.1), new HSV(2.32, 0.32, pow(10, -4) )],
  69. [new HSV(pi(), 1/33, 0.1), new HSV(2.32, 1/33, pow(10, -4) )],
  70. [new HSV(pi(), 1/33, 0.1), new HSV(pi(), 1/33, pow(10, -4) )],
  71. [new HSV(pi(), 1/33, 0.1), new HSV(pi(), pow(10, -4), 0.1 )],
  72. [new HSV(pi(), 1/33, 0.1), new HSV(pi(), pow(10, -4), 0.32 )],
  73. [new HSV(pi(), 1/33, 0.1), new HSV(pow(10, -4), 0.92, 0.1 )],
  74. [new HSV(pi(), 1/33, 0.1), new HSV(pow(10, -4), 0.92, 0.4 )],
  75. [new HSV(pi(), 1/33, 0.1), new HSV(pow(10, -4), 1/33, 0.1 )],
  76. ];
  77. }
  78. /**
  79. * @dataProvider unequalsProvider
  80. */
  81. public function testUnequals($a, $b)
  82. {
  83. $this->assertFalse($a->equals($b));
  84. $this->assertFalse($b->equals($a));
  85. }
  86. public function getHueProvider()
  87. {
  88. return [
  89. [new HSV(0, 0, 0.0 ), 0.0, ],
  90. [new HSV(0.0, 0.0, 0.0 ), 0.0, ],
  91. [new HSV(2.21, 0.4, 0.99 ), 2.21, ],
  92. [new HSV(3, 0.3, 1 ), 1/2*6, ],
  93. [new HSV(pi(), 0.7, 0.0 ), deg2rad(180) ],
  94. [new HSV(sqrt(pi()), sqrt(0.7), 1), sqrt(deg2rad(180))],
  95. ];
  96. }
  97. /**
  98. * @dataProvider getHueProvider
  99. */
  100. public function testGetHue($c, $h)
  101. {
  102. $this->assertEquals($h, $c->hue, '', self::comparison_precision);
  103. $this->assertEquals($h, $c->getHue(), '', self::comparison_precision);
  104. }
  105. public function setHueProvider()
  106. {
  107. return [
  108. [0, 0.0, ],
  109. [0.0, 0.0, ],
  110. [2.21, 2.21, ],
  111. [3, 1/2*6, ],
  112. [pi(), deg2rad(180) ],
  113. [sqrt(pi()), sqrt(deg2rad(180))],
  114. ];
  115. }
  116. /**
  117. * @dataProvider setHueProvider
  118. */
  119. public function testSetHue($s, $g)
  120. {
  121. $c = new HSV(0, 0, 0);
  122. $c->setHue($s);
  123. $this->assertEquals($g, $c->hue, '', self::comparison_precision);
  124. $c = new HSV(0, 0, 0);
  125. $c->hue = $s;
  126. $this->assertEquals($g, $c->hue, '', self::comparison_precision);
  127. }
  128. public function getSaturationProvider()
  129. {
  130. return [
  131. [new HSV(0, 0, 0.0 ), 0.0, ],
  132. [new HSV(0.0, 0.0, 0.0 ), 0.0, ],
  133. [new HSV(2.21, 0.4, 0.99 ), 0.4, ],
  134. [new HSV(3, pi()/2/pi(), 1 ), 0.5, ],
  135. [new HSV(pi(), 1, 0.0 ), 1.0 ],
  136. [new HSV(pi(), sqrt(0.7), 1 ), sqrt(0.7)],
  137. ];
  138. }
  139. /**
  140. * @dataProvider getSaturationProvider
  141. */
  142. public function testGetSaturation($c, $h)
  143. {
  144. $this->assertEquals($h, $c->saturation, '', self::comparison_precision);
  145. $this->assertEquals($h, $c->getSaturation(), '', self::comparison_precision);
  146. }
  147. public function setSaturationProvider()
  148. {
  149. return [
  150. [0, 0.0, ],
  151. [0.0, 0.0, ],
  152. [0.4, 0.4, ],
  153. [pi()/2/pi(), 0.5, ],
  154. [1, 1.0 ],
  155. [sqrt(0.7), sqrt(0.7)],
  156. ];
  157. }
  158. /**
  159. * @dataProvider setSaturationProvider
  160. */
  161. public function testSetSaturation($s, $g)
  162. {
  163. $c = new HSV(0, 0, 0);
  164. $c->setSaturation($s);
  165. $this->assertEquals($g, $c->saturation, '', self::comparison_precision);
  166. $c = new HSV(0, 0, 0);
  167. $c->saturation = $s;
  168. $this->assertEquals($g, $c->saturation, '', self::comparison_precision);
  169. }
  170. public function getValueProvider()
  171. {
  172. return [
  173. [new HSV(0, 0.0, 0 ), 0.0 ],
  174. [new HSV(0.0, 0, 0.0 ), 0.0 ],
  175. [new HSV(2.21, 0.3, 0.4 ), 0.4 ],
  176. [new HSV(3, 1.0, pi()/2/pi()), 0.5 ],
  177. [new HSV(pi(), 0.4, 1 ), 1.0 ],
  178. [new HSV(pi(), 1, sqrt(0.7) ), sqrt(0.7)],
  179. ];
  180. }
  181. /**
  182. * @dataProvider getValueProvider
  183. */
  184. public function testGetValue($c, $h)
  185. {
  186. $this->assertEquals($h, $c->value, '', self::comparison_precision);
  187. $this->assertEquals($h, $c->getValue(), '', self::comparison_precision);
  188. }
  189. public function setValueProvider()
  190. {
  191. return [
  192. [0, 0.0, ],
  193. [0.0, 0.0, ],
  194. [0.4, 0.4, ],
  195. [pi()/2/pi(), 0.5, ],
  196. [1, 1.0 ],
  197. [sqrt(0.7), sqrt(0.7)],
  198. ];
  199. }
  200. /**
  201. * @dataProvider setValueProvider
  202. */
  203. public function testSetValue($s, $g)
  204. {
  205. $c = new HSV(0, 0, 0);
  206. $c->setValue($s);
  207. $this->assertEquals($g, $c->value, '', self::comparison_precision);
  208. $c = new HSV(0, 0, 0);
  209. $c->value = $s;
  210. $this->assertEquals($g, $c->value, '', self::comparison_precision);
  211. }
  212. public function getTupleProvider()
  213. {
  214. return [
  215. [new HSV(0, 0, 0.0 ), [0.0, 0.0, 0.0 ]],
  216. [new HSV(0.0, 0.0, 0.0 ), [0.0, 0.0, 0.0 ]],
  217. [new HSV(2.21, 0.4, 0.99 ), [2.21, 0.4, 0.99 ]],
  218. [new HSV(3, 0.3, 1 ), [3.0, 0.3, 1.0 ]],
  219. [new HSV(pi(), 0.7, 0.0 ), [pi(), 0.7, 0.0 ]],
  220. [new HSV(sqrt(pi()), sqrt(0.7), 1), [sqrt(pi()), sqrt(0.7), 1.0]],
  221. ];
  222. }
  223. /**
  224. * @dataProvider getTupleProvider
  225. */
  226. public function testGetTuple($c, $h)
  227. {
  228. $this->assertEquals($h, $c->tuple, '', self::comparison_precision);
  229. $this->assertEquals($h, $c->getTuple(), '', self::comparison_precision);
  230. }
  231. public function toRGBProvider()
  232. {
  233. return [
  234. [new HSV(0.0, 0.0, 0.0), new RGB(0.0, 0.0, 0.0)],
  235. [new HSV(0.0, 0.0, 1/4), new RGB(1/4, 1/4, 1/4)],
  236. [new HSV(0.0, 0.0, 1.0), new RGB(1.0, 1.0, 1.0)],
  237. [new HSV(0.0, 1.0, 0.0), new RGB(0.0, 0.0, 0.0)],
  238. [new HSV(0.0, 1/5, 0.0), new RGB(0.0, 0.0, 0.0)],
  239. [new HSV(1.0, 0.0, 0.0), new RGB(0.0, 0.0, 0.0)],
  240. [new HSV(1.0, 0.0, 1/4), new RGB(1/4, 1/4, 1/4)],
  241. [new HSV(1.0, 0.0, 1.0), new RGB(1.0, 1.0, 1.0)],
  242. [new HSV(1.0, 1.0, 0.0), new RGB(0.0, 0.0, 0.0)],
  243. [new HSV(1.0, 1/5, 0.0), new RGB(0.0, 0.0, 0.0)],
  244. [new HSV(pi()*3/2, 0.0, 0.0), new RGB(0.0, 0.0, 0.0)],
  245. [new HSV(pi()*3/2, 0.0, 1/4), new RGB(1/4, 1/4, 1/4)],
  246. [new HSV(pi()*3/2, 0.0, 1.0), new RGB(1.0, 1.0, 1.0)],
  247. [new HSV(pi()*3/2, 1.0, 0.0), new RGB(0.0, 0.0, 0.0)],
  248. [new HSV(pi()*3/2, 1/5, 0.0), new RGB(0.0, 0.0, 0.0)],
  249. [new HSV(pi(), 0.0, 0.0), new RGB(0.0, 0.0, 0.0)],
  250. [new HSV(pi(), 0.0, 1/4), new RGB(1/4, 1/4, 1/4)],
  251. [new HSV(pi(), 0.0, 1.0), new RGB(1.0, 1.0, 1.0)],
  252. [new HSV(pi(), 1.0, 0.0), new RGB(0.0, 0.0, 0.0)],
  253. [new HSV(pi(), 1/5, 0.0), new RGB(0.0, 0.0, 0.0)],
  254. [new HSV(pi()/2, 0.0, 0.0), new RGB(0.0, 0.0, 0.0)],
  255. [new HSV(pi()/2, 0.0, 1/4), new RGB(1/4, 1/4, 1/4)],
  256. [new HSV(pi()/2, 0.0, 1.0), new RGB(1.0, 1.0, 1.0)],
  257. [new HSV(pi()/2, 1.0, 0.0), new RGB(0.0, 0.0, 0.0)],
  258. [new HSV(pi()/2, 1/5, 0.0), new RGB(0.0, 0.0, 0.0)],
  259. [new HSV(pi()/6*0, 1.0, 1.0), new RGB(1.0, 0.0, 0.0)],
  260. [new HSV(pi()/6*0, 1.0, 0.6), new RGB(0.6, 0.0, 0.0)],
  261. [new HSV(pi()/6*0, 1.0, 0.2), new RGB(0.2, 0.0, 0.0)],
  262. [new HSV(pi()/6*0, 1.0, 0.0), new RGB(0.0, 0.0, 0.0)],
  263. [new HSV(pi()/6*1, 1.0, 1.0), new RGB(1.0, 0.5, 0.0)],
  264. [new HSV(pi()/6*1, 1.0, 0.6), new RGB(0.6, 0.3, 0.0)],
  265. [new HSV(pi()/6*1, 1.0, 0.2), new RGB(1/5, 0.1, 0.0)],
  266. [new HSV(pi()/6*2, 1.0, 1.0), new RGB(1.0, 1.0, 0.0)],
  267. [new HSV(pi()/6*2, 1.0, 0.6), new RGB(0.6, 0.6, 0.0)],
  268. [new HSV(pi()/6*2, 1.0, 0.2), new RGB(0.2, 0.2, 0.0)],
  269. [new HSV(pi()/6*3, 1.0, 1.0), new RGB(0.5, 1.0, 0.0)],
  270. [new HSV(pi()/6*3, 1.0, 0.6), new RGB(0.3, 0.6, 0.0)],
  271. [new HSV(pi()/6*3, 1.0, 0.2), new RGB(0.1, 0.2, 0.0)],
  272. [new HSV(pi()/6*4, 1.0, 1.0), new RGB(0.0, 1.0, 0.0)],
  273. [new HSV(pi()/6*4, 1.0, 0.6), new RGB(0.0, 0.6, 0.0)],
  274. [new HSV(pi()/6*4, 1.0, 0.2), new RGB(0.0, 0.2, 0.0)],
  275. [new HSV(pi()/6*5, 1.0, 1.0), new RGB(0.0, 1.0, 0.5)],
  276. [new HSV(pi()/6*5, 1.0, 0.6), new RGB(0.0, 0.6, 0.3)],
  277. [new HSV(pi()/6*5, 1.0, 0.2), new RGB(0.0, 0.2, 0.1)],
  278. [new HSV(pi()/6*6, 1.0, 1.0), new RGB(0.0, 1.0, 1.0)],
  279. [new HSV(pi()/6*6, 1.0, 0.6), new RGB(0.0, 0.6, 0.6)],
  280. [new HSV(pi()/6*6, 1.0, 0.2), new RGB(0.0, 0.2, 0.2)],
  281. [new HSV(pi()/6*7, 1.0, 1.0), new RGB(0.0, 0.5, 1.0)],
  282. [new HSV(pi()/6*7, 1.0, 0.6), new RGB(0.0, 0.3, 0.6)],
  283. [new HSV(pi()/6*7, 1.0, 0.2), new RGB(0.0, 0.1, 0.2)],
  284. [new HSV(pi()/6*8, 1.0, 1.0), new RGB(0.0, 0.0, 1.0)],
  285. [new HSV(pi()/6*8, 1.0, 0.6), new RGB(0.0, 0.0, 0.6)],
  286. [new HSV(pi()/6*8, 1.0, 0.2), new RGB(0.0, 0.0, 0.2)],
  287. [new HSV(pi()/6*9, 1.0, 1.0), new RGB(0.5, 0.0, 1.0)],
  288. [new HSV(pi()/6*9, 1.0, 0.6), new RGB(0.3, 0.0, 0.6)],
  289. [new HSV(pi()/6*9, 1.0, 0.2), new RGB(0.1, 0.0, 0.2)],
  290. [new HSV(pi()/6*10, 1.0, 1.0), new RGB(1.0, 0.0, 1.0)],
  291. [new HSV(pi()/6*10, 1.0, 0.6), new RGB(0.6, 0.0, 0.6)],
  292. [new HSV(pi()/6*10, 1.0, 0.2), new RGB(0.2, 0.0, 0.2)],
  293. [new HSV(pi()/6*11, 1.0, 1.0), new RGB(1.0, 0.0, 0.5)],
  294. [new HSV(pi()/6*11, 1.0, 0.6), new RGB(0.6, 0.0, 0.3)],
  295. [new HSV(pi()/6*11, 1.0, 0.2), new RGB(0.2, 0.0, 0.1)],
  296. [new HSV(deg2rad(10), 0.8, 0.4), new RGB(102/255, 34/255, 20.4/255 )],
  297. [new HSV(deg2rad(35), 0.8, 0.4), new RGB(102/255, 68/255, 20.4/255 )],
  298. [new HSV(deg2rad(35), 0.3, 0.6), new RGB(153/255, 133.875/255, 107.1/255 )],
  299. [new HSV(deg2rad(100), 0.3, 0.6), new RGB(122.4/255, 153/255, 107.1/255 )],
  300. [new HSV(deg2rad(130), 0.6, 0.2), new RGB(20.4/255, 51/255, 25.5/255 )],
  301. [new HSV(deg2rad(155), 0.6, 0.9), new RGB(91.8/255, 229.5/255, 172.125/255)],
  302. [new HSV(deg2rad(200), 0.8, 0.6), new RGB(30.6/255, 112.2/255, 153/255 )],
  303. [new HSV(deg2rad(226), 0.8, 0.6), new RGB(30.6/255, 59.16/255, 153/255 )],
  304. [new HSV(deg2rad(226), 0.1, 0.5), new RGB(114.75/255, 117.725/255, 127.5/255 )],
  305. [new HSV(deg2rad(250), 0.9, 0.9), new RGB(57.373/255, 22.95/255, 229.5/255 )],
  306. [new HSV(deg2rad(270), 0.9, 0.9), new RGB(126.225/255, 22.95/255, 229.5/255 )],
  307. [new HSV(deg2rad(295), 0.7, 0.9), new RGB(216.11/255, 68.85/255, 229.5/255 )],
  308. [new HSV(deg2rad(322), 0.7, 0.9), new RGB(229.5/255, 68.85/255, 170.595/255)],
  309. [new HSV(deg2rad(322), 0.8, 0.4), new RGB(102/255, 20.4/255, 72.08/255 )],
  310. [new HSV(deg2rad(340), 0.8, 0.4), new RGB(102/255, 20.4/255, 47.6/255 )],
  311. [new HSV(deg2rad(359), 0.8, 0.4), new RGB(102/255, 20.4/255, 21.76/255 )],
  312. [new HSV(deg2rad(359), 0.5, 0.1), new RGB(25.5/255, 12.75/255, 12.96/255 )],
  313. ];
  314. }
  315. /**
  316. * @dataProvider toRGBProvider
  317. */
  318. public function testToRGB($hsv, $rgb_e)
  319. {
  320. $rgb_r = $hsv->toRGB();
  321. $this->assertTrue(
  322. $rgb_e->equals($rgb_r),
  323. sprintf(
  324. "\$hsv = %s\n\$hsv->toRGB() = %s\n\$rgb_e = %s",
  325. print_r($hsv, true),
  326. print_r($rgb_r, true),
  327. print_r($rgb_e, true)
  328. )
  329. );
  330. }
  331. }