gtkIOStream  1.7.0
GTK+ << C++ IOStream operators for GTK+. Now with ORBing, numerical computation, audio client and more ...
ColourLineSpec.H
Go to the documentation of this file.
1 /* Copyright 2000-2018 Matt Flax <flatmax@flatmax.org>
2  This file is part of GTK+ IOStream class set
3 
4  GTK+ IOStream is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  GTK+ IOStream is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You have received a copy of the GNU General Public License
15  along with GTK+ IOStream
16 */
17 #ifndef COLOURLINESPEC_H_
18 #define COLOURLINESPEC_H_
19 
20 #include <stdlib.h>
21 #include <string.h>
22 #include <string>
23 #include <vector>
24 #include <gtk/gtk.h>
25 
26 #include <cairo/cairo.h>
27 
28 using namespace std;
29 
30 #ifndef uint
31 typedef unsigned int uint;
32 #endif
33 
36 template <int dummy>
38 protected:
39  static int colourCntPrivate;
40  static const char *shortColoursPrivate[];
41  static const char *X11ColoursPrivate[];
42 };
43 // setup the possible plot colours ...
45 template <int dummy> int StandardColours<dummy>::colourCntPrivate=10;
47 template <int dummy> const char *StandardColours<dummy>::shortColoursPrivate[]= {(char *)"b",(char *)"g",(char *)"r",(char *)"c",(char *)"m",(char *)"y",(char *)"w",(char *)"k",(char *)"o",(char *)"a"}; // standard colours
49 template <int dummy> const char *StandardColours<dummy>::X11ColoursPrivate[]= {(char *)"blue", (char *)"green", (char *)"red", (char *)"cyan", (char *)"magenta", (char *)"yellow", (char *)"white", (char *)"black", (char *)"orange", (char *)"gray"};
50 
61 class ColourLineSpec : protected StandardColours<0> {
64  float size;
65 
66 #if GTK_MAJOR_VERSION==3
67  GdkRGBA colour;
68 #else
69  GdkColor colour;
70 #endif
71 public:
72 
73  vector<string> shortColours;
74  vector<string> X11Colours;
75 
76  bool linePlot;
77 
82  setColourAndLinesOrPoints("b1");
83  }
84 
90  ColourLineSpec(const char* clsChar) {
91  if (strlen(clsChar)==0)
92  setColourAndLinesOrPoints("b1");
93  else
94  setColourAndLinesOrPoints(clsChar);
95  }
96 
105  bool wasSizeSpecified(void){
106  return !sizeNotSpecified;
107  }
108 
117  bool wasColourSpecified(void){
118  return !colourNotSpecified;
119  }
120 
124  void setSize(int s){
125  sizeNotSpecified=false;
126  size=(float)s;
127  }
131  void setSize(float s){
132  sizeNotSpecified=false;
133  size=s;
134  }
135 
139  float getSize(void) const {
140  return size;
141  }
142 
146 #if GTK_MAJOR_VERSION>2
147  void setColour(GdkRGBA c){
148 #else
149  void setColour(GdkColor c){
150 #endif
151  colourNotSpecified=false;
152  colour=c;
153  }
154 
158  void setColour(const char* c){
159  colourNotSpecified=false;
160 #if GTK_MAJOR_VERSION>2
161  gdk_rgba_parse(&colour, c);
162 #else
163  gdk_color_parse(c,&colour);
164 #endif
165  }
166 
170 #if GTK_MAJOR_VERSION>2
171  GdkRGBA getColour(void) const {
172 #else
173  GdkColor getColour(void){
174 #endif
175  return colour;
176  }
177 
181  gchar *getColourString(void){
182 #if GTK_MAJOR_VERSION>2
183  return gdk_rgba_to_string(&colour);
184 #else
185  return gdk_color_to_string(&colour);
186 #endif
187  }
188 
194  void setColourAndLinesOrPoints(const char* clsChar) {
195  sizeNotSpecified=true;
196  colourNotSpecified=true;
197 
198  linePlot=1;
199  string clsStr(clsChar);
200 #if GTK_MAJOR_VERSION>2
201  gdk_rgba_parse(&colour, "blue");
202 #else
203  gdk_color_parse("blue",&colour);
204 #endif
205  //cout<<"ColourLineSpec::setColourAndLinesOrPoints colour = "<<"blue"<<endl;
206  if (shortColours.size()==X11Colours.size() && X11Colours.size()!=0){ // if the user has specified their own colours
207  for (unsigned int i=0; i<X11Colours.size(); i++)
208  if (clsStr.find(shortColours[i].c_str())!=string::npos) {
209 #if GTK_MAJOR_VERSION>2
210  gdk_rgba_parse(&colour, X11Colours[i].c_str());
211 #else
212  gdk_color_parse(X11Colours[i].c_str(),&colour);
213 #endif
214  colourNotSpecified=false; // indicate that the colour was specified
215  break;
216  }
217  } else {
218  for (int i=0; i<colourCntPrivate; i++) // if the user didn't specify their own colours, then use the standard colours.
219  if (clsStr.find(shortColoursPrivate[i])!=string::npos) {
220 #if GTK_MAJOR_VERSION>2
221  gdk_rgba_parse(&colour, X11ColoursPrivate[i]);
222 #else
223  gdk_color_parse(X11ColoursPrivate[i],&colour);
224 #endif
225  colourNotSpecified=false; // indicate that the colour was specified
226  // cout<<"ColourLineSpec::setColourAndLinesOrPoints colour = "<<X11ColoursPrivate[i]<<endl;
227  break;
228  }
229  }
230 
231  if (clsStr.find(':')!=string::npos) { // check for the points plotting style
232  linePlot=false;
233  //cout<<"ColourLineSpec::setColourAndLinesOrPoints linePlot = "<<linePlot<<endl;
234  } //else cout<<"ColourLineSpec::setColourAndLinesOrPoints linePlot = "<<linePlot<<endl;
235 
236  size=1.;
237  unsigned int minC=clsStr.find_first_of("0123456789");
238  //cout<<"clsStr : "<<clsStr<<endl;
239  if ((minC!=string::npos) & (minC<=clsStr.size())) {
240  int maxC=clsStr.find_last_of("0123456789");
241  size=atof(clsStr.substr(minC,maxC-minC+1).c_str());
242  sizeNotSpecified=false; // indicate that the size was specified
243  }
244  //cout<<"ColourLineSpec::setColourAndLinesOrPoints size = "<<size<<endl;
245  }
246 
248  if (c.size()==0)
249  setColourAndLinesOrPoints("b1");
250  else
251  setColourAndLinesOrPoints(c.c_str());
252  return *this;
253  }
254 
255  ColourLineSpec &operator=(const char *c){
256  if (strlen(c)==0)
257  setColourAndLinesOrPoints("b1");
258  else
259  setColourAndLinesOrPoints(c);
260  return *this;
261  }
262 
266  static const char ** getColourLabels(void){
267  return shortColoursPrivate;
268  }
269 
274  static const char *getColour(uint i) {
275  return shortColoursPrivate[i%colourCntPrivate];
276  }
277 
282  ColourLineSpec &operator>>(cairo_t *cr){
283  if (cr){
284  //float maxVal=pow(2.,8.*(float)sizeof(colour.red));
285  float maxVal = (float)(1<<8*sizeof(colour.red));
286  cairo_set_source_rgb(cr, colour.red/maxVal, colour.green/maxVal, colour.blue/maxVal);
287  cairo_set_line_width(cr, size);
288  }
289  return *this;
290  }
291 
296  ColourLineSpec setRGBA(cairo_t *cr, double alpha){
297  if (cr){
298  float maxVal = (float)(1<<8*sizeof(colour.red));
299  cairo_set_source_rgba(cr, colour.red/maxVal, colour.green/maxVal, colour.blue/maxVal, alpha);
300  cairo_set_line_width(cr, size);
301  }
302  return *this;
303  }
304 };
305 
306 #endif // COLOURLINESPEC_H_
bool wasColourSpecified(void)
gchar * getColourString(void)
void setColourAndLinesOrPoints(const char *clsChar)
interprets the octave style line/colour spec. By default implements lines specify points using the &#39;:...
size(signal)
void setSize(int s)
ColourLineSpec(const char *clsChar)
use a octave like string to define the line/points or colour Speciy whether to use points or lines an...
float getSize(void) const
STL namespace.
GdkColor getColour(void)
ColourLineSpec(void)
default constructor with blue colour Initiates the defaul blue line type of size 1 ...
static const char ** getColourLabels(void)
bool wasSizeSpecified(void)
void setColour(const char *c)
unsigned int uint
unsigned int uint
Definition: Box.H:28
void setColour(GdkColor c)
ColourLineSpec & operator>>(cairo_t *cr)
ColourLineSpec setRGBA(cairo_t *cr, double alpha)
vector< string > shortColours
User specified colours in character notation :
ColourLineSpec & operator=(const char *c)
void setSize(float s)
bool colourNotSpecified
Set to true if the colour was not specified ... i.e. "12" for size 12 with no colour.
static const char * getColour(uint i)
float size
The size of the line or point.
GdkColor colour
The colour holder.
static int colourCntPrivate
The number of colours defined in the following char ** variables.
ColourLineSpec & operator=(string &c)
bool sizeNotSpecified
Set to true if the size was not specified ... i.e. "r" for red with no size.
vector< string > X11Colours
User specified colour strings :
bool linePlot
The indication for lines or points plotting.
encapsulates a method to specify colour and line or point plotting This class tries to encapsulate oc...
gtkIOStream: /tmp/gtkiostream/include/ColourLineSpec.H Source File
GTK+ IOStream  Beta