| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Vctls\IntervalGraph; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use LogicException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * Holds color information for the graph as an array of CSS classes. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @package Vctls\IntervalGraph | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | class ClassPalette implements PaletteInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * @var array $palette An array of percentages with corresponding color codes. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * In order to keep a simple array, the pairs are defined following these rules: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * Pairs are sorted by value in ascending order. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * The first pair defines the color for all lower values. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * The last pair defines the color for all higher values. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * When two pairs with the same value are consecutive, the second pair defines | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * the color of this discrete value. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     protected $palette = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         [0, 'color_0'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         [0, 'color_0'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         [50, 'color_1'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         [100, 'color_2'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         [100, 'color_3'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         [100, 'color_4'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     protected $bgColor = '#e1e0eb'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * Set the color palette for percent ranges. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * Ranges should be simple arrays, containing only the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * upper bound and the corresponding color value, like this: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      *  [ 50, '.color_3' ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * For discrete values, simply insert the same value twice. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * Make sure values are in the correct order. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @param array $palette | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @return ClassPalette | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 48 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |     public function setColors(array $palette): ClassPalette | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |         $this->palette = $palette; | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * @param string $color A color reference or hex code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     public function setBGColor(string $color): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $this->bgColor = $color; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * Get the hexadecimal color code for the given percentage. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * @param int|null $percent | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     public function getColor(int $percent = null): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $pal = $this->palette; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         if ($percent === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             return $this->bgColor ?? ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         foreach ($pal as $i => $iValue) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             if (($i === 0 && $percent < $iValue[0]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                 || ($i > 0 && $iValue[0] === $pal[$i - 1][0] && $percent === $iValue[0]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                 || ($i > 0 && $iValue[0] !== $pal[$i - 1][0] && $percent < $iValue[0]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                 || ($i === (count($pal) - 1) && $percent > $iValue[0]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |                 return $iValue[1]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         throw new LogicException("The percentage $percent did not match any range in the color palette."); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 89 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 90 |  |  |  |