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.

colorConvertHslToHex.phpt 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. require __DIR__ . '/bootstrap.php';
  3. use Mexitek\PHPColors\Color;
  4. use Tester\Assert;
  5. // Colors in HSL, for testing.
  6. $blue = [
  7. 'H' => 194,
  8. 'S' => 1.0,
  9. 'L' => 0.4,
  10. ];
  11. $yellow = [
  12. 'H' => 57,
  13. 'S' => 0.91,
  14. 'L' => 0.51,
  15. ];
  16. $black = [
  17. 'H' => 0,
  18. 'S' => 0.0,
  19. 'L' => 0.0,
  20. ];
  21. $grey = [
  22. 'H' => 0,
  23. 'S' => 0.0,
  24. 'L' => 0.65,
  25. ];
  26. $white = [
  27. 'H' => 0,
  28. 'S' => 0.0,
  29. 'L' => 1.0,
  30. ];
  31. // Test cases.
  32. $colorsToConvert = array(
  33. 'blue' => [ // hsl(194, 100%, 40%)
  34. 'hex' => '009ccc',
  35. 'hsl' => $blue,
  36. ],
  37. 'yellow' => [ // hsl(57, 91%, 51%)
  38. 'hex' => 'f4e810',
  39. 'hsl' => $yellow,
  40. ],
  41. 'black' => [
  42. 'hex' => '000000',
  43. 'hsl' => $black,
  44. ],
  45. 'grey' => [
  46. 'hex' => 'a6a6a6',
  47. 'hsl' => $grey,
  48. ],
  49. 'white' => [
  50. 'hex' => 'ffffff',
  51. 'hsl' => $white,
  52. ],
  53. );
  54. foreach ($colorsToConvert as $color) {
  55. $hsl = $color['hsl'];
  56. $hex = $color['hex'];
  57. $answer = Color::hslToHex($hsl);
  58. Assert::same(
  59. $hex,
  60. $answer,
  61. 'Incorrect hex result: "' . json_encode($hsl) .
  62. '" should convert to "' . $hex .
  63. '", but output was: "' . $answer . '".'
  64. );
  65. }