Advertisement
Guest User

Color Illusion

a guest
Mar 28th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.25 KB | None | 0 0
  1. <?php
  2.  
  3. class Illusion
  4. {
  5.     protected $color = [];
  6.     protected $changedColor = [];
  7.     protected $name;
  8.  
  9.     public function color($color)
  10.     {
  11.  
  12.         if (strlen($color) == 6 && ctype_xdigit($color)) {
  13.             $this->color ['red'] = substr($color, 0, 2);
  14.             $this->color ['green'] = substr($color, 2, 2);
  15.             $this->color ['blue'] = substr($color, 4, 2);
  16.  
  17.             return $this;
  18.         }
  19.  
  20.         return false;
  21.  
  22.     }
  23.  
  24.  
  25.  
  26.     public function nahid()
  27.     {
  28.         $change = ['red'=>100];
  29.         $this->name = 'Nahid';
  30.  
  31.         return $this->brainProcess($change);
  32.     }
  33.  
  34.     public function obi()
  35.     {
  36.         $change = ['green'=>100];
  37.         $this->name = 'Obi';
  38.  
  39.         return $this->brainProcess($change);
  40.     }
  41.  
  42.     public function naim()
  43.     {
  44.         $change = ['blue'=>100];
  45.         $this->name = 'Naim';
  46.  
  47.         return $this->brainProcess($change);
  48.     }
  49.  
  50.     public function see()
  51.     {
  52.         $originalColor = implode('', $this->color);
  53.         $seenColor = implode('', $this->changedColor);
  54.  
  55.         $output = '<h3>' . $this->name. '</h3>';
  56.         $output .= '<div style="width:100px; height:100px; background-color: #'. $originalColor .'; text-shadow: 1px 2px white;">Original</div>
  57.  
  58. <div style="width:100px; height:100px; background-color: #' .$seenColor .'; text-shadow: 1px 2px white;">Seen</div>';
  59.  
  60.     return $output;
  61.  
  62.     }
  63.  
  64.  
  65.     protected function brainProcess($change)
  66.     {
  67.         $color = [];
  68.  
  69.         foreach($this->color as $key=>$val) {
  70.             if (isset($change[$key])) {
  71.                 $processColor = hexdec($this->color[$key])+$change[$key];
  72.            
  73.                 if ($processColor<=255) {
  74.                     $this->changedColor[$key] = dechex($processColor);
  75.                 }else {
  76.                     $this->changedColor[$key] = dechex(hexdec($this->color[$key])-$change[$key]);
  77.                 }
  78.                
  79.             }else {
  80.                 $this->changedColor[$key] = $this->color[$key];
  81.             }
  82.  
  83.            
  84.         }
  85.  
  86.         return $this;
  87.  
  88.     }
  89.  
  90.  
  91. }
  92.  
  93. $illu = new Illusion;
  94.  
  95. $color = $illu->color('00ffff');
  96.  
  97. echo $color->nahid()->see();
  98. echo $color->obi()->see();
  99. echo $color->naim()->see();
  100. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement