kkarlos
7/20/2018 - 9:17 AM

Basic binary operations example

Basic binary operations example

/**
 * Color object
 */
class Color
{
  /**
	 * Flags that says which part of color we are working with
	 */
	const RED   = 0b100;
	const GREEN = 0b010;
	const BLUE  = 0b001;

/**
	 * Map color name to flag
	 *
	 * @var array
	 */
	protected static $colorMap = [
		'red'   => self::RED,
		'green' => self::GREEN, 
		'blue'  => self::BLUE,
	];
  
/**
	 * Lightens color or its part
	 * @param  float $percent 
	 * @param  int 	 $mode    
	 * @return self
	 */
	public function lighten($percent, $mode = self::RED + self::GREEN + self::BLUE)
	{
		foreach(self::$colorMap as $c => $colorMode)
		{
			if ($mode & $colorMode) {
        echo "Now i`m working with " . $c . PHP_EOL;
				//Do some color operation
      }
		}
		return $this;
	}
  
}

$color = new Color();

// Now i`m working with red
$color->lighten(10, $color::RED);

//Now i`m working with green 
//Now i`m working with blue
$color->lighten(10, $color::GREEN + $color::BLUE);