1
0

RGBA.php 1004 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. namespace fphammerle\helpers\colors;
  3. class RGBA extends RGB
  4. {
  5. private $_alpha;
  6. public function __construct($red = 0, $green = 0, $blue = 0, $alpha = 1)
  7. {
  8. $this->setRed($red);
  9. $this->setGreen($green);
  10. $this->setBlue($blue);
  11. $this->setAlpha($alpha);
  12. }
  13. public function getAlpha()
  14. {
  15. return $this->_alpha;
  16. }
  17. /**
  18. * alpha 0 => 100% transparency
  19. * alpha 1 => opaque, 0% transparency
  20. */
  21. public function setAlpha($alpha)
  22. {
  23. $alpha = (float)$alpha;
  24. if($alpha < 0 || $alpha > 1) {
  25. throw new \UnexpectedValueException('value must be within [0, 1]');
  26. }
  27. $this->_alpha = $alpha;
  28. }
  29. public function equals(RGB $other)
  30. {
  31. return parent::equals($other)
  32. && abs($this->alpha - $other->alpha) < self::comparison_precision;
  33. }
  34. public function getTuple()
  35. {
  36. return [$this->red, $this->green, $this->blue, $this->alpha];
  37. }
  38. }