< >

zuffalsfarben-in-hunderten-von-farblichen-quadraten.php


Quell Code


<?php
class Color {
    const RegExpHex = "/^#?([a-fA-F0-9]{3}|[a-fA-F0-9]{6}|[a-fA-F0-9]{8})$/";
    
    static protected $_names = array(
        "black" => "#000000",
        "white" => "#FFFFFF",
        "red"   => "#FF0000",
        "green" => "#00FF00",
        "blue"  => "#0000FF");

    protected $_red;
    protected $_green;
    protected $_blue;
    protected $_opacity = 255;
    
    public function __construct($color = null) {
        $this->setColor($color);
    }
    
    public function __toString()
    {
        return $this->getHex(false, 6);
    }
    
    public function getHex($hash = false, $length = 6)
    {
        assert('$length == 6 || $length == 8');
        if ($hash) {
            $hex = "#";
        } else {
            $hex = "";
        }
        $hex .= $this->getRed(true) . $this->getGreen(true) . $this->getBlue(true);
        if ($length == 8) {
            $hex .= $this->getOpacity(true);
        }
        return $hex;
    }   
    
    public function setColor($color) {
        if ($this->_isHex($color)) {
            $this->setHex($color);
        } elseif ($this->_isName($color)) {
            $this->setName($color);
        } elseif ($color) {
            throw Color_Exception("unknown color format: $color");
        }
    }
    
    public function setHex($hex)
    {
        $hex     = ltrim($hex, "#");
        $length  = mb_strlen($hex);
        $divisor = $length == 3 ? 2 : 1; // allow #333 (= #333333)
        
        $this->_red   = hexdec(mb_substr($hex, 0,            2 / $divisor));
        $this->_green = hexdec(mb_substr($hex, 2 / $divisor, 2 / $divisor));
        $this->_blue  = hexdec(mb_substr($hex, 4 / $divisor, 2 / $divisor));
        
        if ($length > 6) {
            $this->_opacity = hexdec(mb_substr($hex, 6, 2));
        }
    }
    
    public function getBrightness()
    {
        return (float)sqrt(
            pow($this->_red   / 255, 2) * .241 +
            pow($this->_green / 255, 2) * .691 +
            pow($this->_blue  / 255, 2) * .068);
    }
    
    public function isBright($threshold = 0.5)
    {
        assert('0.0 < $threshold && $threshold < 1.0');
        return $this->getBrightness() > $threshold;
    }
    
    public function isDark($threshold = 0.5)
    {
        return $this->isBright(1 - $threshold);
    }
    
    public function getMix(self $color, $ratio = 0.5)
    {
        assert('0 < $ratio && $ratio < 1');
        
        if ($this->getOpacity(false) == 0) {
            $newColor = clone $color;
        } elseif ($color->getOpacity(false) == 0) {
            $newColor = clone $this;
        } else {
            $inv      = abs(1.0 - $ratio);
            
            $red      = $this->_toHex(($color->getRed(false)     * $ratio) + ($this->getRed(false)     * $inv));
            $green    = $this->_toHex(($color->getGreen(false)   * $ratio) + ($this->getGreen(false)   * $inv));
            $blue     = $this->_toHex(($color->getBlue(false)    * $ratio) + ($this->getBlue(false)    * $inv));
            $opacity  = $this->_toHex(($color->getOpacity(false) * $ratio) + ($this->getOpacity(false) * $inv));
            
            $newColor = new self("#$red$green$blue$opacity");
        }
        return $newColor;
    }
    
    public function setRed($red, $isHex)
    {
        $this->_red = $this->_setValue($red, $isHex);
    }
    
    public function getRed($asHex)
    {
        return $this->_getValue($this->_red, $asHex);
    }
    
    public function setGreen($green, $isHex)
    {
        $this->_green = $this->_setValue($green, $isHex);
    }
    
    public function getGreen($asHex)
    {
        return $this->_getValue($this->_green, $asHex);
    }
    
    public function setBlue($blue, $isHex)
    {
        $this->_blue = $this->_setValue($blue, $isHex);
    }
    
    public function getBlue($asHex)
    {
        return $this->_getValue($this->_blue, $asHex);
    }
    
    public function setOpacity($opacity, $isHex)
    {
        $this->_opacity = $this->_setValue($opacity, $isHex);
    }
    
    public function getOpacity($asHex)
    {
        return $this->_getValue($this->_opacity, $asHex);
    }
    
    public function setAlpha($alpha, $isHex)
    {
        $alpha   = $this->_setValue($alpha, $isHex);
        $opacity = abs(255 - $alpha);
        $this->_opacity = $opacity;
    }
    
    public function getAlpha($asHex)
    {
        $alpha = abs(255 - $this->_opacity);
        return $this->_getValue($alpha, $asHex);
    }
    
    protected function _isHex($color)
    {
        return is_string($color) && preg_match(self::RegExpHex, $color);
    }
    
    protected function _isName($color)
    {
        return in_array($color, self::$_names);
    }
    
    protected function _toHex($dec)
    {
        return str_pad(mb_strtoupper(dechex($dec)), 2, "0", STR_PAD_LEFT);
    }
    
    protected function _toDec($dec)
    {
        return (int)$dec; // might be null
    }
    
    protected function _getValue($dec, $asHex)
    {
        return $asHex
             ? $this->_toHex($dec)
             : $this->_toDec($dec);
    }
    
    protected function _setValue($value, $isHex)
    {
        if ($isHex) {
            $value = hexdec($value);
        }
        return max(0, min(255, $value));
    }
}

 

for ($i = 0; $i < 250; ++$i) {
    $color = new Color();
    $color->setRed(mt_rand(0, 255), false);
    $color->setGreen(mt_rand(0, 255), false);
    $color->setBlue(mt_rand(0, 255), false);
    
     echo '<div style="height:80px;width:80px;float:left;background-color:#' . $color . '">#' . $color . '<br/>' . ($color->isBright() ? "bright" : "dark") . '</div>';

}
echo "<hr style='clear:both;margin:25px 0' />";

$red = new Color();
$red->setRed(255, false);
$green = new Color();
$green->setGreen(255, false);
$res = $red->getMix($green);

?>