那很有趣!
例子:
// examples
$c = Color(0x08090a)->more( 0xFF0000 , 100 )->remove(0x110000)->decrease(35)->add(0x002314);
$c->red = 0xF2;
$c->red->decrease(25);
您可以检查所有方法的来源,简短版本是添加、删除、增加、减少、更多、更少 - 元素和颜色的完整链接,添加了辅助函数 Color() 以使事情变得更容易。
<?php
class ColorElement {
private $value;
public function __construct( $value = 0 )
{
$this->setValue( $value );
}
public function add( $value )
{
$value = self::fixValue($value);
$this->value = self::fixValue( $this->value + $value );
return $this;
}
public function remove( $value )
{
$value = self::fixValue($value);
$this->value = self::fixValue( $this->value - $value );
return $this;
}
public function increase( $percentage=100 )
{
$percentage = self::fixPercentage($percentage);
$this->value = self::fixValue( $this->value + (int)(($this->value/100)*$percentage) );
return $this;
}
public function decrease( $percentage=100 )
{
$percentage = self::fixPercentage($percentage);
$this->value = self::fixValue( $this->value - (int)(($this->value/100)*$percentage) );
return $this;
}
public function less( $value , $percentage=100 )
{
$percentage = self::fixPercentage($percentage);
$value = self::fixValue($value);
$this->value = self::fixValue( $this->value - (int)(($value/100)*$percentage) );
return $this;
}
public function more( $value , $percentage=100 )
{
$percentage = self::fixPercentage($percentage);
$value = self::fixValue($value);
$this->value = self::fixValue( $this->value + (int)(($value/100)*$percentage) );
return $this;
}
public function setValue( $value )
{
$this->value = self::fixValue($value);
return $this;
}
public function getValue()
{
return $this->value;
}
public function __toString()
{
return sprintf('%02X' , $this->value);
}
public static function fixValue( $value )
{
return $value < 0 ? 0 : ($value > 255 ? 255 : $value);
}
public static function fixPercentage( $percentage )
{
return $percentage < 0 ? 0 : ($percentage > 100 ? 100 : $percentage);
}
}
class Color {
private $_red;
private $_green;
private $_blue;
public function __construct( $hex=0x000000 )
{
$this->_red = new ColorElement();
$this->_green = new ColorElement();
$this->_blue = new ColorElement();
$this->setColor($hex);
}
public function add( $hex )
{
list($red, $green, $blue) = self::hexRGB($hex);
$this->_red->add( $red );
$this->_green->add( $green );
$this->_blue->add( $blue );
return $this;
}
public function remove( $hex )
{
list($red, $green, $blue) = self::hexRGB($hex);
$this->_red->remove( $red );
$this->_green->remove( $green );
$this->_blue->remove( $blue );
return $this;
}
public function increase( $percentage=100 )
{
$this->_red->increase( $percentage );
$this->_green->increase( $percentage );
$this->_blue->increase( $percentage );
return $this;
}
public function decrease( $percentage=100 )
{
$this->_red->decrease( $percentage );
$this->_green->decrease( $percentage );
$this->_blue->decrease( $percentage );
return $this;
}
public function less( $hex , $percentage=100 )
{
list($red, $green, $blue) = self::hexRGB($hex);
$this->_red->less( $red , $percentage );
$this->_green->less( $green , $percentage );
$this->_blue->less( $blue , $percentage );
return $this;
}
public function more( $hex , $percentage=100 )
{
list($red, $green, $blue) = self::hexRGB($hex);
$this->_red->more( $red , $percentage );
$this->_green->more( $green , $percentage );
$this->_blue->more( $blue , $percentage );
return $this;
}
public function setColor( $hex )
{
list($this->red, $this->green, $this->blue) = self::hexRGB($hex);
return $this;
}
public function __set( $color , $value )
{
if( !in_array( $color, array('red','green','blue') ) ) return;
$this->{'_'.$color}->setValue( $value );
}
public function &__get( $color )
{
if( !in_array( $color, array('red','green','blue') ) ) return;
return $this->{'_'.$color};
}
public function __toString()
{
return '0x' . $this->_red . $this->_green . $this->_blue;
}
public static function hexRGB( $hex )
{
return array( $hex >> 16 & 0xFF , $hex >> 8 & 0xFF , $hex & 0xFF );
}
}
function Color( $hex=0x000000 ) {
return new Color( $hex );
}
希望有帮助!
编辑:刚刚赶上线程(在这样做之后),看到你想让 0xFFFF00 + 0x0000FF 变成绿色,而不是白色 - 叹息这不会那样做,它只适用于十六进制 rgb 颜色 - 抱歉!