PocketMine-MP 5.17.1 git-df4ada81e5d74a14046f27cf44a37dcee69d657e
Color.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20*/
21
22declare(strict_types=1);
23
24namespace pocketmine\color;
25
26use function count;
27use function intdiv;
28
29final class Color{
30 private int $a;
31 private int $r;
32 private int $g;
33 private int $b;
34
35 public function __construct(int $r, int $g, int $b, int $a = 0xff){
36 $this->r = $r & 0xff;
37 $this->g = $g & 0xff;
38 $this->b = $b & 0xff;
39 $this->a = $a & 0xff;
40 }
41
45 public function getA() : int{
46 return $this->a;
47 }
48
52 public function getR() : int{
53 return $this->r;
54 }
55
59 public function getG() : int{
60 return $this->g;
61 }
62
66 public function getB() : int{
67 return $this->b;
68 }
69
75 public static function mix(Color $color1, Color ...$colors) : Color{
76 $colors[] = $color1;
77 $count = count($colors);
78
79 $a = $r = $g = $b = 0;
80
81 foreach($colors as $color){
82 $a += $color->a;
83 $r += $color->r;
84 $g += $color->g;
85 $b += $color->b;
86 }
87
88 return new Color(intdiv($r, $count), intdiv($g, $count), intdiv($b, $count), intdiv($a, $count));
89 }
90
94 public static function fromRGB(int $code) : Color{
95 return new Color(($code >> 16) & 0xff, ($code >> 8) & 0xff, $code & 0xff);
96 }
97
101 public static function fromARGB(int $code) : Color{
102 return new Color(($code >> 16) & 0xff, ($code >> 8) & 0xff, $code & 0xff, ($code >> 24) & 0xff);
103 }
104
108 public function toARGB() : int{
109 return ($this->a << 24) | ($this->r << 16) | ($this->g << 8) | $this->b;
110 }
111
115 public static function fromRGBA(int $code) : Color{
116 return new Color(($code >> 24) & 0xff, ($code >> 16) & 0xff, ($code >> 8) & 0xff, $code & 0xff);
117 }
118
122 public function toRGBA() : int{
123 return ($this->r << 24) | ($this->g << 16) | ($this->b << 8) | $this->a;
124 }
125
129 public function equals(self $other) : bool{
130 return $this->a === $other->a && $this->r === $other->r && $this->g === $other->g && $this->b === $other->b;
131 }
132}
static mix(Color $color1, Color ... $colors)
Definition: Color.php:75
static fromRGBA(int $code)
Definition: Color.php:115
static fromRGB(int $code)
Definition: Color.php:94
static fromARGB(int $code)
Definition: Color.php:101
equals(self $other)
Definition: Color.php:129