예제 #1
0
 public static function merge_colors($category_ids_temp, $custom_category_to_table_mapping = array())
 {
     //because I might unset some of the values in the $category_ids array
     $category_ids = reports::array_copy($category_ids_temp);
     //check if we're looking at category 0
     if (count($category_ids) == 0 || $category_ids[0] == '0') {
         return Kohana::config('settings.default_map_all');
     }
     $red = 0;
     $green = 0;
     $blue = 0;
     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     //Lets handle custom categories
     foreach ($custom_category_to_table_mapping as $cat_name => $custom_cats) {
         $where_str_color = "";
         //to get the colors we're gonna use
         $i = 0;
         foreach ($category_ids as $key => $id) {
             //check if we have a custom cateogry ID
             $delimiter_pos = strpos($id, "_");
             if ($delimiter_pos !== false) {
                 //get the custom category name
                 $custom_cat_name = substr($id, 0, $delimiter_pos);
                 //get the custom category's numeric id
                 $custom_cat_id = substr($id, $delimiter_pos + 1);
                 //does the custom_cat_name match our current custom cat
                 if ($cat_name == $custom_cat_name) {
                     $i++;
                     if ($i > 1) {
                         $where_str_color = $where_str_color . " OR ";
                     }
                     $where_str_color = $where_str_color . "id = " . $custom_cat_id;
                     unset($category_ids[$key]);
                 }
             }
         }
         if ($where_str_color != "") {
             //get the custom categories themselves and add up their colors:
             // Retrieve all the categories with their colors
             $categories = ORM::factory($custom_cats['child'])->where($where_str_color)->find_all();
             //now for each color break it into RGB, add them up, then normalize
             foreach ($categories as $category) {
                 $color = $category->category_color;
                 $numeric_colors = self::_hex2RGB($color);
                 $red = $red + $numeric_colors['red'];
                 $green = $green + $numeric_colors['green'];
                 $blue = $blue + $numeric_colors['blue'];
             }
         }
     }
     //end loop through all custom categorie sources
     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     //Next lets handle regular categories
     //first lets figure out the composite color that we're gonna usehere
     if (count($category_ids) > 0) {
         $where_str_color = "";
         //to get the colors we're gonna use
         $i = 0;
         foreach ($category_ids as $id) {
             $i++;
             if ($i > 1) {
                 $where_str_color = $where_str_color . " OR ";
             }
             $where_str_color = $where_str_color . "id = " . $id;
         }
         // Retrieve all the categories with their colors
         $categories = ORM::factory('category')->where($where_str_color)->find_all();
         //now for each color break it into RGB, add them up, then normalize
         foreach ($categories as $category) {
             $color = $category->category_color;
             $numeric_colors = self::_hex2RGB($color);
             $red = $red + $numeric_colors['red'];
             $green = $green + $numeric_colors['green'];
             $blue = $blue + $numeric_colors['blue'];
         }
     }
     //now normalize
     $color_length = sqrt($red * $red + $green * $green + $blue * $blue);
     //make sure there's no divide by zero
     if ($color_length == 0) {
         $color_length = 255;
     }
     $red = $red / $color_length * 255;
     $green = $green / $color_length * 255;
     $blue = $blue / $color_length * 255;
     //pad with zeros if there's too much space
     $red = dechex($red);
     if (strlen($red) < 2) {
         $red = "0" . $red;
     }
     $green = dechex($green);
     if (strlen($green) < 2) {
         $green = "0" . $green;
     }
     $blue = dechex($blue);
     if (strlen($blue) < 2) {
         $blue = "0" . $blue;
     }
     //now put the color back together and return it
     return $red . $green . $blue;
 }