View source
<?php
namespace Drupal\color_field;
class ColorRGB extends ColorBase {
private $red;
private $green;
private $blue;
public function __construct($red, $green, $blue, $opacity) {
if ($red < 0 || $red > 255) {
}
if ($green < 0 || $green > 255) {
}
if ($blue < 0 || $blue > 255) {
}
$this->red = $red;
$this->green = $green;
$this->blue = $blue;
$this->opacity = floatval($opacity);
}
public function getRed() {
return 0.5 + $this->red | 0;
}
public function getGreen() {
return 0.5 + $this->green | 0;
}
public function getBlue() {
return 0.5 + $this->blue | 0;
}
public function toString($opacity = TRUE) {
if ($opacity) {
$output = 'rgba(' . $this
->getRed() . ',' . $this
->getGreen() . ',' . $this
->getBlue() . ',' . $this
->getOpacity() . ')';
}
else {
$output = 'rgb(' . $this
->getRed() . ',' . $this
->getGreen() . ',' . $this
->getBlue() . ')';
}
return strtolower($output);
}
public function toHex() {
return new ColorHex($this
->getRed() << 16 | $this
->getGreen() << 8 | $this
->getBlue(), $this
->getOpacity());
}
public function toRgb() {
return $this;
}
public function toHsl() {
$r = $this
->getRed() / 255;
$g = $this
->getGreen() / 255;
$b = $this
->getBlue() / 255;
$max = max($r, $g, $b);
$min = min($r, $g, $b);
$l = ($max + $min) / 2;
if ($max == $min) {
$h = $s = 0;
}
else {
$d = $max - $min;
$s = $l > 0.5 ? $d / (2 - $max - $min) : $d / ($max + $min);
switch ($max) {
case $r:
$h = ($g - $b) / $d + ($g < $b ? 6 : 0);
break;
case $g:
$h = ($b - $r) / $d + 2;
break;
case $b:
$h = ($r - $g) / $d + 4;
break;
}
$h /= 6;
}
$h = floor($h * 360);
$s = floor($s * 100);
$l = floor($l * 100);
return new ColorHSL(intval($h), intval($s), intval($l), $this
->getOpacity());
}
}