1 /* Lepton EDA library
2  * Copyright (C) 1998-2010 Ales Hvezda
3  * Copyright (C) 1998-2015 gEDA Contributors
4  * Copyright (C) 2017-2021 Lepton EDA Contributors
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 /*!
22  * \file  color.c
23  * \brief Colors and color maps
24  */
25 
26 #include <config.h>
27 #include "liblepton_priv.h"
28 
29 
30 #define DEFAULT_COLOR GRAPHIC_COLOR
31 
32 
33 static LeptonColorMap print_colors;
34 LeptonColorMap display_colors;
35 LeptonColorMap display_outline_colors;
36 
37 
38 #define WHITE   {0xff, 0xff, 0xff, 0xff, TRUE}
39 #define GRAY    {0x88, 0x88, 0x88, 0xff, TRUE}
40 #define BLACK   {0x00, 0x00, 0x00, 0xff, TRUE}
41 
42 static LeptonColor default_colors[] =
43 {
44   WHITE,           /*  0: background         */
45   BLACK,           /*  1: pin                */
46   BLACK,           /*  2: net-endpoint       */
47   BLACK,           /*  3: graphic            */
48   BLACK,           /*  4: net                */
49   BLACK,           /*  5: attribute          */
50   BLACK,           /*  6: logic-bubble       */
51   BLACK,           /*  7: dots-grid          */
52   BLACK,           /*  8: detached-attribute */
53   BLACK,           /*  9: text               */
54   BLACK,           /* 10: bus                */
55   GRAY,            /* 11: select             */
56   GRAY,            /* 12: bounding-box       */
57   GRAY,            /* 13: zoom-box           */
58   GRAY,            /* 14: stroke             */
59   BLACK,           /* 15: lock               */
60   GRAY,            /* 16: output-background  */
61   GRAY,            /* 17: freestyle1         */
62   GRAY,            /* 18: freestyle2         */
63   GRAY,            /* 19: freestyle3         */
64   GRAY,            /* 20: freestyle4         */
65   BLACK,           /* 21: junction           */
66   GRAY,            /* 22: mesh-grid-major    */
67   GRAY             /* 23: mesh-grid-minor    */
68 };
69 
70 
71 size_t
colors_count()72 colors_count()
73 {
74   return COLORS_COUNT;
75 }
76 
77 
78 
79 /* \brief Check if a color \id is valid (one of the defined *_COLOR constants).
80  */
81 gboolean
color_id_valid(size_t id)82 color_id_valid (size_t id)
83 {
84   return id >= 0 && id < colors_count();
85 }
86 
87 
88 
89 size_t
default_color_id()90 default_color_id()
91 {
92   return DEFAULT_COLOR;
93 }
94 
95 
96 const LeptonColor*
lepton_colormap_color_by_id(const LeptonColor * color_map,size_t id)97 lepton_colormap_color_by_id (const LeptonColor *color_map,
98                              size_t id)
99 {
100   return &color_map[id];
101 }
102 
103 void
lepton_colormap_disable_color(LeptonColor * color_map,size_t id)104 lepton_colormap_disable_color (LeptonColor *color_map,
105                                size_t id)
106 {
107   color_map[id].enabled = FALSE;
108 }
109 
110 void
lepton_colormap_set_color(LeptonColor * color_map,size_t id,guint8 r,guint8 g,guint8 b,guint8 a)111 lepton_colormap_set_color (LeptonColor *color_map,
112                            size_t id,
113                            guint8 r,
114                            guint8 g,
115                            guint8 b,
116                            guint8 a)
117 {
118   color_map[id].enabled = TRUE;
119   color_map[id].r = r;
120   color_map[id].g = g;
121   color_map[id].b = b;
122   color_map[id].a = a;
123 }
124 
125 
126 /*! \brief Get the color blue value as a double
127  *
128  *  A getter until colors convert to double natively
129  *
130  *  \param [in] color the color
131  *  \return the blue value
132  */
133 gdouble
lepton_color_get_blue_double(const LeptonColor * color)134 lepton_color_get_blue_double (const LeptonColor *color)
135 {
136   g_return_val_if_fail (color != NULL, 1.0);
137 
138   return color->b / 255.0;
139 }
140 
141 /*! \brief Get the color green value as a double
142  *
143  *  A getter until colors convert to double natively
144  *
145  *  \param [in] color the color
146  *  \return the green value
147  */
148 gdouble
lepton_color_get_green_double(const LeptonColor * color)149 lepton_color_get_green_double (const LeptonColor *color)
150 {
151   g_return_val_if_fail (color != NULL, 1.0);
152 
153   return color->g / 255.0;
154 }
155 
156 /*! \brief Get the color red value as a double
157  *
158  *  A getter until colors convert to double natively
159  *
160  *  \param [in] color the color
161  *  \return the red value
162  */
163 gdouble
lepton_color_get_red_double(const LeptonColor * color)164 lepton_color_get_red_double (const LeptonColor *color)
165 {
166   g_return_val_if_fail (color != NULL, 1.0);
167 
168   return color->r / 255.0;
169 }
170 
171 /*! \brief Get the color alpha value as a double
172  *
173  *  A getter until colors convert to double natively
174  *
175  *  \param [in] color the color
176  *  \return the alpha value
177  */
178 gdouble
lepton_color_get_alpha_double(const LeptonColor * color)179 lepton_color_get_alpha_double (const LeptonColor *color)
180 {
181   g_return_val_if_fail (color != NULL, 1.0);
182 
183   return color->a / 255.0;
184 }
185 
186 
187 /*! \brief Initialise a color map to B&W
188  *  \par Function Description
189  *  Initialises a color map to a simple default: black features on a
190  *  white background, with "special" colors as gray.
191  *
192  *  \param map Color map to initialise.
193  */
194 void
lepton_color_map_init(LeptonColorMap map)195 lepton_color_map_init (LeptonColorMap map)
196 {
197   for (size_t i = 0; i < colors_count(); ++i)
198   {
199     map[ i ] = default_colors[ i ];
200   }
201 }
202 
203 
204 
205 /*! \brief Initialises the color subsystem
206  *  \par Function Description
207  *  At the moment, just initialises the print color map.
208  */
209 void
s_color_init()210 s_color_init()
211 {
212   lepton_color_map_init (print_colors);
213 }
214 
215 LeptonColor*
print_colors_array()216 print_colors_array ()
217 {
218   return print_colors;
219 }
220 
221 
222 /*! \brief: For a given \a color_index, get Scheme symbol name
223  */
224 const gchar*
color_get_name(int color_index)225 color_get_name (int color_index)
226 {
227   switch (color_index)
228   {
229     case BACKGROUND_COLOR:         return "background";
230     case PIN_COLOR:                return "pin";
231     case NET_ENDPOINT_COLOR:       return "net-endpoint";
232     case GRAPHIC_COLOR:            return "graphic";
233     case NET_COLOR:                return "net";
234     case ATTRIBUTE_COLOR:          return "attribute";
235     case LOGIC_BUBBLE_COLOR:       return "logic-bubble";
236     case DOTS_GRID_COLOR:          return "dots-grid";
237     case DETACHED_ATTRIBUTE_COLOR: return "detached-attribute";
238     case TEXT_COLOR:               return "text";
239     case BUS_COLOR:                return "bus";
240     case SELECT_COLOR:             return "select";
241     case BOUNDINGBOX_COLOR:        return "bounding-box";
242     case ZOOM_BOX_COLOR:           return "zoom-box";
243     case STROKE_COLOR:             return "stroke";
244     case LOCK_COLOR:               return "lock";
245     case OUTPUT_BACKGROUND_COLOR:  return "output-background";
246     case FREESTYLE1_COLOR:         return "freestyle1";
247     case FREESTYLE2_COLOR:         return "freestyle2";
248     case FREESTYLE3_COLOR:         return "freestyle3";
249     case FREESTYLE4_COLOR:         return "freestyle4";
250     case JUNCTION_COLOR:           return "junction";
251     case MESH_GRID_MAJOR_COLOR:    return "mesh-grid-major";
252     case MESH_GRID_MINOR_COLOR:    return "mesh-grid-minor";
253     default:
254       break;
255   }
256 
257   return "";
258 
259 } /* color_get_name() */
260 
261 
262 
263 /*! \brief: For a given \a color_index, get (localized) human readable name
264  */
265 const char*
color_get_strname(int color_index)266 color_get_strname (int color_index)
267 {
268   switch(color_index)
269   {
270     case BACKGROUND_COLOR:         return _("Background");
271     case PIN_COLOR:                return _("Pin");
272     case NET_ENDPOINT_COLOR:       return _("Net endpoint");
273     case GRAPHIC_COLOR:            return _("Graphic");
274     case NET_COLOR:                return _("Net");
275     case ATTRIBUTE_COLOR:          return _("Attribute");
276     case LOGIC_BUBBLE_COLOR:       return _("Logic bubble");
277     case DOTS_GRID_COLOR:          return _("Grid point");
278     case DETACHED_ATTRIBUTE_COLOR: return _("Detached attribute");
279     case TEXT_COLOR:               return _("Text");
280     case BUS_COLOR:                return _("Bus");
281     case SELECT_COLOR:             return _("Selection");
282     case BOUNDINGBOX_COLOR:        return _("Bounding box");
283     case ZOOM_BOX_COLOR:           return _("Zoom box");
284     case STROKE_COLOR:             return _("Stroke");
285     case LOCK_COLOR:               return _("Lock");
286     case OUTPUT_BACKGROUND_COLOR:  return _("Output background");
287     case FREESTYLE1_COLOR:         return _("Freestyle 1");
288     case FREESTYLE2_COLOR:         return _("Freestyle 2");
289     case FREESTYLE3_COLOR:         return _("Freestyle 3");
290     case FREESTYLE4_COLOR:         return _("Freestyle 4");
291     case JUNCTION_COLOR:           return _("Net junction");
292     case MESH_GRID_MAJOR_COLOR:    return _("Mesh grid major");
293     case MESH_GRID_MINOR_COLOR:    return _("Mesh grid minor");
294     default:
295       break;
296   }
297   return _("Unknown");
298 
299 } /* color_get_strname() */
300