?? colors.cc
字號:
/* * Stage : a multi-robot simulator. * Copyright (C) 2001, 2002 Richard Vaughan, Andrew Howard and Brian Gerkey. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * *//* * Desc: Some useful color routines * Author: Andrew Howard, Richard Vaughan * Date: 8 Jun 2002 * CVS info: $Id: colors.cc,v 1.2.8.1 2003/04/17 23:40:10 rtv Exp $ */#include <errno.h>#include <string.h>#include <stdio.h>#include "stage_types.hh"#include "colors.hh"///////////////////////////////////////////////////////////////////////////// Look up the color in a data based (transform color name -> color value).StageColor LookupColor(const char *name){ FILE *file; const char *filename; filename = COLOR_DATABASE; file = fopen(filename, "r"); if (!file) { PRINT_ERR2("unable to open the X11 color database %s : %s", filename, strerror(errno)); fclose(file); return 0xFFFFFF; } while (true) { char line[1024]; if (!fgets(line, sizeof(line), file)) break; // it's a macro or comment line - ignore the line if (line[0] == '!' || line[0] == '#' || line[0] == '%') continue; // Trim the trailing space while (strchr(" \t\n", line[strlen(line)-1])) line[strlen(line)-1] = 0; // Read the color int r, g, b; int chars_matched = 0; sscanf( line, "%d %d %d %n", &r, &g, &b, &chars_matched ); // Read the name char* nname = line + chars_matched; // If the name matches if (strcmp(nname, name) == 0) { fclose(file); return ((r << 16) | (g << 8) | b); } } PRINT_WARN1("unable to find color [%s]; using default (red)", name); fclose(file); return 0xFF0000;}