RGBATest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. <?php
  2. namespace fphammerle\helpers\tests\colors;
  3. use \fphammerle\helpers\colors\RGBA;
  4. class RGBATest extends \PHPUnit_Framework_TestCase
  5. {
  6. const comparison_precision = 0.00001;
  7. public function testConstruct0()
  8. {
  9. $c = new RGBA();
  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. $this->assertEquals(1.0, $c->alpha, '', self::comparison_precision);
  14. }
  15. public function testConstruct1()
  16. {
  17. $c = new RGBA(1.0);
  18. $this->assertEquals(1.0, $c->red, '', self::comparison_precision);
  19. $this->assertEquals(0.0, $c->green, '', self::comparison_precision);
  20. $this->assertEquals(0.0, $c->blue, '', self::comparison_precision);
  21. $this->assertEquals(1.0, $c->alpha, '', self::comparison_precision);
  22. }
  23. public function testConstruct2()
  24. {
  25. $c = new RGBA(1.0, 0.5);
  26. $this->assertEquals(1.0, $c->red, '', self::comparison_precision);
  27. $this->assertEquals(0.5, $c->green, '', self::comparison_precision);
  28. $this->assertEquals(0.0, $c->blue, '', self::comparison_precision);
  29. $this->assertEquals(1.0, $c->alpha, '', self::comparison_precision);
  30. }
  31. public function testConstruct3()
  32. {
  33. $c = new RGBA(0.3, 0.2, 1, 0.6);
  34. $this->assertEquals(0.3, $c->red, '', self::comparison_precision);
  35. $this->assertEquals(0.2, $c->green, '', self::comparison_precision);
  36. $this->assertEquals(1.0, $c->blue, '', self::comparison_precision);
  37. $this->assertEquals(0.6, $c->alpha, '', self::comparison_precision);
  38. }
  39. public function equalsProvider()
  40. {
  41. return [
  42. [new RGBA(0.0, 0.0, 0.0, 0.0), new RGBA(0.0, 0.0, 0.0, 0.0 )],
  43. [new RGBA(0.0, 0.0, 0.0, 0.0), new RGBA(0, 0, 0, 0 )],
  44. [new RGBA(0.0, 0.0, 0.0, 0.0), new RGBA(pow(10, -10), pow(10, -10), pow(10, -10), pow(10, -10) )],
  45. [new RGBA(1.0, 1.0, 1.0, 1.0), new RGBA(1.0, 1.0, 1.0, 1.0 )],
  46. [new RGBA(1.0, 1.0, 1.0, 1.0), new RGBA(1, 1, 1, 1 )],
  47. [new RGBA(1.0, 1.0, 1.0, 1.0), new RGBA(1 - pow(10, -10), 1 - pow(10, -10), 1 - pow(10, -10), 1 - pow(10, -10))],
  48. [new RGBA(0.1, 0.2, 0.3, 0.4), new RGBA(0.1, 0.2, 0.3, 0.4 )],
  49. [new RGBA(1/7, 1/9, 1/6, 1/3), new RGBA(1/14*2, 1/36*4, 6/36, 9/27 )],
  50. ];
  51. }
  52. /**
  53. * @dataProvider equalsProvider
  54. */
  55. public function testEquals($a, $b)
  56. {
  57. $this->assertTrue($a->equals($b));
  58. $this->assertTrue($b->equals($a));
  59. }
  60. public function unequalsProvider()
  61. {
  62. return [
  63. [new RGBA(0.0, 0.0, 0.0), new RGBA(0.0, 0.0, pow(10, -4) )],
  64. [new RGBA(0.0, 0.0, 0.0), new RGBA(0.0, pow(10, -4), 0.0 )],
  65. [new RGBA(0.0, 0.0, 0.0), new RGBA(0.0, pow(10, -4), pow(10, -4) )],
  66. [new RGBA(0.0, 0.0, 0.0), new RGBA(pow(10, -4), 0.0, 0.0 )],
  67. [new RGBA(0.0, 0.0, 0.0), new RGBA(pow(10, -4), pow(10, -4), 0.0 )],
  68. [new RGBA(0.0, 0.0, 0.0), new RGBA(pow(10, -4), pow(10, -4), pow(10, -4) )],
  69. [new RGBA(0.1, 0.2, 0.3), new RGBA(0.1, 0.2, 0.4 )],
  70. [new RGBA(0.1, 0.2, 0.3), new RGBA(0.1, 0.4, 0.3 )],
  71. [new RGBA(0.1, 0.2, 0.3), new RGBA(0.4, 0.2, 0.3 )],
  72. [new RGBA(1.0, 1.0, 1.0), new RGBA(1 - pow(10, -4), 1 - pow(10, -4), 1 - pow(10, -4))],
  73. [new RGBA(1.0, 1.0, 1.0), new RGBA(1 - pow(10, -4), 1 - pow(10, -4), 1.0 )],
  74. [new RGBA(1.0, 1.0, 1.0), new RGBA(1 - pow(10, -4), 1.0, 1.0 )],
  75. [new RGBA(1.0, 1.0, 1.0), new RGBA(1.0, 1 - pow(10, -4), 1 - pow(10, -4))],
  76. [new RGBA(1.0, 1.0, 1.0), new RGBA(1.0, 1 - pow(10, -4), 1.0 )],
  77. [new RGBA(1.0, 1.0, 1.0), new RGBA(1.0, 1.0, 1 - pow(10, -4))],
  78. [new RGBA(0.0, 0.0, 0.0, 0.0), new RGBA(0.0, 0.0, 0.0, pow(10, -4))],
  79. [new RGBA(0.1, 0.0, 0.0, 0.0), new RGBA(0.0, 0.0, 0.0, 0.0 )],
  80. [new RGBA(0.0, 0.2, 0.0, 0.0), new RGBA(0.0, 0.0, 0.0, 0.0 )],
  81. [new RGBA(0.0, 0.0, 0.3, 0.0), new RGBA(0.0, 0.0, 0.0, 0.0 )],
  82. ];
  83. }
  84. /**
  85. * @dataProvider unequalsProvider
  86. */
  87. public function testUnequals($a, $b)
  88. {
  89. $this->assertFalse($a->equals($b));
  90. $this->assertFalse($b->equals($a));
  91. }
  92. public function getRedProvider()
  93. {
  94. return [
  95. [new RGBA(0, 0.1, 0.2, 0.4), 0.0],
  96. [new RGBA(0.0, 0.2, 0.3, 0.4), 0.0],
  97. [new RGBA(0.1, 0.2, 0.3, 0.4), 0.1],
  98. [new RGBA(1/7, 0.2, 0.3, 0.4), 1/7],
  99. ];
  100. }
  101. /**
  102. * @dataProvider getRedProvider
  103. */
  104. public function testGetRed($c, $h)
  105. {
  106. $this->assertEquals($h, $c->red, '', self::comparison_precision);
  107. $this->assertEquals($h, $c->getRed(), '', self::comparison_precision);
  108. }
  109. public function setRedProvider()
  110. {
  111. return [
  112. [0, 0.0],
  113. [0, 0.0],
  114. [0.1, 0.1],
  115. [1/7, 1/7],
  116. ];
  117. }
  118. /**
  119. * @dataProvider setRedProvider
  120. */
  121. public function testSetRed($s, $g)
  122. {
  123. $c = new RGBA(0, 0, 0, 0);
  124. $c->setRed($s);
  125. $this->assertEquals($g, $c->red, '', self::comparison_precision);
  126. $c = new RGBA(0, 0, 0, 0);
  127. $c->red = $s;
  128. $this->assertEquals($g, $c->red, '', self::comparison_precision);
  129. }
  130. public function getGreenProvider()
  131. {
  132. return [
  133. [new RGBA(0.1, 0, 0.2, 0.4), 0.0],
  134. [new RGBA(0.2, 0.0, 0.3, 0.4), 0.0],
  135. [new RGBA(0.2, 0.1, 0.3, 0.4), 0.1],
  136. [new RGBA(0.2, 1/7, 0.3, 0.4), 1/7],
  137. ];
  138. }
  139. /**
  140. * @dataProvider getGreenProvider
  141. */
  142. public function testGetGreen($c, $h)
  143. {
  144. $this->assertEquals($h, $c->green, '', self::comparison_precision);
  145. $this->assertEquals($h, $c->getGreen(), '', self::comparison_precision);
  146. }
  147. public function setGreenProvider()
  148. {
  149. return [
  150. [0, 0.0],
  151. [0, 0.0],
  152. [0.1, 0.1],
  153. [1/7, 1/7],
  154. ];
  155. }
  156. /**
  157. * @dataProvider setGreenProvider
  158. */
  159. public function testSetGreen($s, $g)
  160. {
  161. $c = new RGBA(0, 0, 0, 0);
  162. $c->setGreen($s);
  163. $this->assertEquals($g, $c->green, '', self::comparison_precision);
  164. $c = new RGBA(0, 0, 0, 0);
  165. $c->green = $s;
  166. $this->assertEquals($g, $c->green, '', self::comparison_precision);
  167. }
  168. public function getBlueProvider()
  169. {
  170. return [
  171. [new RGBA(0.1, 0.2, 0, 0.4), 0.0],
  172. [new RGBA(0.2, 0.3, 0.0, 0.4), 0.0],
  173. [new RGBA(0.2, 0.3, 0.1, 0.4), 0.1],
  174. [new RGBA(0.2, 0.3, 1/7, 0.4), 1/7],
  175. ];
  176. }
  177. /**
  178. * @dataProvider getBlueProvider
  179. */
  180. public function testGetBlue($c, $h)
  181. {
  182. $this->assertEquals($h, $c->blue, '', self::comparison_precision);
  183. $this->assertEquals($h, $c->getBlue(), '', self::comparison_precision);
  184. }
  185. public function setBlueProvider()
  186. {
  187. return [
  188. [0, 0.0],
  189. [0, 0.0],
  190. [0.1, 0.1],
  191. [1/7, 1/7],
  192. ];
  193. }
  194. /**
  195. * @dataProvider setBlueProvider
  196. */
  197. public function testSetBlue($s, $g)
  198. {
  199. $c = new RGBA(0, 0, 0, 0);
  200. $c->setBlue($s);
  201. $this->assertEquals($g, $c->blue, '', self::comparison_precision);
  202. $c = new RGBA(0, 0, 0, 0);
  203. $c->blue = $s;
  204. $this->assertEquals($g, $c->blue, '', self::comparison_precision);
  205. }
  206. public function getAlphaProvider()
  207. {
  208. return [
  209. [new RGBA(0.1, 0.2, 0.4, 0 ), 0.0],
  210. [new RGBA(0.2, 0.3, 0.4, 0.0), 0.0],
  211. [new RGBA(0.2, 0.3, 0.4, 0.1), 0.1],
  212. [new RGBA(0.2, 0.3, 0.4, 1/7), 1/7],
  213. ];
  214. }
  215. /**
  216. * @dataProvider getAlphaProvider
  217. */
  218. public function testGetAlpha($c, $h)
  219. {
  220. $this->assertEquals($h, $c->alpha, '', self::comparison_precision);
  221. $this->assertEquals($h, $c->getAlpha(), '', self::comparison_precision);
  222. }
  223. public function setAlphaProvider()
  224. {
  225. return [
  226. [0, 0.0],
  227. [0, 0.0],
  228. [0.1, 0.1],
  229. [1/7, 1/7],
  230. ];
  231. }
  232. /**
  233. * @dataProvider setAlphaProvider
  234. */
  235. public function testSetAlpha($s, $g)
  236. {
  237. $c = new RGBA(0, 0, 0, 0);
  238. $c->setAlpha($s);
  239. $this->assertEquals($g, $c->alpha, '', self::comparison_precision);
  240. $c = new RGBA(0, 0, 0, 0);
  241. $c->alpha = $s;
  242. $this->assertEquals($g, $c->alpha, '', self::comparison_precision);
  243. }
  244. public function getTupleProvider()
  245. {
  246. return [
  247. [new RGBA(0.0, 0.0, 0.0, 0.0), [0.0, 0.0, 0.0, 0.0]],
  248. [new RGBA(0.2, 0.3, 0.4, 0.5), [0.2, 0.3, 0.4, 0.5]],
  249. [new RGBA(0.8, 0.9, 1.0, 0.7), [0.8, 0.9, 1.0, 0.7]],
  250. [new RGBA(1/7, 1/9, 1/3, 1/4), [1/7, 1/9, 1/3, 1/4]],
  251. ];
  252. }
  253. /**
  254. * @dataProvider getTupleProvider
  255. */
  256. public function testGetTuple($c, $h)
  257. {
  258. $this->assertEquals($h, $c->tuple, '', self::comparison_precision);
  259. $this->assertEquals($h, $c->getTuple(), '', self::comparison_precision);
  260. }
  261. public function getDigitalTupleProvider()
  262. {
  263. return [
  264. [new RGBA(0.0, 0.0, 0.0, 0.0), 1, [0, 0, 0, 0]],
  265. [new RGBA(0.0, 0.0, 0.0, 0.0), 2, [0, 0, 0, 0]],
  266. [new RGBA(0.0, 0.0, 0.0, 0.0), 3, [0, 0, 0, 0]],
  267. [new RGBA(0.2, 0.3, 0.4, 0.5), 1, [0, 0, 0, 1]],
  268. [new RGBA(0.2, 0.3, 0.4, 0.5), 2, [1, 1, 1, 2]],
  269. [new RGBA(0.2, 0.3, 0.4, 0.5), 3, [1, 2, 3, 4]],
  270. [new RGBA(0.8, 0.9, 1.0, 0.7), 1, [1, 1, 1, 1]],
  271. [new RGBA(0.8, 0.9, 1.0, 0.7), 2, [2, 3, 3, 2]],
  272. [new RGBA(0.8, 0.9, 1.0, 0.7), 3, [6, 6, 7, 5]],
  273. [new RGBA(1/7, 1/9, 1/3, 1/8), 1, [0, 0, 0, 0]],
  274. [new RGBA(1/7, 1/9, 1/3, 1/8), 2, [0, 0, 1, 0]],
  275. [new RGBA(1/7, 1/9, 1/3, 1/8), 3, [1, 1, 2, 1]],
  276. [new RGBA(1/8, 1/4, 1/2, 1/4), 3, [1, 2, 4, 2]],
  277. ];
  278. }
  279. /**
  280. * @dataProvider getDigitalTupleProvider
  281. */
  282. public function testGetDigitalTuple($c, $bits, $tuple)
  283. {
  284. $this->assertSame($tuple, $c->getDigitalTuple($bits));
  285. }
  286. public function getDigitalHexTupleProvider()
  287. {
  288. return [
  289. [new RGBA(0.0, 0.0, 0.0, 0.0), 1, ['0', '0', '0', '0' ]],
  290. [new RGBA(0.0, 0.0, 0.0, 0.0), 2, ['0', '0', '0', '0' ]],
  291. [new RGBA(0.0, 0.0, 0.0, 0.0), 3, ['0', '0', '0', '0' ]],
  292. [new RGBA(0.2, 0.3, 0.4, 0.5), 1, ['0', '0', '0', '1' ]],
  293. [new RGBA(0.2, 0.3, 0.4, 0.5), 2, ['1', '1', '1', '2' ]],
  294. [new RGBA(0.2, 0.3, 0.4, 0.5), 3, ['1', '2', '3', '4' ]],
  295. [new RGBA(0.8, 0.9, 1.0, 0.7), 1, ['1', '1', '1', '1' ]],
  296. [new RGBA(0.8, 0.9, 1.0, 0.7), 2, ['2', '3', '3', '2' ]],
  297. [new RGBA(0.8, 0.9, 1.0, 0.7), 4, ['c', 'e', 'f', 'b' ]],
  298. [new RGBA(1/7, 1/9, 1/3, 1/8), 1, ['0', '0', '0', '0' ]],
  299. [new RGBA(1/7, 1/9, 1/3, 1/8), 5, ['4', '3', 'a', '4' ]],
  300. [new RGBA(1/7, 1/9, 1/3, 1/8), 5, ['4', '3', 'a', '4' ]],
  301. [new RGBA(1/8, 1/4, 1/2, 1/4), 6, ['8', '10', '20', '10']],
  302. [new RGBA(1/4, 1/2, 1/1, 1/8), 8, ['40', '80', 'ff', '20']],
  303. ];
  304. }
  305. /**
  306. * @dataProvider getDigitalHexTupleProvider
  307. */
  308. public function testGetDigitalHexTuple($c, $bits, $tuple)
  309. {
  310. $this->assertSame($tuple, $c->getDigitalHexTuple($bits));
  311. }
  312. public function getHexTripletProvider()
  313. {
  314. return [
  315. [new RGBA(1/4, 1/2, 1/1), '4080ff'],
  316. [new RGBA(0.3, 0.6, 0.9), '4d99e6'],
  317. [new RGBA(1.0, 1/3, 0.0), 'ff5500'],
  318. [new RGBA(1/7, 1/8, 1/9), '24201c'],
  319. [new RGBA(1/16, 1/32, 1/64), '100804'],
  320. [new RGBA(1/32, 1/64, 1/96), '080403'],
  321. ];
  322. }
  323. /**
  324. * @dataProvider getHexTripletProvider
  325. */
  326. public function testGetHexTriplet($c, $triplet)
  327. {
  328. $this->assertSame($triplet, $c->hexTriplet);
  329. }
  330. }