RGB.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace fphammerle\helpers\colors;
  3. class RGB
  4. {
  5. use \fphammerle\helpers\PropertyAccessTrait;
  6. const comparison_precision = 0.00001;
  7. private $_red;
  8. private $_green;
  9. private $_blue;
  10. public function __construct($red = 0, $green = 0, $blue = 0)
  11. {
  12. $this->setRed($red);
  13. $this->setGreen($green);
  14. $this->setBlue($blue);
  15. }
  16. public function getRed()
  17. {
  18. return $this->_red;
  19. }
  20. public function setRed($red)
  21. {
  22. $red = (float)$red;
  23. if($red < 0 || $red > 1) {
  24. throw new \UnexpectedValueException('value must be within [0, 1]');
  25. }
  26. $this->_red = $red;
  27. }
  28. public function getGreen()
  29. {
  30. return $this->_green;
  31. }
  32. public function setGreen($green)
  33. {
  34. $green = (float)$green;
  35. if($green < 0 || $green > 1) {
  36. throw new \UnexpectedValueException('value must be within [0, 1]');
  37. }
  38. $this->_green = $green;
  39. }
  40. public function getBlue()
  41. {
  42. return $this->_blue;
  43. }
  44. public function setBlue($blue)
  45. {
  46. $blue = (float)$blue;
  47. if($blue < 0 || $blue > 1) {
  48. throw new \UnexpectedValueException('value must be within [0, 1]');
  49. }
  50. $this->_blue = $blue;
  51. }
  52. public function equals(RGB $other)
  53. {
  54. return abs($this->red - $other->red) < self::comparison_precision
  55. && abs($this->green - $other->green) < self::comparison_precision
  56. && abs($this->blue - $other->blue) < self::comparison_precision;
  57. }
  58. }