Advertisement
Guest User

Untitled

a guest
Feb 5th, 2015
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.59 KB | None | 0 0
  1. <?php
  2.  
  3. $color = new Colors('FF0000', 'FFFF00', 100);
  4.  
  5. var_dump($color->step(10)); // hex
  6. var_dump($color->step_rgb(10)); // rgb
  7.  
  8.  
  9. class Colors {
  10.    
  11.     function __construct ($start, $end, $steps){
  12.  
  13.         // convert hex into rgb arrays
  14.         $this->start = $this->hex_rgb($start);
  15.         $this->end = $this->hex_rgb($end);
  16.        
  17.         // calculate the step size required the two rgb arrays
  18.         $this->num_steps = $steps;
  19.         $this->step = $this->rgb_step($this->start, $this->end, $this->num_steps);
  20.        
  21.     }
  22.    
  23.  
  24.     // returns the hex color
  25.     public function step ($num){
  26.        
  27.         if ($num > $this->num_steps) $num = $this->num_steps;
  28.         return  str_pad(dechex($this->start['red']+($this->step['red']*$num)),2,'0',STR_PAD_LEFT).
  29.                 str_pad(dechex($this->start['green']+($this->step['green']*$num)),2,'0',STR_PAD_LEFT).
  30.                 str_pad(dechex($this->start['blue']+($this->step['blue']*$num)),2,'0',STR_PAD_LEFT);
  31.     }
  32.    
  33.    
  34.     public function step_rgb ($num){
  35.        
  36.         if ($num > $this->num_steps) $num = $this->num_steps;
  37.         return  intval($this->start['red']+($this->step['red']*$num)).','.
  38.                 intval($this->start['green']+($this->step['green']*$num)).','.
  39.                 intval($this->start['blue']+($this->step['blue']*$num));
  40.     }
  41.        
  42.    
  43.     private function hex_rgb($hex){
  44.        
  45.         return ['red' => hexdec(substr($hex,0,2)),
  46.                 'green' => hexdec(substr($hex,2,2)),
  47.                 'blue' => hexdec(substr($hex,4,2))];
  48.                
  49.     }
  50.    
  51.    
  52.     private function rgb_step($start, $end, $steps){
  53.        
  54.         return ['red' => ($end['red']-$start['red'])/$steps,
  55.                 'green' => ($end['green']-$start['green'])/$steps,
  56.                 'blue' => ($end['blue']-$start['blue'])/$steps];
  57.  
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement