Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
Color.php
1<?php
10
11class Color
12{
13 protected
18
26 public function __construct($red = 0, $green = 0, $blue = 0, $alpha = 1.0)
27 {
28 $this->setRed($red)
29 ->setGreen($green)
30 ->setBlue($blue)
31 ->setAlpha($alpha);
32 }
33
39 public static function createFromHex($color)
40 {
41 $color = preg_replace("/[^a-f0-9]/is", "", $color);
42 if(strlen($color) != 6)
43 {
44 $color = "FF0000";
45 }
46
47 return new static(
48 hexdec(substr($color, 0, 2)),
49 hexdec(substr($color, 2, 2)),
50 hexdec(substr($color, 4, 2))
51 );
52 }
53
58 public function toHex()
59 {
60 return sprintf("#%02x%02x%02x", $this->getRed(), $this->getGreen(), $this->getBlue());
61 }
62
67 public function toRgba()
68 {
69 return sprintf("rgba(%d, %d, %d, %0.4f)", $this->getRed(), $this->getGreen(), $this->getBlue(), $this->getAlpha());
70 }
71
75 public function getRed()
76 {
77 return $this->red;
78 }
79
84 public function setRed($red)
85 {
86 if($red < 0 || $red > 255)
87 {
88 $red = 0;
89 }
90 $this->red = (int)$red;
91 return $this;
92 }
93
97 public function getGreen()
98 {
99 return $this->green;
100 }
101
106 public function setGreen($green)
107 {
108 if($green < 0 || $green > 255)
109 {
110 $green = 0;
111 }
112 $this->green = (int)$green;
113 return $this;
114 }
115
119 public function getBlue()
120 {
121 return $this->blue;
122 }
123
128 public function setBlue($blue)
129 {
130 if($blue < 0 || $blue > 255)
131 {
132 $blue = 0;
133 }
134 $this->blue = (int)$blue;
135 return $this;
136 }
137
141 public function getAlpha()
142 {
143 return $this->alpha;
144 }
145
150 public function setAlpha($alpha)
151 {
152 $this->alpha = (float)$alpha;
153 return $this;
154 }
155}
__construct($red=0, $green=0, $blue=0, $alpha=1.0)
Definition Color.php:26
static createFromHex($color)
Definition Color.php:39