HSV.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. }