RGBTest.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php
  2. namespace fphammerle\helpers\tests\colors;
  3. use \fphammerle\helpers\colors\RGB;
  4. class RGBTest extends \PHPUnit_Framework_TestCase
  5. {
  6. const comparison_precision = 0.00001;
  7. public function testConstruct0()
  8. {
  9. $c = new RGB();
  10. $this->assertEquals(0.0, $c->red, '', self::comparison_precision);
  11. $this->assertEquals(0.0, $c->green, '', self::comparison_precision);
  12. $this->assertEquals(0.0, $c->blue, '', self::comparison_precision);
  13. }
  14. public function testConstruct1()
  15. {
  16. $c = new RGB(1.0);
  17. $this->assertEquals(1.0, $c->red, '', self::comparison_precision);
  18. $this->assertEquals(0.0, $c->green, '', self::comparison_precision);
  19. $this->assertEquals(0.0, $c->blue, '', self::comparison_precision);
  20. }
  21. public function testConstruct2()
  22. {
  23. $c = new RGB(1.0, 0.5);
  24. $this->assertEquals(1.0, $c->red, '', self::comparison_precision);
  25. $this->assertEquals(0.5, $c->green, '', self::comparison_precision);
  26. $this->assertEquals(0.0, $c->blue, '', self::comparison_precision);
  27. }
  28. public function testConstruct3()
  29. {
  30. $c = new RGB(0.3, 0.2, 1);
  31. $this->assertEquals(0.3, $c->red, '', self::comparison_precision);
  32. $this->assertEquals(0.2, $c->green, '', self::comparison_precision);
  33. $this->assertEquals(1.0, $c->blue, '', self::comparison_precision);
  34. }
  35. public function equalsProvider()
  36. {
  37. return [
  38. [new RGB(0.0, 0.0, 0.0 ), new RGB(0.0, 0.0, 0.0 )],
  39. [new RGB(0.0, 0.0, 0.0 ), new RGB(0, 0, 0 )],
  40. [new RGB(0.0, 0.0, 0.0 ), new RGB(pow(10, -10), pow(10, -10), pow(10, -10) )],
  41. [new RGB(1.0, 1.0, 1.0 ), new RGB(1.0, 1.0, 1.0 )],
  42. [new RGB(1.0, 1.0, 1.0 ), new RGB(1, 1, 1 )],
  43. [new RGB(1.0, 1.0, 1.0 ), new RGB(1 - pow(10, -10), 1 - pow(10, -10), 1 - pow(10, -10))],
  44. [new RGB(0.1, 0.2, 0.3 ), new RGB(0.1, 0.2, 0.3 )],
  45. [new RGB(1/7, 1/9, 1/321), new RGB(1/14*2, 1/36*4, 1/321 )],
  46. ];
  47. }
  48. /**
  49. * @dataProvider equalsProvider
  50. */
  51. public function testEquals($a, $b)
  52. {
  53. $this->assertTrue($a->equals($b));
  54. $this->assertTrue($b->equals($a));
  55. }
  56. public function unequalsProvider()
  57. {
  58. return [
  59. [new RGB(0.0, 0.0, 0.0), new RGB(0.0, 0.0, pow(10, -4) )],
  60. [new RGB(0.0, 0.0, 0.0), new RGB(0.0, pow(10, -4), 0.0 )],
  61. [new RGB(0.0, 0.0, 0.0), new RGB(0.0, pow(10, -4), pow(10, -4) )],
  62. [new RGB(0.0, 0.0, 0.0), new RGB(pow(10, -4), 0.0, 0.0 )],
  63. [new RGB(0.0, 0.0, 0.0), new RGB(pow(10, -4), pow(10, -4), 0.0 )],
  64. [new RGB(0.0, 0.0, 0.0), new RGB(pow(10, -4), pow(10, -4), pow(10, -4) )],
  65. [new RGB(0.1, 0.2, 0.3), new RGB(0.1, 0.2, 0.4 )],
  66. [new RGB(0.1, 0.2, 0.3), new RGB(0.1, 0.4, 0.3 )],
  67. [new RGB(0.1, 0.2, 0.3), new RGB(0.4, 0.2, 0.3 )],
  68. [new RGB(1.0, 1.0, 1.0), new RGB(1 - pow(10, -4), 1 - pow(10, -4), 1 - pow(10, -4))],
  69. [new RGB(1.0, 1.0, 1.0), new RGB(1 - pow(10, -4), 1 - pow(10, -4), 1.0 )],
  70. [new RGB(1.0, 1.0, 1.0), new RGB(1 - pow(10, -4), 1.0, 1.0 )],
  71. [new RGB(1.0, 1.0, 1.0), new RGB(1.0, 1 - pow(10, -4), 1 - pow(10, -4))],
  72. [new RGB(1.0, 1.0, 1.0), new RGB(1.0, 1 - pow(10, -4), 1.0 )],
  73. [new RGB(1.0, 1.0, 1.0), new RGB(1.0, 1.0, 1 - pow(10, -4))],
  74. ];
  75. }
  76. /**
  77. * @dataProvider unequalsProvider
  78. */
  79. public function testUnequals($a, $b)
  80. {
  81. $this->assertFalse($a->equals($b));
  82. $this->assertFalse($b->equals($a));
  83. }
  84. public function getRedProvider()
  85. {
  86. return [
  87. [new RGB(0, 0.1, 0.2), 0.0],
  88. [new RGB(0, 0.2, 0.3), 0.0],
  89. [new RGB(0.1, 0.2, 0.3), 0.1],
  90. [new RGB(1/7, 0.2, 0.3), 1/7],
  91. ];
  92. }
  93. /**
  94. * @dataProvider getRedProvider
  95. */
  96. public function testGetRed($c, $h)
  97. {
  98. $this->assertEquals($h, $c->red, '', self::comparison_precision);
  99. $this->assertEquals($h, $c->getRed(), '', self::comparison_precision);
  100. }
  101. public function setRedProvider()
  102. {
  103. return [
  104. [0, 0.0],
  105. [0, 0.0],
  106. [0.1, 0.1],
  107. [1/7, 1/7],
  108. ];
  109. }
  110. /**
  111. * @dataProvider setRedProvider
  112. */
  113. public function testSetRed($s, $g)
  114. {
  115. $c = new RGB(0, 0, 0);
  116. $c->setRed($s);
  117. $this->assertEquals($g, $c->red, '', self::comparison_precision);
  118. $c = new RGB(0, 0, 0);
  119. $c->red = $s;
  120. $this->assertEquals($g, $c->red, '', self::comparison_precision);
  121. }
  122. public function getGreenProvider()
  123. {
  124. return [
  125. [new RGB(0.1, 0, 0.2), 0.0],
  126. [new RGB(0.2, 0, 0.3), 0.0],
  127. [new RGB(0.2, 0.1, 0.3), 0.1],
  128. [new RGB(0.2, 1/7, 0.3), 1/7],
  129. ];
  130. }
  131. /**
  132. * @dataProvider getGreenProvider
  133. */
  134. public function testGetGreen($c, $h)
  135. {
  136. $this->assertEquals($h, $c->green, '', self::comparison_precision);
  137. $this->assertEquals($h, $c->getGreen(), '', self::comparison_precision);
  138. }
  139. public function setGreenProvider()
  140. {
  141. return [
  142. [0, 0.0],
  143. [0, 0.0],
  144. [0.1, 0.1],
  145. [1/7, 1/7],
  146. ];
  147. }
  148. /**
  149. * @dataProvider setGreenProvider
  150. */
  151. public function testSetGreen($s, $g)
  152. {
  153. $c = new RGB(0, 0, 0);
  154. $c->setGreen($s);
  155. $this->assertEquals($g, $c->green, '', self::comparison_precision);
  156. $c = new RGB(0, 0, 0);
  157. $c->green = $s;
  158. $this->assertEquals($g, $c->green, '', self::comparison_precision);
  159. }
  160. public function getBlueProvider()
  161. {
  162. return [
  163. [new RGB(0.1, 0.2, 0 ), 0.0],
  164. [new RGB(0.2, 0.3, 0 ), 0.0],
  165. [new RGB(0.2, 0.3, 0.1), 0.1],
  166. [new RGB(0.2, 0.3, 1/7), 1/7],
  167. ];
  168. }
  169. /**
  170. * @dataProvider getBlueProvider
  171. */
  172. public function testGetBlue($c, $h)
  173. {
  174. $this->assertEquals($h, $c->blue, '', self::comparison_precision);
  175. $this->assertEquals($h, $c->getBlue(), '', self::comparison_precision);
  176. }
  177. public function setBlueProvider()
  178. {
  179. return [
  180. [0, 0.0],
  181. [0, 0.0],
  182. [0.1, 0.1],
  183. [1/7, 1/7],
  184. ];
  185. }
  186. /**
  187. * @dataProvider setBlueProvider
  188. */
  189. public function testSetBlue($s, $g)
  190. {
  191. $c = new RGB(0, 0, 0);
  192. $c->setBlue($s);
  193. $this->assertEquals($g, $c->blue, '', self::comparison_precision);
  194. $c = new RGB(0, 0, 0);
  195. $c->blue = $s;
  196. $this->assertEquals($g, $c->blue, '', self::comparison_precision);
  197. }
  198. public function getTupleProvider()
  199. {
  200. return [
  201. [new RGB(0.0, 0.0, 0.0), [0.0, 0.0, 0.0]],
  202. [new RGB(0.2, 0.3, 0.4), [0.2, 0.3, 0.4]],
  203. [new RGB(0.8, 0.9, 1.0), [0.8, 0.9, 1.0]],
  204. [new RGB(1/7, 1/9, 1/3), [1/7, 1/9, 1/3]],
  205. ];
  206. }
  207. /**
  208. * @dataProvider getTupleProvider
  209. */
  210. public function testGetTuple($c, $h)
  211. {
  212. $this->assertEquals($h, $c->tuple, '', self::comparison_precision);
  213. $this->assertEquals($h, $c->getTuple(), '', self::comparison_precision);
  214. }
  215. public function getDigitalTupleProvider()
  216. {
  217. return [
  218. [new RGB(0.0, 0.0, 0.0), 1, [0, 0, 0]],
  219. [new RGB(0.0, 0.0, 0.0), 2, [0, 0, 0]],
  220. [new RGB(0.0, 0.0, 0.0), 3, [0, 0, 0]],
  221. [new RGB(0.2, 0.3, 0.4), 1, [0, 0, 0]],
  222. [new RGB(0.2, 0.3, 0.4), 2, [1, 1, 1]],
  223. [new RGB(0.2, 0.3, 0.4), 3, [1, 2, 3]],
  224. [new RGB(0.8, 0.9, 1.0), 1, [1, 1, 1]],
  225. [new RGB(0.8, 0.9, 1.0), 2, [2, 3, 3]],
  226. [new RGB(0.8, 0.9, 1.0), 3, [6, 6, 7]],
  227. [new RGB(1/7, 1/9, 1/3), 1, [0, 0, 0]],
  228. [new RGB(1/7, 1/9, 1/3), 2, [0, 0, 1]],
  229. [new RGB(1/7, 1/9, 1/3), 3, [1, 1, 2]],
  230. [new RGB(1/8, 1/4, 1/2), 3, [1, 2, 4]],
  231. ];
  232. }
  233. /**
  234. * @dataProvider getDigitalTupleProvider
  235. */
  236. public function testGetDigitalTuple($c, $bits, $tuple)
  237. {
  238. $this->assertSame($tuple, $c->getDigitalTuple($bits));
  239. }
  240. public function getDigitalHexTupleProvider()
  241. {
  242. return [
  243. [new RGB(0.0, 0.0, 0.0), 1, ['0', '0', '0']],
  244. [new RGB(0.0, 0.0, 0.0), 2, ['0', '0', '0']],
  245. [new RGB(0.0, 0.0, 0.0), 3, ['0', '0', '0']],
  246. [new RGB(0.2, 0.3, 0.4), 1, ['0', '0', '0']],
  247. [new RGB(0.2, 0.3, 0.4), 2, ['1', '1', '1']],
  248. [new RGB(0.2, 0.3, 0.4), 3, ['1', '2', '3']],
  249. [new RGB(0.8, 0.9, 1.0), 1, ['1', '1', '1']],
  250. [new RGB(0.8, 0.9, 1.0), 2, ['2', '3', '3']],
  251. [new RGB(0.8, 0.9, 1.0), 4, ['c', 'e', 'f']],
  252. [new RGB(1/7, 1/9, 1/3), 1, ['0', '0', '0']],
  253. [new RGB(1/7, 1/9, 1/3), 5, ['4', '3', 'a']],
  254. [new RGB(1/7, 1/9, 1/3), 5, ['4', '3', 'a']],
  255. [new RGB(1/8, 1/4, 1/2), 6, ['8', '10', '20']],
  256. [new RGB(1/4, 1/2, 1/1), 8, ['40', '80', 'ff']],
  257. ];
  258. }
  259. /**
  260. * @dataProvider getDigitalHexTupleProvider
  261. */
  262. public function testGetDigitalHexTuple($c, $bits, $tuple)
  263. {
  264. $this->assertSame($tuple, $c->getDigitalHexTuple($bits));
  265. }
  266. }