meter_color_t stateful_execute_meter(meter_t *meter, int index, uint32_t input) { pthread_mutex_lock(&meter->lock); meter_instance_t *instance = &meter->instances[index]; struct timeval now; gettimeofday(&now, NULL); uint64_t time_since_init = (now.tv_sec - time_init.tv_sec) * 1000000 + (now.tv_usec - time_init.tv_usec); uint64_t usec_diff; uint32_t new_tokens; meter_queue_t *queue; meter_color_t color = METER_EXCEED_ACTION_COLOR_GREEN; int i; for(i = 1; i < METER_EXCEED_ACTION_COLOR_END_; i++) { queue = &instance->queues[i]; if(!queue->valid) continue; usec_diff = time_since_init - queue->last_timestamp; new_tokens = (uint32_t) (usec_diff * queue->info_rate); if(new_tokens > 0) { queue->tokens += new_tokens; if(queue->tokens > queue->burst_size) { queue->tokens = queue->burst_size; } /* TODO : improve this ? */ queue->last_timestamp += (uint32_t) (new_tokens / queue->info_rate); } RMT_LOG(P4_LOG_LEVEL_TRACE, "adding %u tokens, now have %u\n", new_tokens, queue->tokens); if(queue->tokens < input) { color = queue->color; } else { queue->tokens -= input; break; } } instance->counters[color]++; pthread_mutex_unlock(&meter->lock); RMT_LOG(P4_LOG_LEVEL_VERBOSE, "meter %s is marking packet %s\n", meter->name, color_to_str(color)); return color; }
// Gdk::Color -> 16進数表記の文字列 std::string MISC::color_to_str( const Gdk::Color& color ) { // R,G,Bを取得 int l_rgb[3]; l_rgb[0] = color.get_red(); l_rgb[1] = color.get_green(); l_rgb[2] = color.get_blue(); return color_to_str( l_rgb ); }
// htmlカラー (#ffffffなど) -> 16進数表記の文字列 std::string MISC::htmlcolor_to_str( const std::string& _htmlcolor ) { std::string htmlcolor = _htmlcolor; int rgb[ 3 ]; if( htmlcolor == "red" ) htmlcolor = "#ff0000"; else if( htmlcolor == "fuchsia" ) htmlcolor = "#ff00ff"; else if( htmlcolor == "purple" ) htmlcolor = "#800080"; else if( htmlcolor == "maroon" ) htmlcolor = "#800000"; else if( htmlcolor == "yellow" ) htmlcolor = "#ffff00"; else if( htmlcolor == "lime" ) htmlcolor = "#00ff00"; else if( htmlcolor == "green" ) htmlcolor = "#008000"; else if( htmlcolor == "olive" ) htmlcolor = "#808000"; else if( htmlcolor == "blue" ) htmlcolor = "#0000ff"; else if( htmlcolor == "aqua" ) htmlcolor = "#00ffff"; else if( htmlcolor == "teal" ) htmlcolor = "#008080"; else if( htmlcolor == "navy" ) htmlcolor = "#000080"; else if( htmlcolor == "white" ) htmlcolor = "#ffffff"; else if( htmlcolor == "silver" ) htmlcolor = "#c0c0c0"; else if( htmlcolor == "gray" ) htmlcolor = "#808080"; else if( htmlcolor == "black" ) htmlcolor = "#000000"; int offset = 0; if( htmlcolor.find( "#" ) == 0 ) offset = 1; std::string tmpstr = htmlcolor.substr( offset, 2 ); rgb[ 0 ] = strtol( std::string( "0x" + tmpstr + tmpstr ).c_str(), NULL, 16 ); tmpstr = htmlcolor.substr( 2 + offset, 2 ); rgb[ 1 ] = strtol( std::string( "0x" + tmpstr + tmpstr ).c_str(), NULL, 16 ); tmpstr = htmlcolor.substr( 4 + offset, 2 ); rgb[ 2 ] = strtol( std::string( "0x" + tmpstr + tmpstr ).c_str(), NULL, 16 ); #ifdef _DEBUG std::cout << "MISC::htmlcolor_to_gdkcolor color = " << htmlcolor << " r = " << rgb[ 0 ] << " g = " << rgb[ 1 ] << " b = " << rgb[ 2 ] << std::endl; #endif return color_to_str( rgb ); }
// Given a Config variable name and a default COLOR value, // return the corresponding COLOR if the variable is defined, // otherwise return the default value: inline CCOLOR get_var_color(const string& var_name, CCOLOR& default_val) { string col_str = Config::get_var_str(var_name, color_to_str(default_val)); double r, g, b; sscanf(col_str.c_str(), "%lf%lf%lf", &r, &g, &b); return COLOR(r,g,b); }
void write_color_scheme_file(void) { FILE *fp; char colors_dir[PATH_MAX]; int i; snprintf(colors_dir, sizeof(colors_dir), "%s/colors", cfg.config_dir); if(make_dir(colors_dir, 0777) != 0) return; strncat(colors_dir, "/Default", sizeof(colors_dir) - strlen(colors_dir) - 1); if((fp = fopen(colors_dir, "w")) == NULL) return; fprintf(fp, "\" You can edit this file by hand.\n"); fprintf(fp, "\" The \" character at the beginning of a line comments out the line.\n"); fprintf(fp, "\" Blank lines are ignored.\n\n"); fprintf(fp, "\" The Default color scheme is used for any directory that does not have\n"); fprintf(fp, "\" a specified scheme and for parts of user interface like menus. A\n"); fprintf(fp, "\" color scheme set for a base directory will also\n"); fprintf(fp, "\" be used for the sub directories.\n\n"); fprintf(fp, "\" The standard ncurses colors are:\n"); fprintf(fp, "\" Default = -1 = None, can be used for transparency or default color\n"); fprintf(fp, "\" Black = 0\n"); fprintf(fp, "\" Red = 1\n"); fprintf(fp, "\" Green = 2\n"); fprintf(fp, "\" Yellow = 3\n"); fprintf(fp, "\" Blue = 4\n"); fprintf(fp, "\" Magenta = 5\n"); fprintf(fp, "\" Cyan = 6\n"); fprintf(fp, "\" White = 7\n\n"); fprintf(fp, "\" Light versions of colors are also available (set bold attribute):\n"); fprintf(fp, "\" LightBlack\n"); fprintf(fp, "\" LightRed\n"); fprintf(fp, "\" LightGreen\n"); fprintf(fp, "\" LightYellow\n"); fprintf(fp, "\" LightBlue\n"); fprintf(fp, "\" LightMagenta\n"); fprintf(fp, "\" LightCyan\n"); fprintf(fp, "\" LightWhite\n\n"); fprintf(fp, "\" Available attributes (some of them can be combined):\n"); fprintf(fp, "\" bold\n"); fprintf(fp, "\" underline\n"); fprintf(fp, "\" reverse or inverse\n"); fprintf(fp, "\" standout\n"); fprintf(fp, "\" none\n\n"); fprintf(fp, "\" Vifm supports 256 colors you can use color numbers 0-255\n"); fprintf(fp, "\" (requires properly set up terminal: set your TERM environment variable\n"); fprintf(fp, "\" (directly or using resources) to some color terminal name (e.g.\n"); fprintf(fp, "\" xterm-256color) from /usr/lib/terminfo/; you can check current number\n"); fprintf(fp, "\" of colors in your terminal with tput colors command)\n\n"); fprintf(fp, "\" highlight group cterm=attrs ctermfg=foreground_color ctermbg=background_color\n\n"); for(i = 0; i < MAXNUM_COLOR - 2; ++i) { char fg_buf[16], bg_buf[16]; if(i == OTHER_LINE_COLOR) { /* Skip OtherLine as there is no way to express defaults. */ continue; } color_to_str(cfg.cs.color[i].fg, sizeof(fg_buf), fg_buf); color_to_str(cfg.cs.color[i].bg, sizeof(bg_buf), bg_buf); fprintf(fp, "highlight %s cterm=%s ctermfg=%s ctermbg=%s\n", HI_GROUPS[i], attrs_to_str(cfg.cs.color[i].attr), fg_buf, bg_buf); } fclose(fp); }