Created
November 15, 2015 15:25
-
-
Save thekid/92e020b9f5bbc7e9cb5f to your computer and use it in GitHub Desktop.
Color class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php namespace lang\functions\unittest; | |
| /** | |
| * Represents an RGB color | |
| * | |
| * @see http://docs.oracle.com/javase/8/docs/api/java/awt/Color.html | |
| */ | |
| class Color implements \lang\Value { | |
| const FACTOR= 0.7; | |
| private $r, $g, $b; | |
| public function __construct($r, $g, $b) { | |
| $this->r= $r; | |
| $this->g= $g; | |
| $this->b= $b; | |
| } | |
| public function brighter() { | |
| $i= (int)(1.0 / (1.0 - self::FACTOR)); | |
| if (0 === $this->r && 0 === $this->g && 0 === $this->b) { | |
| return new self($i, $i, $i); | |
| } else { | |
| $r= ($this->r > 0 && $this->r < $i) ? $i : $this->r; | |
| $g= ($this->g > 0 && $this->g < $i) ? $i : $this->g; | |
| $b= ($this->b > 0 && $this->b < $i) ? $i : $this->b; | |
| return new self( | |
| min((int)($r / self::FACTOR), 0xff), | |
| min((int)($g / self::FACTOR), 0xff), | |
| min((int)($b / self::FACTOR), 0xff) | |
| ); | |
| } | |
| } | |
| public function darker() { | |
| return new self( | |
| max((int)($this->r * self::FACTOR), 0), | |
| max((int)($this->g * self::FACTOR), 0), | |
| max((int)($this->b * self::FACTOR), 0) | |
| ); | |
| } | |
| public function hashCode() { | |
| return sprintf('#%02x%02x%02x', $this->r, $this->g, $this->b); | |
| } | |
| public function toString() { | |
| return sprintf('%s(r= %d, g= %d, b= %d)', nameof($this), $this->r, $this->g, $this->b); | |
| } | |
| public function compareTo($that) { | |
| if ($that instanceof self) { | |
| $a= $this->r << 16 | $this->g << 8 | $this->b; | |
| $b= $that->r << 16 | $that->g << 8 | $that->b; | |
| return $a === $b ? 0 : ($a < $b ? -1 : 1); | |
| } else { | |
| return 1; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment