1
0

HSV.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace fphammerle\helpers\colors;
  3. class HSV
  4. {
  5. use \fphammerle\helpers\PropertyAccessTrait;
  6. const comparison_precision = 0.0001;
  7. private $_hue;
  8. private $_saturation;
  9. private $_value;
  10. public function __construct($hue = 0.0, $saturation = 0.0, $value = 0.0)
  11. {
  12. $this->setHue($hue);
  13. $this->setSaturation($saturation);
  14. $this->setValue($value);
  15. }
  16. public function getHue()
  17. {
  18. return $this->_hue;
  19. }
  20. public function setHue($hue)
  21. {
  22. $hue = (float)$hue;
  23. if($hue < 0 || $hue >= 2 * pi()) {
  24. throw new \UnexpectedValueException('value must be within [0, 2*pi)');
  25. }
  26. $this->_hue = $hue;
  27. }
  28. public function getSaturation()
  29. {
  30. return $this->_saturation;
  31. }
  32. public function setSaturation($saturation)
  33. {
  34. $saturation = (float)$saturation;
  35. if($saturation < 0 || $saturation > 1) {
  36. throw new \UnexpectedValueException('value must be within [0, 1]');
  37. }
  38. $this->_saturation = $saturation;
  39. }
  40. public function getValue()
  41. {
  42. return $this->_value;
  43. }
  44. public function setValue($value)
  45. {
  46. $value = (float)$value;
  47. if($value < 0 || $value > 1) {
  48. throw new \UnexpectedValueException('value must be within [0, 1]');
  49. }
  50. $this->_value = $value;
  51. }
  52. public function getTuple()
  53. {
  54. return [$this->hue, $this->saturation, $this->value];
  55. }
  56. public function equals(HSV $other)
  57. {
  58. return abs($this->hue - $other->hue) < self::comparison_precision
  59. && abs($this->saturation - $other->saturation) < self::comparison_precision
  60. && abs($this->value - $other->value) < self::comparison_precision;
  61. }
  62. public function toRGB()
  63. {
  64. // @see https://en.wikipedia.org/wiki/HSL_and_HSV#Converting_to_RGB
  65. $chroma = $this->value * $this->saturation; // [0, 1]
  66. $basic_hue = $this->hue / deg2rad(60); // [0, 6]
  67. $x = $chroma * (1 - abs(fmod($basic_hue, 2.0) - 1));
  68. if(0.0 <= $basic_hue && $basic_hue < 1.0) {
  69. $r = $chroma; $g = $x; $b = 0.0;
  70. } elseif(1.0 <= $basic_hue && $basic_hue < 2.0) {
  71. $r = $x; $g = $chroma; $b = 0.0;
  72. } elseif(2.0 <= $basic_hue && $basic_hue < 3.0) {
  73. $r = 0.0; $g = $chroma; $b = $x;
  74. } elseif(3.0 <= $basic_hue && $basic_hue < 4.0) {
  75. $r = 0.0; $g = $x; $b = $chroma;
  76. } elseif(4.0 <= $basic_hue && $basic_hue < 5.0) {
  77. $r = $x; $g = 0.0; $b = $chroma;
  78. } elseif(5.0 <= $basic_hue && $basic_hue < 6.0) {
  79. $r = $chroma; $g = 0.0; $b = $x;
  80. } else {
  81. $r = 0.0; $g = 0.0; $b = 0.0;
  82. }
  83. $m = $this->value - $chroma;
  84. return new RGB($r + $m, $g + $m, $b + $m);
  85. }
  86. }