gschem

x_color.c

Go to the documentation of this file.
00001 /* gEDA - GPL Electronic Design Automation
00002  * gschem - gEDA Schematic Capture
00003  * Copyright (C) 1998-2010 Ales Hvezda
00004  * Copyright (C) 1998-2010 gEDA Contributors (see ChangeLog for details)
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00019  */
00020 #include <config.h>
00021 
00022 #include <stdio.h>
00023 #ifdef HAVE_STRING_H
00024 #include <string.h>
00025 #endif
00026 #include <math.h>
00027 
00028 #include "gschem.h"
00029 
00030 #ifdef HAVE_LIBDMALLOC
00031 #include <dmalloc.h>
00032 #endif
00033 
00034 COLOR display_colors[MAX_COLORS];
00035 COLOR display_outline_colors[MAX_COLORS];
00036 
00037 static GdkColor* gdk_colors[MAX_COLORS];
00038 static GdkColor* gdk_outline_colors[MAX_COLORS];
00039 
00040 static GdkColormap *colormap = NULL;
00041 
00047 void
00048 x_color_init (void)
00049 {
00050   colormap = gdk_colormap_get_system ();
00051 
00052   /* Initialise default color maps */
00053   s_color_map_defaults (display_colors);
00054   s_color_map_defaults (display_outline_colors);
00055 }
00056 
00062 void
00063 x_color_free (void)
00064 {
00065   int i;
00066 
00067   gdk_colormap_free_colors (colormap, &black, 1);
00068   gdk_colormap_free_colors (colormap, &white, 1);
00069 
00070   for (i = 0; i < MAX_COLORS; i++) {
00071     if (display_colors[i].enabled)
00072       gdk_colormap_free_colors (colormap, gdk_colors[i], 1);
00073     if (display_outline_colors[i].enabled)
00074       gdk_colormap_free_colors (colormap, gdk_outline_colors[i], 1);
00075   }
00076 }
00077 
00083 void x_color_allocate (void)
00084 {
00085   int error;
00086   int i;        
00087   COLOR c;
00088 
00089   gdk_color_parse ("black", &black);
00090   if (!gdk_colormap_alloc_color (colormap,
00091                                  &black,
00092                                  FALSE,
00093                                  TRUE)) {
00094     fprintf (stderr, _("Could not allocate the color %s!\n"), _("black"));
00095     exit (-1);
00096   }
00097 
00098   gdk_color_parse ("white", &white);
00099   if (!gdk_colormap_alloc_color (colormap,
00100                                  &white,
00101                                  FALSE,
00102                                  TRUE)) {
00103     fprintf (stderr, _("Could not allocate the color %s!\n"), _("white"));
00104     exit (-1);
00105   }
00106 
00107   for (i = 0; i < MAX_COLORS; i++) {
00108 
00109     if (display_colors[i].enabled) {
00110       gdk_colors[i] = (GdkColor *)
00111         g_malloc(sizeof(GdkColor));
00112 
00113       c = display_colors[i];
00114 
00115       /* Interpolate 8-bpp colours into 16-bpp GDK color
00116        * space. N.b. ignore transparency because GDK doesn't
00117        * understand it. */
00118       gdk_colors[i]->red = c.r + (c.r<<8);
00119       gdk_colors[i]->green = c.g + (c.g<<8);
00120       gdk_colors[i]->blue = c.b + (c.b<<8);
00121 
00122       error = gdk_color_alloc(colormap, gdk_colors[i]);
00123 
00124       if (error == FALSE) {
00125         g_error (_("Could not allocate display color %i!\n"), i);
00126       }
00127     } else {
00128       gdk_colors[i] = NULL;
00129     }
00130 
00131     if (display_outline_colors[i].enabled) {
00132       gdk_outline_colors[i] = (GdkColor *)
00133         g_malloc(sizeof(GdkColor));
00134 
00135       c = display_outline_colors[i];
00136 
00137       /* Interpolate 8-bpp colours into 16-bpp GDK color
00138        * space. N.b. ignore transparency because GDK doesn't
00139        * understand it. */
00140       gdk_outline_colors[i]->red = c.r + (c.r<<8);
00141       gdk_outline_colors[i]->green = c.g + (c.g<<8);
00142       gdk_outline_colors[i]->blue = c.b + (c.b<<8);
00143 
00144       error = gdk_color_alloc(colormap, gdk_outline_colors[i]);
00145 
00146       if (error == FALSE) {
00147         g_error (_("Could not allocate outline color %i!\n"), i);
00148       }
00149     } else {
00150       gdk_outline_colors[i] = NULL;
00151     }
00152   }
00153 }
00154 
00160 GdkColor *x_get_color(int color)
00161 {
00162   if ((color < 0) || (color >= MAX_COLORS)
00163       || (gdk_colors[color] == NULL)) {
00164     g_warning (_("Tried to get an invalid color: %d\n"), color);
00165     return(&white);
00166   } else {
00167     return(gdk_colors[color]);
00168   }
00169 }
00170 
00177 GdkColor *x_get_darkcolor(int color)
00178 {
00179   if ((color < 0) || (color >= MAX_COLORS)
00180       || (gdk_outline_colors[color] == NULL)) {
00181     g_warning (_("Tried to get an invalid color: %d\n"), color);
00182     return(&white);
00183   } else {
00184     return(gdk_outline_colors[color]);
00185   }
00186 }
00187 
00193 COLOR *x_color_lookup (int color)
00194 {
00195   if (color < 0 || color >= MAX_COLORS ||
00196       !display_colors[color].enabled) {
00197     fprintf(stderr, _("Tried to get an invalid color: %d\n"), color);
00198     return &display_colors[DEFAULT_COLOR];
00199   } else {
00200     return &display_colors[color];
00201   }
00202 }
00203 
00210 COLOR *x_color_lookup_dark (int color)
00211 {
00212   if (color < 0 || color >= MAX_COLORS ||
00213       !display_outline_colors[color].enabled) {
00214     g_warning (_("Tried to get an invalid outline color: %d\n"), color);
00215     return &display_outline_colors[DEFAULT_COLOR];
00216   } else {
00217     return &display_outline_colors[color];
00218   }
00219 
00220 }
00221 
00227 gchar *x_color_get_name(int index)
00228 {
00229   COLOR c;
00230 
00231   if ((index >= MAX_COLORS) || (index < 0)) {
00232     return(NULL);
00233   }
00234 
00235   if (display_colors[index].enabled) {
00236     c = display_colors[index];
00237     return s_color_rgba_encode (c.r, c.g, c.b, c.a);
00238   }
00239 
00240   /* didn't find a color, but there still might be more */
00241   return(NULL);
00242 }
00243 
00244 gboolean
00245 x_color_display_enabled (int index)
00246 {
00247   return (gdk_colors[index] != NULL);
00248 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines