You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

colorConvertRgbToHex.phpt 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. require __DIR__ . '/bootstrap.php';
  3. use Mexitek\PHPColors\Color;
  4. use Tester\Assert;
  5. // Colors in RGB, for testing.
  6. $blue = [
  7. 'R' => 0,
  8. 'G' => 158,
  9. 'B' => 204,
  10. ];
  11. $yellow = [
  12. 'R' => 244,
  13. 'G' => 231,
  14. 'B' => 15,
  15. ];
  16. $black = [
  17. 'R' => 0,
  18. 'G' => 0,
  19. 'B' => 0,
  20. ];
  21. $white = [
  22. 'R' => 255,
  23. 'G' => 255,
  24. 'B' => 255,
  25. ];
  26. // Test cases.
  27. $colorsToConvert = array(
  28. 'blue' => [ // rgb(0, 158, 204)
  29. 'hex' => '009ecc',
  30. 'rgb' => $blue,
  31. ],
  32. 'yellow' => [ // rgb(244, 231, 15)
  33. 'hex' => 'f4e70f',
  34. 'rgb' => $yellow,
  35. ],
  36. 'black' => [
  37. 'hex' => '000000',
  38. 'rgb' => $black,
  39. ],
  40. 'white' => [
  41. 'hex' => 'ffffff',
  42. 'rgb' => $white,
  43. ],
  44. );
  45. foreach ($colorsToConvert as $color) {
  46. $rgb = $color['rgb'];
  47. $hex = $color['hex'];
  48. $answer = Color::rgbToHex($rgb);
  49. Assert::same(
  50. $hex,
  51. $answer,
  52. 'Incorrect hex result: "' . Color::rgbToString($rgb) .
  53. '" should convert to "' . $hex .
  54. '", but output was: "' . $answer . '".'
  55. );
  56. $revertAnswer = Color::hexToRgb($hex);
  57. Assert::same(
  58. $rgb,
  59. $revertAnswer,
  60. 'Incorrect rgb result: "' . $hex .
  61. '" should convert to "' . Color::rgbToString($rgb) .
  62. '", but output was: "' . Color::rgbToString($revertAnswer) . '".'
  63. );
  64. }